1. Create a new project and add the Jni folder.
2. Add the CPP file in the generated Jni folder.
3. Add the code to the CPP file that you added.Here we use the dynamic registration method toadding JNI functions is convenient and does not require Javah to generate. h Files
#include <jni.h> #include <android/log.h> #include <assert.h> #include <stdio.h>/* output log macro */# ifndef log#define Log_tag "imgprocess" #define ALOGD (...) __android_log_print (Android_log_debug, Log_tag, __va_args__); #define ALOGE (...) __android_log_print (Android_log_error, Log_tag, __va_args__); #define ALOGV (...) __android_log_print (Android_log_verbose, Log_tag, __va_args__); #endif log#ifndef nelem# define Nelem (x) ((int) (sizeof (x)/sizeof ((x) [0])) #endifjstring Native_hello (jnienv* env, Jobject thiz) {return env->newstringutf ("Hello World From jni~ ");} The native function is located in the Java class where the static const char *classpathname = "com/test/hc/mainactivity";//mapping table, where we correspond the Java function and the C function to static. Jninativemethod gmethods[] = {{"Native_hello", "() ljava/lang/string;", (void*) Native_hello},};int registerNativeMe Thods (jnienv* env, const char* className, jninativemethod* gmethods, int nummethods) { Jclass Clazz; CLazz = Env->findclass (className); if (clazz = = NULL) {aloge ("Native Registration unable to find class '%s '", className); return jni_false; } if (Env->registernatives (Clazz, Gmethods, Nummethods) < 0) {Aloge ("Registernatives failed for '%s '", cl Assname); return jni_false; } return jni_true;} Staticint Register_my_jni_methods (jnienv* env) {return registernativemethods (env, Classpathname, Gmethods, NELEM ( gmethods));} When we load the so library, Jni_onload will automatically be called to register the JNI function Jint jni_onload (javavm* vm, void*) {jnienv* env = NULL; Jint result =-1; if (vm->getenv (void**) &env, jni_version_1_4)! = JNI_OK) {aloge ("error:getenv failed\n"); Goto bail; } assert (env! = NULL); if (Register_my_jni_methods (env) < 0) {Aloge ("Error:native registration failed\n"); Goto bail; } aloge ("Success:native registration successed\n"); result = Jni_version_1_4; Bail:return result;}
4. Add NDK compilation items to the app's build.properties as follows
ModuleName: Generate the name of so library Ldlibs: the library to use when linking, because we want to print the log, we need to reference abifilters: Which ABI architectures are generated so libraries
5.in thegradle.propertiesAdd android.usedeprecatedndk=true in
Otherwise, the following error will occur:
Error: (0) ERROR:NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin. for details, see Http://tools.android.com/tech-docs/new-build-system/gradle-experimental. Set "Android.usedeprecatedndk=true" in gradle.properties to continue using the current NDK integration.
6. Configure the NDK path in local.properties
7. Load the so library in Mainactivity and declare the native function
8. Run the time to find the NDK error, this is the NDK under Windows a bug, when just compile a file, this problem occurs when the solution is to add an empty CPP file to the Jni folder, theNDK generated so file in the app In the/build/intermediates/ndk/debug/lib directory, it is automatically packaged into the APK :
Last Run result
Android Studio JNI Programming and NDK environment configuration