1, downloadNDK,Latest VersionAndroid-ndk-r9.
Windows 32-bit version :
Http://dl.google.com/android/ndk/android-ndk-r9-windows-x86.zip
Windows 64-bit version :
Http://dl.google.com/android/ndk/android-ndk-r9-windows-x86_64.zip
2. Unpack the compressed package.
Android-ndk-r9\build: Put some cross-compilation tools
Android-ndk-r9\platforms\android-9: Compiling corresponding processor tools for different processors
View \android-ndk-r9\platforms\android-9\arch-arm\usr\include : jni.h File Simple Understanding of thejni protocol
For example:
1, typedef const struct JNINATIVEINTERFACE* jnienv;
JNIEnv is a constant body structure pointer alias for const struct jninativeinterface*
3. Configure Environment variables.
1. Enter the D:\android-ndk-r9-windows-x86\android-ndk-r9\ directory:
Enter command:/D D:\android-ndk-r9-windows-x86\android-ndk-r9\
Execute command:ndk-build
If once and for all, you need to configure environment variables
D:\android-ndk-r9-windows-x86\android-ndk-r9
Open any command-line input command: Ndk-build
———————————— look at a slightly more complex configuration process development ————————————
3. Create Project 4, create Jni folder in SRC's sibling directory
Create android.mk under JNI (android.mk is the compilation rule file used when the Android OS compiles )
the format of the following android.mk file can be from F:\sdk\adt-bundle-windows-x86-20130917_2\android-ndk-r9\docs\ android-mk.html View
#调用系统工具链, the current function is to invoke the current directory file Local_path: = $ (call My-dir) #初始化工具链, and clear the environment variable, and do not have Local_path cleared to include $ (clear_vars) #LOCAL_ MODULE corresponds to the name of the library, preceded by the omission of Lib; So, and cannot add, plus will error local_module: = <span style= "color: #ff0000;" >atguigu</span> #LOCAL_SRC_FILES引用源文件, if there are multiple source files to do, use the space link local_src_files: =<span style= "color: #ff0000;" > hello.c</span> #配置动态链接库BUILD_SHARED_LIBRARY, generating. So files, smaller files # General use dynamic link Library # To configure the static link library build_static_library, Generate. A files, larger files include $ (build_shared_library)
6. configuration file and Entry codeactivity_main.xml (only one button)
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity "> <button android:onclick=" JAVACALLC " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "Java call C code"/></relativelayout>
Mainactivity.java
Package Com.atguigu.jnihellodemo;import Android.app.activity;import Android.os.bundle;import android.util.Log; Import Android.view.view;public class Mainactivity extends Activity {//Load library and Local_module: = Atguigu consistent system.loadlibrary ("Atguigu");} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);} Click event-java to call C-language public void Javacallc (view view) {String result = Say_111hello (); LOG.E ("TAG", "result===" +result); /** * Native method, method has no parameters, also need to pass parameter in c file * Call C code, return string type from C code */public native string Say_111hello ();}
6. Operation Command Line
At the root of the project, shift+ right-click into the command line, Ndk-build generates Armeabi folder in Libs, and generates libatguigu.so in its subdirectories
In the SRC sibling directory, shift+ right-click into the command line, Javah com.atguigu.javacallc.MainActivity (mainactivity full path), Generate Javah Com.atguigu.javacallc.mainactivity.h file and drag it into the JNI directory
7. Create a. c file
Com
#include <stdio.h> #include <stdlib.h> #include "<span style=" color: #ff0000; " >com_atguigu_jnihellodemo_MainActivity.h</span> "/** * Path: Java_ Full class name _ (Turn. _) Method name * because: typedef const struct Jninativeinterface* jnienv; Java and C Communication environment variable structure is a first-class pointer * jnienv* env parameter: const struct jninativeinterface** is a two-level pointer * Jobject Jobj: Who called this method, is who; Current is Mainactivity.this */jstring Java_com_atguigu_jnihellodemo_mainactivity_say_1111hello (JNIEnv* env,jobject jobj) { /* struct is a first-level pointer (typedef const struct JNINATIVEINTERFACE* jnienv;) * struct method: jstring (*newstringutf) (jnienv*, const char* ); */char* Text = "I am from C"; return (*env)->newstringutf (Env,text);//return (**env). Newstringutf (Env,text);//return (**env). Newstringutf (env, "I am from C");}
—————————————— below to see the simple process configuration development environment ——————————
1. Locate the NDK address
WINDOW--PREFERENCES--NDK Location--f:\sdk\adt-bundle-windows-x86-20130917_2\android-ndk-r9 (This is my NDK address)
2, right click on the project--android Tools--add Native Support (i named Jnisimple), will be generated libs under armeabi\libjnisimple.so
3, feel disorderly feeling, first put down the project configuration file it
Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <button android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:onclick=" JAVACALLC " android:text=" Java off C "/></relativelayout>
Mainactivity.java
Package Com.example.jnisimple;import Android.os.bundle;import Android.app.activity;import android.view.Menu;import Android.view.view;import Android.widget.toast;public class Mainactivity extends Activity {{system.loadlibrary (" Jnisimple ");} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);} public void Javacallc (view view) {String result = SayHello (); Toast.maketext (This, "result==" +result, 1). Show (); Public native String SayHello ();}
4. Create Jni folder under src Sibling directory
Create android.mk under the Jni folder
Local_path: = $ (call My-dir) include $ (clear_vars) local_module : = jnisimplelocal_src_files: = Jnisimple.cinclude $ ( Build_shared_library)
5. Generate header File
Click on src to enter the file directory (the root of src), then right-shift+ into the command line, enter Javah com.example.jnisimple.MainActivity, and drag it into the JNI directory
6. Create jnisimple.c
#include "com_example_jnisimple_mainactivity.h" #include <stdio.h> #include <stdlib.h>jniexport jstring Jnicall Java_com_example_jnisimple_mainactivity_sayhello (jnienv * env, Jobject jobj) {return (*ENV) Newstringutf (env, "I am from Jnisimple");}
Description The method name of the. c file, which can be copied directly from the Com_example_jnisimple_mainactivity.h
7. Related source code
Right-click--properties--c\c++ general--paths and symbols--add--f:\sdk\adt-bundle-windows-x86-20130917_2\ on the project Android-ndk-r9\platforms\android-9\arch-arm\usr\include
(This is my own address directory, everyone's main reference to which level directory can be located)
In-depth analysis of NDK configuration Environment vs easy-to-configure JNI process