In this article, we will talk about how to use NDK to generate the SO library, use the SO library for JNI calls, create files in Android sdcard, and write data.
Tools:
NDK1.5 R1, android SDK1.5 R1, SDCARD, Eclipse, ADT 0.9, Eclipse Galileo for C/C ++, Cygwin 1.5.
There are many tools. I operate on Windows XP. If I operate on Ubuntu or other Linux systems, go to step 2. See the steps below:
1. Install Cygwin
About Cygwin installation please Baidu or google, because of speed reasons, it is recommended that you use the local installation package for download: http://www.gougou.com/search? Search = cygwin & id = 2 download a 38.2 M version.
2. Install NDK
Go to the topic, install NDK, first open cygwin, and then use the cd command to enter your NDK root directory. will not use the linux Command ?? Google go) and then run: build/host-setup.sh
If the preceding information is displayed, the installation is correct. If GCC is found, enter the gcc command to view the information. If the toolchain is not found, please try to modify the first line of code in the build/host-setup.sh file, will :#! /Bin/sh #! /Bin/bash, which needs to be modified on Ubuntu
After the installation, run the make command to compile the built-in program. Enter make APP = hello-jni, and hello-jni as the name of the hello-jni folder under apps, this command will first find the Application under apps \ hello-jni. mk file, find the directory source \ samples \ hello-jni, and find Android. the configuration information in the file mk is compiled. Figure:
Because I have already compiled it, use make APP = hello-jni-B to re-compile it.
The above information indicates that the compilation is successful. The so file is in the apps/hello-jni/project/libs/armeabi folder.
3. Write the java native Interface
Next we enter the actual stage and write code.
Create an android1.5 project. NDK does not support versions earlier than 1.5
JNI. java code:
JniTest. java code:
4. Generate the JNI header file
The JNI header file is generated below. Use the javah command to generate the JNI .. java file!
Run cmd to enter the bin directory of the android project, and then run the javah-classpath.-jni cc. androidos. jni. JNI command. Don't forget the package name! ^_^)
Generate a cc_androidos_jni_JNI.h
Open and check:
Header file declaration in C Language