Google has improved the NDK development process, and for the development of the NDK under the Windows environment, if the NDK used is a prior version of R7, it is necessary to install Cygwin to use the NDK. And at the beginning of NDKr7, Google's Windows version of the NDK provides a ndk-build.cmd script so that it can be compiled directly from the script without the need to use Cygwin. Just add a builders to the Eclipse Android project, and the builder configured for Eclipse is actually executing cygwin and passing ndk-build as a parameter, so that eclipse will automatically compile the NDK. So let's start now:
A little explanation about the NDK:
NDK Full Name: Native development Kit.
The NDK provides a range of tools to help developers quickly develop C (or C + +) dynamic libraries and automatically package so and Java applications together as APK. These tools are great for developers.
The NDK integrates the cross compiler and provides the corresponding MK file isolation CPU, platform, ABI and other differences, developers simply need to modify the Mk file (stating "which files need to compile", "compilation characteristics Requirements", etc.), you can create a so.
The NDK can automatically package so with Java applications, greatly reducing the developer's packaging effort.
Second, NDK Construction of bad environment:
Note: At present has been out of ndk-r9b, because the author wrote this log when the download is still ndk-r8c, so still take ndk-r8c as an example to explain.
Similar operation, from Ndk-7, Google has improved the operation of the NDK, do not need to use Cygwin to cross-compile, which greatly improved our development speed.
(1) Download and install ndk-r8c.
: http://developer.android.com/sdk/ndk/index.html
(2) Open Eclipse, create a new Android project (my name is TESTNDK), create a new JNI folder under the project directory TESTNDK, which is used to save the file code that the NDK needs to compile, and so on.
(3) Start new and configure a builder
(a) Project->properties->builders->new, create a new builder.
(b) In the "Choose Configuration Type" dialog box that pops up, select "Program" and click "OK":
(c) in the "Edit Configuration" dialog box that pops up, configure the tab "Main".
Enter the name of the new builders in "name" (this name can be taken arbitrarily).
Enter the path of the Nkd-build.cmd in "location" (this is the path after downloading ndk8 after the decompression, this recommendation is placed under the root directory, the path cannot have spaces and Chinese). Depending on the respective NDK path settings, you can also click "Browser File System ..." to select this path.
Enter the TESTNDK location in "Working Diretcoty" (You can also click "Browse Workspace" to select the Testndk directory). 1
Figure 1
(d) continue to configure the tab "Refresh" in this "Edit Configuration" dialog box. 2
Tick "Refresh Resources upon completion",
Tick "The entire workspace",
Tick "recuresively include sub-folders".
Figure 2
(e) continue to configure the tab "Build options" in the "Edit Configuration" dialog box.
Tick "After a" and "clean", (tick this operation, if you want to compile the NDK, only need to clean the project to start cross-compiling)
Tick "During Manual builds",
Tick "During Auto Builds",
Tick "Specify working set of relevant resources". 3
Figure 3
Click "Specify Resources ..." To tick the new "JNI" directory in the TESTNDK project and click "Finish". Click "OK" to complete the configuration. 4
Figure 4
Here, congratulations, compile the environment and build successfully!
So the building is finished, of course, to play a play, well, let's run a demo test, let you know the NDK development process
Third, the NDK Program demo development (below is the key, please check carefully)
1. Create a new Jniclient.class in the TESTNDK project (in order to call C + + code) with the following:
Package Com.ndk.test;public class Jniclient { static public native string Addstr (string stra, String strB); static public native int AddInt (int a, int b);}
2. C + + header file to generate. h
(1) Use the cmd command to navigate to the directory where the Jniclient.class is located, enter "Javac Jniclient.java" after the return, generate Jniclinet.class file (if the project was built with Eclipse, in testndk\bin\ There are already jniclinet.class files in the Classes\com\ndk\test directory).
(2) Copy the Jniclinet.class to the Testndk\bin\classes\com\ndk\test directory, position the cmd command to the Testndk\bin\classes directory, and enter "Javah Com.ndk.test.JniClient "After the carriage return, the C + + header file is generated in the Testndk\bin\classes directory com_ndk_test_jniclient.h
The contents of the Com_ndk_test_jniclient.h file are as follows:
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class com_ndk_test_jniclient */#i Fndef _included_com_ndk_test_jniclient#define _included_com_ndk_test_jniclient#ifdef __cplusplusextern "C" {#endif/ * * Class: com_ndk_test_jniclient * Method: addstr * Signature: (ljava/lang/string; ljava/lang/string;) ljava/lang/string; */jniexport jstring jnicall java_com_ndk_test_jniclient_addstr (jnienv *, Jclass, jstring, jstring); */* Class: com_ndk_test_jniclient * Method: AddInt * Signature: (II) I */jniexport jint jnicall java_com_ndk_test_jniclient_addint (jnienv *, Jclass, Jint, Jint); #ifdef __cplusplus} #endif #endif
3. Create a new Android.mk file under the JNI directory with the following contents (about the Mk file needs to be noted, it is important, and C and C + + files of the Mk file is not the same, here is called the C language of the Mk file, as for the other how to call, this oneself go to Baidu Bar, here is not much to say)
Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = testndklocal_src_files: = Com_ndk_test_ Jniclient.cinclude $ (build_shared_library)
4. Copy the com_ndk_test_jniclient.h that you just manually generated to the JNI directory of the TESTNDK project.
Then create a new com_ndk_test_jniclient.c file to complete the implementation of the function in the header file, the content is as follows (originally wanted to write two methods, now only the first method, return a string "HelloWorld from JNI", another method is a A + B of the operation, method written here, interested in can study on their own):
Com_ndk_test_jniclient.c
#include "com_ndk_test_jniclient.h" #include <stdlib.h> #include <stdio.h> #ifdef __cplusplus extern " C " { #endif/ * * Class: com_ndk_test_jniclient * Method: addstr * Signature: (ljava/lang/string; ljava/lang/string;) ljava/lang/string; */jniexport jstring jnicall java_com_ndk_test_jniclient_addstr (jnienv *env, Jclass arg, jstring InstringA, jstring INSTRINGB) { jstring str = (*env)->newstringutf (env, "HelloWorld from JNI!"); return str; } /** Class: com_ndk_test_jniclient* Method: addint* Signature: (II) i*/jniexport jint jnicall Java_com_ndk_ Test_jniclient_addint (jnienv *env, Jclass Arg, jint A, Jint b) { return a + b;} #ifdef __cplusplus } #endif
At this point, when you edit the com_ndk_test_jniclient.c and save it, Project-clean the project, you can see the TESTNKD project under the Obj/local/armeabi directory will automatically generate Libtestndk.so library.
5. Complete the call to the function in Jniclient.java ( first statically load the dynamic-link so library ) in Testndkactivity.java:
Package Com.ndk.test;import Android.app.activity;import Android.os.bundle;import android.widget.textview;public Class Testndkactivity extends Activity { static { system.loadlibrary ("Testndk"); } /** called when the activity is first created. * /@Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); The first method passed two parameters without doing an operation, directly returning Hello Jni, without the tube String str = jniclient.addstr ("Test", "Test"); The second method is temporarily not implemented // int isum = Jniclient.addint (5, 2); String Strsum = "5 + 7 =" + Isum; TextView TV1 = new TextView (this); Tv1.settext (str); Setcontentview (TV1); }}
6. Run the TESTNDK project and you can see the interface output from the "HelloWorld from JNI" in the com_ndk_test_jniclient.c file in the console! The
This completes the NDK instance.
Further complex operations require in-depth learning ndk/jni,
For example, the data type conversion of the C + + and Java, and the writing format of android.mk file.
If there is any problem, welcome to communicate! Thank you very much for your interest in Andye! About the. h file how to generate, you can see my other log, although it is written in Cygwin, a bit old, but there is about how to generate. h, for reference, address: http://www.cnblogs.com/yejiurui/archive/2012/11/ 21/2780940.html
The most understandable Android JNI development data in history--NDK environment building