2. Set in the root directory: local.properties Add NDK configuration:
ndk.dir=D\:\\Users\\Administrator\\AppData\\Local\\Android\\sdk\\ ndk-bundle
3. Set the project's build.gradle and add
ndk{
moduleName "JniTest"
ldLibs "log","z","m"
abiFilters "armeabi" , "armeabi-v7a" ,"x86" to defaultConfig{
}
4. Add a static code block to MainActivity
static { System.loadLibrary("JniTest"); }
The name of JniTest should be consistent with the moduleName in ndk{} in build.gradle;
add the JNI method
public native String getStringFromNative();
5. cd to the same level directory as AndroidManifest.xml, execute
javah -d ../jni com_example_administrator_aaa_MainActivity
, the com_example_administrator_aaa_MainActivity.h header file will be generated in the jni directory
6. Manually create a c file: main.c
content:
#include "com_example_administrator_aaa_MainActivity.h" #include <stdio.h> #include <stdlib.h> JNIEXPORT jstring JNICALL Java_com_example_administrator_aaa_MainActivity_getStringFromNative (JNIEnv *env, jobject jobj) { return (*env) -> NewStringUTF(env,"FUCK YOU"); }
After the above steps are completed, you can call getStringFromNative