First, the construction of the environment:
1. Required Tools:
A, Cygwin (using Linux systems under Windows) http://www.cygwin.com
B, Android-ndk-1.6_r1-windows (mainly the role of compiling C or C + + into a dynamic library) Http://dl.google.com/android/ndk/android-ndk-1.6_r1-windows.zip
C, Eclipse (build Android engineering software, the use of Android children's shoes should have been installed, in this do not repeat).
2, installation Cygwin
A, download cygwin software from the URL specified above, click Cygwin.exe to install.
B, after installing the software to run, usually use the default, always click on the next step is good.
The above figure has three options, the general default is good, the second is that only download does not install, download requires manual installation, and the third is that after downloading to the local installation;
This refers to the installation path of the software, generally do not need to replace.
This path refers to where the downloaded content is stored, which can be specified by itself.
Let it download automatically and then click Next.
This is the need to choose the address of the download, before the choice of n times, after the installation has failed, if you want to select a few more, you can press and hold "Ctrl" to choose, do not recommend multiple selection, multiple selection will create more than one folder
If the failure starts again, select the new address and install it repeatedly to succeed.
Let it download automatically and then click Next.
This step is to choose what needs to be installed, generally like the GNU GCC g++ and other commonly used tools need to install, of course, can also be installed, but all installation time will be very long.
After the final download is completed, it will be installed automatically, click Finish.
3. Verify that the Cygwin installation is successful.
Run the installed Cygwin software, enter the Gcc-v make-v g++-v command, see if you can get its version information, such as can not be obtained, some tools are not installed, need to reinstall
Tools that are not installed (the same way as before, just select the tools you don't have installed).
At this point the Cygwin software is installed, and if you later use the new command, if you are unable to use it, you will need to reinstall the tools that are not installed.
4. Installing the NDK
A, according to the address given above, download the NDK tool software.
b, the tool software to extract to a path (try not to be Chinese path, because in the Cygwin environment, the use of Chinese is not convenient) E:\androidNDK\android-ndk-1.6_r1-windows\android-ndk-1.6_r1
C, open the Cygwin software, first enter the root directory, in the use of CD cygdrive/e/androidndk/android-ndk-1.6_r1-windows/android-ndk-1.6_r1/into the directory
D, enter build/host-setup.sh if the following information appears to indicate that the installation was successful.
5, the environment is set up, of course, is to write a JNI interface how to call the. so file. Steps are as follows
A, e/androidndk/android-ndk-1.6_r1-windows/android-ndk-1.6_r1/directory has an app directory, specifically for the NDK project, so we
Specify the location as E/androidndk/android-ndk-1.6_r1-windows/android-ndk-1.6_r1/app/first_jni when you create an Android project
Create a JNI interface class Jin.java, which reads as follows:
Package example.dialog.test;
public class Jin {
public native int add ();
Public native String getString ();
}
B, run the DOS command window, enter the above file directory, use Javac Jin.java generate Jin.class class
C, run the JAVAH-JNI package name +jin.class command, and finally generate the package name +jin.h
D. Create the JNI directory under the Android project directory and copy the previously generated package name +jin.h file to this directory, and then new. c file in this directory to implement the function in package name +jin.h.
The implementation is as follows:
#include <stdio.h>
#include <stdlib.h>
#include "Example_dialog_test_jin.h"
int Add ()
{
int x, y;
x=100;
y=100;
return x*y;
}
Jniexport jint jnicall java_example_dialog_test_jin_add (jnienv *env,jobject thiz) {
return add ();
}
Jniexport jstring jnicall java_example_dialog_test_jin_getstring (jnienv *env,jobject thiz) {
(*env)->newstringutf (env, "Hello World in C!!!");
}
E, JNI implements calls to the C\c++ method by invoking the dynamic link library (under Windows, Dll,linux), so now we need to write the
C code compiled into so file.
You need to compile the files again from: Android.mk and application.mk. The storage directory for these two files is fastidious, the former exists with a C file in a directory, the latter
exists under the Android engineering root directory.
The contents of the Android.mk file can usually be copied to other projects, as follows:
Local_path: = $ (call My-dir)//local_path indicates that the root directory of the project directory is at this time, (call My-dir) returns the current directory location.
Include $ (clear_vars)
Local_module: = NEWJNI//used to distinguish each module in the ANDROID.MK, the file name must be unique, cannot have a space, this is the file name generated by your library file, the compiler will automatically give you the prefix lib and suffix.
Local_src_files: = EXAMPLE_DIALOG_TEST_JIN.C//c file at compile time
Include $ (build_shared_library)//indicates build dynamic library
Content in Application.mk:
App_project_path: = $ (call My-dir)/firstjni-test
App_modules: = Newjni //values are the same as the values in Local_module in Android.mk.
Finally, launch Cygwin into the NDK directory, enter make App=frist_jin, and the following interface appears to indicate the success of the compilation. That's the E:\androidNDK\android-ndk-1.6_r1-windows\android-ndk-1.6_r1\apps\first-jni\firstjni-test\libs\armeabi. The libnewjni.so dynamic library is generated under this directory.
F, then we will use the library in the Android app, create a new Myjni.java class under the above project, with the following content:
Package example.dialog.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.TextView;
Import Example.dialog.test.Jin;
public class Myjni extends activity{
Private TextView TextView;
static{
System.loadlibrary ("Newjni");
}
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Jin Jni = new Jin ();
int result = Jni.add ();
TextView = (TextView) Findviewbyid (r.id.tv);
Textview.settext ("add () =" +result+ "\ngetstring () =" +jni.getstring () ");
}
}
Finally run the application, as shown in the following figure:
The entire project directory is as follows:
By this point, the entire process is over, and being able to do so also means that you are getting started with the JNI interface and the use of NDK tools.
Of course, there will be some problems throughout the process, I have mainly encountered the following issues:
1, the installation of Cygwin software, the use of build/host-setup.h when the GNU can not be used, and later found out that the reason is not to download the GNU in the installation of Cygwin and later use of make, re-download is good.
2, when using the JAVAH-JNI package name + class name (and do not need to add the. class suffix), the package name + class name can not be found, because the hierarchy of the directory is not correct, you should use the command in the parent directory of the package name.
3. Use the Make App= project folder (instead of generating the so file name).
4, in the operation of the mold, has been an error, said the quoted so file content is empty, and later query to the reason is because the simulator is Inter atom (x86), and. So files belong to the ARM series, replaced successfully.