Getting the android JNI helloworld

Older steps

After creating the project

  • Open gradle.properties, added:
android.useDeprecatedNdk=true
  • Open local.properties, added:
ndk.dir=NDK的路径
  • Finally, open the app build.gradle, add ndk disposed android / defaultConfig below
ndk {
            moduleName "JNISample"  //moduleName 表示编译出的so文件的名字
        }
  • Native declare a class method (Method C / C ++ to write) at any
 public static native String getJniString();
  • Into the source code directory in the terminal where the method according to the native file path, enter the following command to generate header filescom_instructionextract_sdkdemo_sdkDemo.h
javah com.instructionextract.sdkdemo.sdkDemo
  • Create a directory for the jni Native level code, to move the first document in the main directory to directory jni
  • Using the name of the header file, create cpp file com_instructionextract_sdkdemo_sdkDemo.cppto write the code, for example,
#include <com_instructionextract_sdkdemo_sdkDemo.h>

//方法定义是从.h头文件中copy过来的
JNIEXPORT jstring JNICALL Java_com_instructionextract_sdkdemo_sdkDemo_Anti_1TraceMe
  (JNIEnv *, jclass){
    return env -> NewStringUTF("Hello World");;
 }
  • Native layer using a method in java layer, first load the library file, the file name is defined by moduleName
    static {
        System.loadLibrary("AntiDebug");
    }

The new steps

Google officially recommended reference to add C and C ++ code to your project , install the NDK, LLDB, when new projects in the Configure your new project of the wizard, select the Include C ++ Support check box.

After creating the project, both to native code, and new projects there is a method of acoustic demo generated by the system can refer to in the cpp directory

reference

Android Studio jni development of entry - I see enough!

Published 118 original articles · won praise 14 · views 50000 +

Guess you like

Origin blog.csdn.net/github_38641765/article/details/90653404