Android NDK (JNI) development

Source: Internet
Author: User

"Windows-based, Android NDK (JNI) Development Technology"


Summary This article describes how to use the Android NDK technology in Eclipse to develop "JNI (Java Native Interface)" On the Android platform, based on the Windows platform. Android NDK Development requires a certain Java JNI Technology Foundation. Android NDK essence, can be simply considered as a set of packages provided by the Android platform, the use of a number of C + + code in the form of JNI for the Android platform reuse, the technology can make the previously written, mature C/s The code can be ported to Android without modification or modification. At the same time, in some sensitive special scenarios, it may be necessary to have C + + code, such as CPU sensitive, graphics image etc.
"keywords" Android, NDK, JNI, Windows


The Android platform for the NDK (Native Development Kit) is developed and needs to be downloaded from Google's official Android NDK SDK Development Kit. In the older version of Android NDK development, additional downloads are required to install third-party Cygwin, but the latest Android NDK has integrated the Cygwin feature and does not require a separate download of the integrated Cygwin, which is Cygwin-free installation. Android NDK official download page: https://developer.android.com/ndk/downloads/index.html, download and use of Android in this article The NDK version is a 64-bit Windows-based Android NDK r10e version:

Version name: Windows 64-bit, Android-ndk-r10e-windows-x86_64.exe
Download Link: Http://dl.google.com/android/ndk/android-ndk-r10e-windows-x86_64.exe

Windows 64-bit, android-ndk-r10e-windows-x86_64.exe after the download is complete is a file:


is actually a compressed file containing the Android NDK development resources, click Install and unzip it to a folder. This example is placed in the following: D:\Program\android\ndk directory.

Then open eclipse. Windows-Preferences-Android, NDK,


In the NDK location, select the Android NDK directory you just installed.
This is where the Android NDK development environment is built.

The next step is how to use and specifically develop.
To create a new Ndktest Android project:



Then add Ndk:add Native support for the project Ndktest, right-click on the project in Ndktest, Android Tools, add Native support,


When add Natvie support, Eclipse Pop-up dialog, prompt to specify the Lib name, the default is the project name, this example uses Ndktest as the Lib name.


(Note: The Android NDK automatically adds the prefix "Lib" to the library, so that the last library name is "libxxx.so", assuming that it is originally LIB in its own project, then the Android NDK will no longer add the "Lib" prefix and use your own name.) )

In this case, a JNI subdirectory is automatically generated in the project's directory, with two files under JNI: NDKTest.cpp and Android.mk,

Where ANDROID.MK does not need to be modified, and NDKTest.cpp is what we need to add to our implementation later.
Typically, the Ndk\jni library \ Method required in a project is best placed in a common Java class, which facilitates management and code maintenance. In this example, add a Myjni.java class file, in the Myjni.java class file, the function at the beginning of the native modifier, the modifier native that this is the JNI function we will implement, write the Myjni.java file content and storage location and content



Note that you need to first wrap the layer with static code blocks in this class, load our library files.

Here, the rest of the code work is to complete the native method. Run this project, at this time run this project is meaningless, we just to get the compiled *.class file before run, get the. class file before using the Javah compile command to obtain the JNI header file.
After run, a compiled class file package has been generated under the Ndktest/bin/classses directory,

Next we want to obtain the Myjni.java corresponding JNI header file, start the Java console to navigate to the classes directory, and then execute the javah command: Javah Zhangphil.ndktest.MyJNI,


If everything is OK, then it will be generated in the classes directory: zhangphil_ndktest_myjni.h header file, do not modify this code file. Zhangphil_ndktest_myjni.h inside the code content is as follows:

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Zhangphil_ndktest_myjni */# ifndef _included_zhangphil_ndktest_myjni#define _included_zhangphil_ndktest_myjni#ifdef __cplusplusextern "C" {# endif/* * Class:     zhangphil_ndktest_myjni * Method:    getString * Signature: () ljava/lang/string; */jniexport Jstring jnicall java_zhangphil_ndktest_myjni_getstring  (jnienv *, jobject); #ifdef __cplusplus} #endif #endif


Copy zhangphil_ndktest_myjni.h This header file to the JNI directory, structure hierarchy


Open the NDKTest.cpp file automatically generated by the IDE, there is actually only one line of code:


Next
1, we will include the header of this file in our zhangphil_ndktest_myjni.h file.
2, the implementation is defined in the Zhangphil_ndktest_myjni.h file:

Jniexport jstring jnicall java_zhangphil_ndktest_myjni_getstring  (jnienv *, jobject);

Native method.

(Note: java_zhangphil_ndktest_myjni_getstring (), which is a JNI signature function that is automatically generated by the Java system, is characterized by the beginning of Java, followed by our own package name, followed by the class name, Finally, we define our own native method, except that "." In the Java naming convention is replaced by "_"

Completion NDKTest.cpp, simple period, assuming our getstring () function simply returns "Hello, World!" I ' m from JNI! " String:

#include <jni.h> #include <zhangphil_ndktest_myjni.h>jniexport jstring jnicall java_zhangphil_ndktest_ Myjni_getstring  (jnienv * env, Jobject obj) {returnenv->newstringutf ("Hello, world! I ' m from JNI! ");}


At this point, all the code for the Android JNI has been completed, and the rest is to use our native method.
Write a simple Android activity to test our completed Android native function.

Package Zhangphil.ndktest;import Android.support.v7.app.actionbaractivity;import Android.widget.toast;import Android.os.bundle;public class Mainactivity extends actionbaractivity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Myjni JNI = new Myjni (); String s = jni.getstring (); Toast.maketext (this, S, Toast.length_long). Show ();}}

Run results



It worked!


Summarize:

Android NDK development can actually be thought of as the Java JNI under the Android platform framework, which comes down to the following steps:
(1th) Download and install the latest Android NDK, build your development environment, and then "ADD natvie support".
(2nd) Write the classes and native methods you need in your project. In this case, we have added the Myjni class, and in the Myjni class we have written a getstring () function to demonstrate that the function is native.
(3rd) Run this project, this stage of run is only to obtain the compiled class file. Then start the Java console, locate the classes package in the Bin directory, execute the javah command, and generate the. h header file for our Myjni class files. In this case, the zhangphil_ndktest_myjni.h file.
(4th) Copy the. h header files obtained in step 3rd to the JNI directory and then complete the. cpp file in the JNI directory, noting that the head of the. cpp needs to #include the. h header file that we produced in 3rd step. Also, complete the functions defined in the. h header file in the. cpp source code file.
In this case, the function defined in zhangphil_ndktest_myjni.h is:

Jniexport jstring jnicall java_zhangphil_ndktest_myjni_getstring  (jnienv *, jobject);
</pre><p></p><p> completed: </p><p><pre class= "cpp" name= "code" >jniexport Jstring jnicall java_zhangphil_ndktest_myjni_getstring  (jnienv * env, Jobject obj) {Returnenv->newstringutf (" Hello, World! I ' m from JNI! ");}

(5th Step) After the completion, you can directly in our project using the native method in the Myjni class GetString ().



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android NDK (JNI) development

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.