Android 23: The NDK development of Android Studio

Source: Internet
Author: User

The newer NDK version is the r10b,android Studio support for NDK development is still in the ideation stage, so many jobs such as using Javah to generate header files and so on do it yourself. Use an example today to demonstrate the NDK development in as.

Create a new project Secondndktest

Create a new module in this project called Ndklibrary, which is drawn separately as so library. Create a new Java class secondlib in the library with the following content:

 PackageCom.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 nativeString[]Stringmethod();}

Build->make Module ' ndklibrary ', so the Secondlib is compiled, through the Secondlib.class, with the Javah to generate C header file, as follows:

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 contents of the header file are as follows:

/* Don't EDIT this file-it are 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 JintarrayJnicallJava_com_linc_ndklibrary_secondlib_intmethod (jnienv *, jclass);/* * class:com_linc_ndklibrary_secondlib * Method:stringmethod * Signature: () [ljava/lang/string; * *Jniexport JobjectarrayJnicallJava_com_linc_ndklibrary_secondlib_stringmethod (jnienv *, jclass);#ifdef __cplusplus}#endif#endif
Native code Implementation

In the JNI directory, the new C file secondlib.c corresponds to the header file, respectively, to implement the above two methods, the contents are as follows:

#include "com_linc_ndklibrary_secondlib.h" Conststatic int length=Ten;//iNT Array Jniexport jintarray jnicall java_com_linc_ndklibrary_secondlib_intmethod (jnienv *env, Jclass obj) {JINTARR Ay array; Array=(*env)Newintarray (ENV,Ten); int i=1; for(; i<=Ten; ++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);returnArray }//String array jniexport jobjectarray jnicall java_com_linc_ndklibrary_secondlib_stringmethod (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!!","In Progress","Learning","JNI","and","NDK","Technology! "}; int i=0; for(; i<length;++i) { Jstr=(*env)Newstringutf (Env,_char[i]); (*env)Setobjectarrayelement (ENV,STRING,I,JSTR); }returnString }
Compile

To add the NDK path in local.properties:

ndk.dir=/opt/ndk/android-ndk-r10b

Then add the NDK definition in the Ndklibrary build.gradle Defaultconfig, as follows:

    defaultConfig {        15        22        1        "1.0"        ndk{            "SecondLib"        }    }

This allows you to compile directly without having to write your own make files.
Build->make Module ' ndklibrary ', generated so 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:
Refer to the first two links below for a direct use of native in the activity. I have encountered a problem that has not been resolved:

$ 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

Reference:
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

Android 23: The NDK development of Android Studio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.