Practical Android skills 23: Android Studio ndk development and androidndk
The newer NDK version is r10b, and Android Studio's support for NDK development is still in the conception stage. Therefore, many tasks such as generating header files with javah need to be done by yourself. Today, we will use an example to demonstrate the NDK development in.
Create a project SecondNdkTest
Create a Module named ndklibrary in this project and separate it as the so library. Create a new java class SecondLib in the library with the following content:
package com.linc.ndklibrary;/** * Created by linc on 15-3-29. */public class SecondLib { // Native implementation static { System.loadLibrary("SecondLib"); } //int array public static native int[] intMethod(); //string array public static native String[] stringMethod();}
Build-> Make Module 'ndklibrary', so that the SecondLib compilation is complete. Use SecondLib. class to generate the C header file with javah, as shown below:
AndroidStudioProjects/SecondNdkTest/ndklibrary/src/main$ javah -d jni -classpath ../../build/intermediates/classes/debug com.linc.ndklibrary.SecondLibAndroidStudioProjects/SecondNdkTest/ndklibrary/src/main$ lsAndroidManifest.xml java jni resAndroidStudioProjects/SecondNdkTest/ndklibrary/src/main$ ls jni/com_linc_ndklibrary_SecondLib.h
The header file content is as follows:
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_linc_ndklibrary_SecondLib */#ifndef _Included_com_linc_ndklibrary_SecondLib#define _Included_com_linc_ndklibrary_SecondLib#ifdef __cplusplusextern "C" {#endif/* * Class: com_linc_ndklibrary_SecondLib * Method: intMethod * Signature: ()[I */JNIEXPORT jintArray JNICALL Java_com_linc_ndklibrary_SecondLib_intMethod (JNIEnv *, jclass);/* * Class: com_linc_ndklibrary_SecondLib * Method: stringMethod * Signature: ()[Ljava/lang/String; */JNIEXPORT jobjectArray JNICALL Java_com_linc_ndklibrary_SecondLib_stringMethod (JNIEnv *, jclass);#ifdef __cplusplus}#endif#endif
Native code implementation
Create the c file SecondLib. c and the header file in the jni directory. Implement the above two methods respectively. The content is as follows:
# Include "com_linc_ndklibrary_SecondLib.h" const static int length = 10; // int array JNIEXPORT jintArray JNICALL inline (JNIEnv * env, jclass obj) {jintArray array; array = (* env) -> NewIntArray (env, 10); int I = 1; for (; I <= 10; ++ I) {(* env)-> SetIntArrayRegion (env, array, i-1, 1, & I) ;}// get array length int len = (* env)-> GetArrayLength (env, array); // array content jint * elems = (* Env)-> GetIntArrayElements (env, array, 0); return array;} // string array JNIEXPORT jobjectArray JNICALL sums (JNIEnv * env, jclass obj) {jclass class = (* env)-> FindClass (env, "java/lang/String"); jobjectArray string = (* env)-> NewObjectArray (env, (jsize) length, class, 0); jstring jstr; char * _ char [] = {"my", "name", "is", "linc !! "," Being "," Learning "," JNI "," and "," NDK "," technology! "}; Int I = 0; for (; I <length; ++ I) {jstr = (* env)-> NewStringUTF (env, _ char [I]); (* env)-> SetObjectArrayElement (env, string, I, jstr);} return string ;}
Compile
Add the ndk path to local. properties:
ndk.dir=/opt/ndk/android-ndk-r10b
Add the ndk definition to defaultConfig in build. gradle of ndklibrary, as shown below:
defaultConfig { minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName "1.0" ndk{ moduleName "SecondLib" } }
In this way, you can directly compile the make file without writing the make file yourself.
Build-> Make Module 'ndklibrary'. The generated so is as follows:
AndroidStudioProjects/SecondNdkTest$ find -name *.so./ndklibrary/build/intermediates/ndk/debug/lib/armeabi/libSecondLib.so./ndklibrary/build/intermediates/ndk/debug/lib/armeabi-v7a/libSecondLib.so./ndklibrary/build/intermediates/ndk/debug/lib/mips/libSecondLib.so./ndklibrary/build/intermediates/ndk/debug/lib/x86/libSecondLib.so./ndklibrary/build/intermediates/ndk/debug/obj/local/armeabi/libSecondLib.so./ndklibrary/build/intermediates/ndk/debug/obj/local/armeabi-v7a/libSecondLib.so./ndklibrary/build/intermediates/ndk/debug/obj/local/mips/libSecondLib.so./ndklibrary/build/intermediates/ndk/debug/obj/local/x86/libSecondLib.so
Note:
For the method of using native directly in the Activity, see the first two links below. The problem I encountered was not solved:
$ javah -d jni -classpath /opt/sdk/platforms/android-5.1/android.jar;../../build/intermediates/classes/debug com.linc.secondndktest.MainActivityError: no classes specifiedbash: ../../build/intermediates/classes/debug/: Is a directory
Refer:
Http://blog.csdn.net/rznice/article/details/42295215
Http://blog.csdn.net/sodino/article/details/41946607
Http://stackoverflow.com/questions/10483959/javah-error-android-app-activity-not-found
Http://blog.csdn.net/lincyang/article/details/6705143