preparatory work
1. Download install android NDK
Address: http://developer.android.com/sdk/ndk/index.html
Configure the NDK environment variables, such as:
Ndk_home C:\ANDROID-NDK-R9
PATH%ndk_home%
2. Download and install Cygwin
Under the window platform, you need to build a Linux environment, Cygwin is a UNIX simulation environment running on the Windows platform, it is useful for learning unix/linux operating environments, or porting applications from UNIX to Windows. With it, you can use the NDK to compile C, C + + code without installing Linux.
: http://www.cygwin.com
During installation, when selecting Select Packages is selected, only the
Devel:install
Shells:install
Run Cygwin, in the popup command line window input: Cygcheck-c Cygwin command, will print out the current Cygwin version and running state, if the status is OK, then Cygwin run normally.
Create a sample project
Mainactivity.javapublic class Mainactivity extends Activity {//1. Define a C method interface, which is equivalent to defining an interface in Java code private native String HELLOWORLDFROMC ();//5. Introduce library static{system.loadlibrary ("Hello") in Java Code;//Note: Remove the front lib, and the following. so} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main);} public void Click (View view) {Toast.maketext (this, HELLOWORLDFROMC (), Toast.length_short). Show ();}}
Create a JNI folder under Engineering
Add c files and android.mk files inside the folder
Hello.c#include <stdio.h> #include <jni.h>//android-ndk-r9\android-ndk-r9\platforms\android-8\ arch-arm\usr\include//Private native String Helloworldfromc (); Jstring java_com_android_helloworldfromc_ MAINACTIVITY_HELLOWORLDFROMC (jnienv* env, Jobject obj) {//2. Implement C code//Return a Java string//jstring (*NEWSTRINGUTF) ( jnienv*, const char*);//(*ENV) equivalent to jninativeinterface* jnienv//return (**env). Newstringutf (env, "HELLOWORLDFROMC"), Return (*ENV)->newstringutf (env, "HELLOWORLDFROMC");//Generate MK File//Android.mk Tell the compiler how to package C code into a library//3. Package C code as a function library}
android.mk as follows
Local_path: = $ (call My-dir) include $ (clear_vars) # package into function library name Local_module : = hello# corresponding C code file Local_src_files: = Hello.cinclude $ (build_shared_library)
Compiling C code
CD Engineering Catalogue/
Ndk-build
No error occurs, which is through
Run the program note: NDK works only on an arm-architecture emulator
The naming conventions for function names in C files, such as:
Jstring JAVA_COM_ANDROID_HELLOWORLDFROMC_MAINACTIVITY_HELLOWORLDFROMC
Return value Java_ The C method interface defined by the package name _ class name _
It is also possible to automatically generate header files via Javah, so you don't have to write your own method name
When the JDK version is 1.6
CD Engineering Catalogue \bin\classes
Javah-classpath. Com.android.helloworldfromc.MainActivity
Note: It is important to indicate that the path is the current directory, or there will be no class errors found
Because it is by relative path to find your class file, as above, relative path is:
. \com\anroid\hellowordfromc\mainactivity.java
So, you can also write this
CD Project catalog \ Bin
Javah-classpath. \classes com.android.helloworldfromc.MainActivity
The corresponding header file is generated in the bin directory
When the JDK version is 1.7
CD Engineering Catalogue \SRC
Javah com.android.helloworldfromc.MainActivity
Common errors in JNI
1 Error 1 parameters of the Forgotten method
2 Error 2 E/androidruntime (821): Java.lang.UnsatisfiedLinkError:Native Method not found: Com.example.error.DemoActivity.helloWorld: () ljava/lang/string;
3 Error 3 General no log print Direct error project stop General C code has run error
4 error 4 in cross-compiled toolchain escalation error C code has compile errors such as some functions do not declare some types do not declare less symbols
5 Error 5 No android.mk file
6 Error 6 Android.mk file is wrong
7 error 7 referencing others. So library needs your own native method corresponds to the package name of the class and the package name that was previously packaged as. So library
Sample Project Download
The NDK development of Android