NDK (JNI) development in the Android studio environment

Source: Internet
Author: User

Preface 1, what is the NDK?

The NDK full name is native development KIT,NDK offers a range of tools to help developers quickly develop C (or C + +) dynamic libraries and automatically package so and Java applications together as APK. The NDK integrates a cross compiler (a cross compiler requires a UNIX or Linux system environment), and provides the corresponding MK file isolation CPU, platform, ABI, and other differences, developers simply need to modify the Mk file ("which files need to compile", "compile feature requirements", etc.), you can create a so.

2. Why use the NDK?

1, the protection of the Code. Because the Java layer Code of the APK is very easy to decompile, the C + + library is more difficult to reverse.
2. It is easy to use the existing open Source library. Most of the existing open source libraries are written in C + + code.
3, improve the execution efficiency of the procedure. Use c development for application logic that requires high performance to improve application execution efficiency.
4, easy to transplant. The library can be used in other embedded platforms easily.

3. What is JNI?

The full name of JNI is Java Native Interface, which provides a number of APIs that enable the communication of Java and other languages (mainly C and C + +).

4. Why use JNI?

The purpose of JNI is to enable Java methods to invoke some of the functions implemented by C.

5. What is the so file in Android?

The so file used in Android is a C + + library of functions. In the Jni of Android, the corresponding C language must first be packaged into so library and then imported into the Lib folder for Java to call.

Android Studio NDK development environment ConfigurationFirst of all, Studio is ready for Android SDK Development, which means that the previous JDK can be used for anything. So, the next step is the NDK's environment configuration. (Studio doesn't have to install Cygwin like Eclipse, which is the power of Studio) 1, download the NDK, you can go under yourself, or come here to download: "http://blog.csdn.net/qiujuer/article/details/42040963"

After downloading, double-click Run decompression, wait. Note: After the operation of the decompression process may be longer, be sure to wait for all decompression completed (cmd box will prompt everything finished). Then there will be ndk-build in the Extract directory (which will be used later). :

( Note: If there is no ndk-build, only ndk-build.cmd or other files are not complete, it is possible that the decompression is not completely caused by the proposed re-decompression, it must wait until all completed ).

2, open Studio, create a new project, File>>project structure set the NDK path, is the path you unzipped


then click OK.

3. New Local method

In activity (or write a Class), create the following:

At this time, the method name is red, reported red because the so file has not been generated, not through so file calls. (You can see the native keyword in the declaration of this method, which means that this method is a local method, which means that the method Getstrfromjni () is implemented by native code (c/S/s) and is only declared in Java code. )

4. Generate. h files (This step is not necessary, just to facilitate the writing of. c files, can be deleted)

Under Studio's terminal window, go to the project's Java directory (shown below) and enter the JAVAH-JNI package name. Class name, as follows:

Then enter, the corresponding. h file is generated, as follows. (If you have created a new local native method in more than one class, you can create multiple. h files by repeating the above actions separately)

This file is only intended to assist us in writing the corresponding. c file, which can be deleted when it is finished. The contents of the file are as follows:

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_example_administrator_ndkdemo_MainActivity */"C" {#endif/* * Class:     com_example_administrator_ndkdemo_MainActivity * Method:    getStringFromNative * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_example_administrator_ndkdemo_MainActivity_getStringFromNative  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif        

What's useful is

Careful observation can be seen that he is following the "Java_ Package name Class name local method name" to organize (after understanding these we can later do not generate. h file and then go directly to write. c files).

5, under Main (or under the app) create a new JNI folder, write a. c file


Create a new. c file under Jni and write the method in it, as follows:

6, JNI under the writing android.mk file
编辑Android.mk代码:LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE        := demo         //要生成的so库的名称,但实际为libdemo.soLOCAL_SRC_FILES     := demo.c       //要使用的文件,刚才编写的demo.c文件include $(BUILD_SHARED_LIBRARY)
7. Generate So File ( Important

In the studio console, enter the project's main (or app) directory (the previous level of the directory where JNI resides), and then enter Ndk-build (shown below) to compile successfully without problems.

Or go into the cmd operation is the same.
After compiling the project, you can see the Libs and obj folders generated in the app directory, where Libs is useful and the Obj folder is useless to delete. The generated libdemo.so file can be seen in the Libs.

8. Load so file, call local method

Note: In the above process may be error, need to make some settings

1, in the app's Build.gradle android node settings:


2, JNI under the new empty. c File

3. Add code in Gradle.properities as follows:

Last Click Run OK

NDK (JNI) development in the Android studio environment

Related Article

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.