Android ndk development, androidndk
Preparations
1. Download and install Android NDK
Address: http://developer.android.com/sdk/ndk/index.html
Configure NDK environment variables, such:
NDK_HOME C: \ android-ndk-r9
PATH % NDK_HOME %
2. Download and install cygwin
On the windows platform, you must first build a linux environment. cygwin is a unix simulation environment running on the windows platform, or porting applications from unix to windows is very useful. With this feature, you can use NDK to compile C and C ++ code without installing linux.
: Http://www.cygwin.com
When selecting Select Packages during installation, you only need to Select
Devel: Install
Shells: Install
Run cygwin. In the displayed command line window, enter the cygcheck-c cygwin command to print the current cygwin version and running status. If the status is OK, cygwin runs normally.
Create a sample project
// MainActivity. javapublic class MainActivity extends Activity {// 1. defining a c method interface is equivalent to defining an interface private native String helloworldfromC () in java code; // 5. introduce the function library static {System. loadLibrary ("hello"); // Note: remove the preceding 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 the project
Add C files and android. mk files to 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 encode (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 the mk file // android. mk tells the compiler how to package c code into a function library // 3. package c code into a function library}
Android. mk is as follows:
LOCAL_PATH: = $ (call my-dir) include $ (CLEAR_VARS) # package it into the name of the function library LOCAL_MODULE: = hello # Corresponds to the c code file LOCAL_SRC_FILES: = Hello. cinclude $ (BUILD_SHARED_LIBRARY)
Compile c code
Cd project directory/
Ndk-build
If no error occurs
Note: The ndk project can only run on arm architecture simulators.
The naming rules for the numbers in the C file, such:
Jstring Java_com_android_helloworldfromc_MainActivity_helloworldfromC
Return Value c method interface defined by Java _ package name_classname _
You can also use javah to automatically generate the header file without writing the method name.
When the jdk version is 1.6
Cd project directory \ bin \ classes
Javah-classpath. com. android. helloworldfromc. MainActivity
Note: The path must be specified as the current directory. Otherwise, the class cannot be found.
Because it finds your class file through the relative path, as shown above, the relative path is:
. \ Com \ anroid \ hellowordfromc \ MainActivity. java
So you can also write
Cd project directory \ 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 project directory \ src
Javah com. android. helloworldfromc. MainActivity
Common jni errors
1 error 1 missing method parameters
2 error 2 E/AndroidRuntime (821): java. lang. UnsatisfiedLinkError: Native method not found: com. example. error. DemoActivity. helloWorld :() Ljava/lang/String;
3 error 3 generally no log printing direct Error Reporting Project STOP General c code running error
4 error 4 errors are reported on the tool chain of cross-compilation. c code has a compilation error. For example, some functions do not declare some types and do not declare less symbols.
5 Error 5 No Android. mk File
6 Error 6 Android. mk file Error
7 Error 7 reference to the other. so function library requires that the package name of the class corresponding to your native method be the same as the package name packaged into the. so function library.
Download Sample project