Android JNI, NDK achieve

I. Introduction
Why use jni, ndk?
The efficiency of the C / C ++ is the native language, than java more efficient
code migration, if developed over the module before using C language, you can reuse c existing code
java decompiler is easier than C language general encryption algorithms are written in C language, not easy to be decompiled

Second, the configuration environment
1. Install the JDK, configure the environment variables:
the JAVA_HOME: D: \ installSoftCache \ jdk1.8.0_31 (jdk installation directory)
the CLASSPATH:;.% The JAVA_HOME% \ lib (jdk directory reference, attention front.)
Increased Path : D: \ installSoftCache \ jre1.8.0_31 \ bin

If problems arise: "android in javah is not an internal command" because the environment variable is not configured or android studio does not refer to jdk directory, enter the command in the Terminal window:
setX the PATH "% the PATH%; D: \ installSoftCache \ jre1.8.0 _31 \ bin "

Third, the project to achieve native method
1. Create a new Android project, a package named com.whh.jni4ndk

2. New Package ndk, in which the new NDKTools.class file
contents of the file

public class NDKTools {
    static {
        System.loadLibrary("jni4ndk-jni");
    }

    public static native String getStringFromNDK();
}

The display in the UI native SUMMARY

 String text = NDKTools.getStringFromNDK();
 ((TextView)findViewById(R.id.tv)).setText(text);

4. Compile the project to generate classes file

5. In the Terminal window, enter the app \ src \ mian directory, enter the command:
E: \ Android_workspace \ JNI4NDK \ App \ src \ main> javah -d JNI -jni -classpath .... \ Build \ Intermediates \ classes \ debug com.whh.jni4ndk.ndk.NDKTools
after a few seconds JNI4NDK \ app \ src \ main \ jni following generation com_whh_jni4ndk_ndk_NDKTools.h file, as follows

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_whh_jni4ndk_ndk_NDKTools */

#ifndef _Included_com_whh_jni4ndk_ndk_NDKTools
#define _Included_com_whh_jni4ndk_ndk_NDKTools
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_whh_jni4ndk_ndk_NDKTools
 * Method:    getStringFromNDK
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_whh_jni4ndk_ndk_NDKTools_getStringFromNDK
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

6. Add the corresponding .c files in the directory jni

#include "com_whh_jni4ndk_ndk_NDKTools.h"

JNIEXPORT jstring JNICALL Java_com_whh_jni4ndk_ndk_NDKTools_getStringFromNDK
  (JNIEnv *env, jobject obj){
     return (*env)->NewStringUTF(env,"Hellow World,这是慧慧同学的NDK的第一行代码");
  }

7. Increase Android.mk file in the directory jni, which reads as follows:


include $(CLEAR_VARS)

LOCAL_MODULE    := jni4ndk-jni

LOCAL_SRC_FILES := jni4ndktest.c

include $(BUILD_SHARED_LIBRARY)

8. Check local.properties ndk file contains directory configuration, the configuration is not
open AS-Settings- search sdk- selected SDK Toos, mounted in the list select ndk


3013099-5c5aae33c6b8f091.png
ndk Download

In the final document local.properties add:

ndk.dir=D\:\\installSoftCache\\Android\\sdk\\ndk-bundle
sdk.dir=D\:\\installSoftCache\\Android\\sdk

ndk configuration is complete, making the project support NDK, behind gradle.properties need to add a file in the project:

Android.useDeprecatedNdk=true 

9. In order to generate the file so, the increase in the build.gradle app module directory content


3013099-f4d04dfb70d73b44.png
app gradle Configuration
ndk{
            moduleName "jni4ndk-jni"
            abiFilters "armeabi", "armeabi-v7a", "x86"
        }
externalNativeBuild {
            ndkBuild {
                path 'src/main/jni/Android.mk'
            }
        }
        sourceSets.main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['src/main/jniLibs']
        }

四、总结
JNI和NDK
JNI是Java调用Native 语言的一种特性,属于Java,Java本地接口,使Java与本地其他类型语言交互(C++)
实现步骤:在Java中声明Native方法,编译该文件得到.class文件,通过javah命令导出JNI头文件(.h文件),使用Java需要交互的本地代码实现子啊Java中声明的Native方法,编译so文件,通过Java执行Java程序,最终实现Java调用本地代码
NDK(Native Develop Kit):Android开发工具包,属于Android。
作用:快速开发C、C++动态库,并自动将so文件和应用打包成APK,即可通过NDK在Android中使用JNI与本地代码(C、C++)交互(Android开发需要本地代码C、C++实现)
特点:运行效率高,代码安全性高,功能拓展性好,易于代码复用和移植。
使用步骤:①配置NDK环境;②创建Android项目,并于NDK进行关联;③在Android项目中声明所需调用的Native方法;④使用该Native方法;⑤通过NDK build命令编译产生so文件;⑥编译AS工程,实现调用本地代码。
JNI和NDK的关系:JNI实现目的,NDK是Android实现JNI的手段,即在AS开发环境中通过NDK从而实现JNI功能。

Reference article:
one day master the Android JNI local programming Quick Start - rocomp - blog Park
https://www.cnblogs.com/rocomp/p/4892866.html
Android JNI Learning (II) - JNI combat the "hello world" - Jane book
https://www.jianshu.com/p/b4431ac22ec2

A little bit of progress every day (2019-06-02):
I share the collected works of art catalog https://www.jianshu.com/p/23485c157d76

Guess you like

Origin blog.csdn.net/weixin_34415923/article/details/90967384