Lazy nonsense a lot of concepts, about ADT, NDK concept If you do not understand, how can you search here? So you just need to follow the steps below to complete the NDK environment setup.
Step: (Assuming you have not installed any of the relevant development tools, if you have already installed, you can skip)
I. Environmental construction
1. Installing the JDK
Configure environment variables after installation (I installed jdk1.7)
Java_home = C:\Java\jdk1.7.0_51
PATH = C:\Java\jdk1.7.0_51\bin
CLASSPATH =.; %java_home%/lib/dt.jar;%java_home%/lib/tools.jar
You can open cmd and enter: Java-version Check that the installation was successful.
2. Installing Eclipse and ADT
The Androna network has integrated ADT into eclipse, and the process of installing ADT Online was a horrible one.
Download the integration pack on Androna Web, I downloaded Adt-bundle-windows-x86_64-20140624.zip
I share these two files in the Baidu Cloud disk can be downloaded: http://pan.baidu.com/s/1kTA4vn5
After downloading, it is recommended to unzip the D:\Android directory or select another directory.
After the integration package is decompressed, there will be two directories and one file under D:\Android:
Eclipse:eclipse Directory
SDK: Android SDK directory with only 4.4 of packages
SDK Manager.exe:SDK Manager, can be used to download other versions of the development package, domestic access is often denied, bypassing the method can be online search.
3. Installing the NDK
Download the NDK compression Pack and unzip it on the Androna web. Note that the NDK directory must not have spaces, the proposal is still extracted to D:\Android
The version I downloaded is android-ndk-r9d-windows-x86_64.zip
After the R7 version can be compiled directly using the Ndk-build command, you do not have to install the Cygwin environment.
Configure the environment variable path:
Add the NDK installation directory to path, for example: D:\Android\android-ndk-r9d
Ensure that when CMD is turned on, input ndk-build displays the following prompt.
Instead of finding the Ndk-build command!
Two. Testndk
1. Building a new Android project: TESTNDK
The default package prefix is com.example when the wizard creates the app, and there is no need to change it. So the last package name is: COM.EXAMPLE.TESTNDK
Create a start activity, called TESTNDK (not to call mainactivity words, back remember changes)
PS: When I created it, I chose to create the activity results without reacting, only to create a Java file. At this time remember in the androidmanifest.xml inside the application tag add:
<Activity Android:name= "Com.example.testndk.TestNDK" Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" />、
</Intent-filter> </Activity>
Whether you are creating a new Java file with a wizard or directly, then overwrite the Testndk.java file with the following code:
Packagecom.example.testndk; Importandroid.app.Activity;ImportAndroid.widget.TextView;ImportAndroid.os.Bundle; Public classTestndkextendsActivity{@Override Public voidonCreate (Bundle savedinstancestate) { Super. OnCreate (savedinstancestate);TextView Mytextview =NewTextView ( This); Mytextview.settext (STRINGTESTNDK ()); Setcontentview (mytextview); } Public nativeString stringtestndk (); Public nativeString stringTestNdk2 (); Static { Try{System.loadlibrary ("Testndk");}Catch(Unsatisfiedlinkerror ule) { System.err.println ("Warning:could not load library testndk!"); } }}
2. Generate the. h header File
Compile the project with eclipse in order to generate the associated. class file, because the dynamic link library has not yet been created and crashes even when installed on the phone.
Do not know is not the version of the problem, I searched the tutorial inside anyway messy, does not correspond to my actual situation.
On my computer, eclipse stores the generated class file in bin\classes (relative to the project directory, and the operations are all based on the root directory)
The program that generates the. h header file is Javah (do not know the information can be searched for the relevant data to see)
Start the command prompt in the root directory (or open a command prompt to switch to the root directory, depending on if you have a command prompt in the right-click menu).
Enter the following command:
Javah-classpath bin\classes;d:\android\sdk\platforms\android-20\android.jar-d JNI Com.example.testndk.TestNDK
Where-classpath there are two directories, one is said in front of the bin\classes, according to the actual situation change
The second is an Android jar package, if not added will be an error, if you install the SDK in a different directory, make the corresponding changes. If there are spaces inside the directory, enclose the string "".
-d Specifies the generated directory, the header file that will be generated exists in the JNI directory under the project directory.
After completion of the above command, the JNI directory adds a: Com_example_testndk_testndk.h file, the content is temporarily free of control.
3. Compiling and generating so files
Create two files under the JNI directory
The first one is: TESTNDK.C
#include <jni.h> #include <string/ public native String Stringtestndk (); C signature of method, formatted as Java_ Package Name _ Class Name _ Method name this){ return"hello,test NDK! " ); }
The second one is: android.mk
Local_path: = $ (call my-dir)include $ (clear_vars) local_module : = testndk Local_src_files: = testndk.c include $ (build_shared_library)
After saving, go back to the project directory to open a command prompt, execute: ndk-build
After the command is executed, a libtestndk.so file should be added to the Libs\armeabi directory.
4. Install apk
Recompile run eclipse, no accident will start a program that shows: Hello,test NDK!
If the program runs, and log is displayed: Warning:could not load library testndk!.
Description Load Shared library failed, if you determine the code is not changed, use WinRAR open the bin directory under the apk file, see if there is libtestndk.so in Lib/armeabi. If not, open the Java bulid path under the properties of the Android project, add Libs/armeabi to the source path, and re-clean the run.
Three. Related instructions
1. I have defined two native methods in the Testndk class, and the resulting header file has signatures for both methods, but I only implement the first method.
If you call the second method in the program, the native method exception is not found.
If the shared library is loaded successfully (that is, the custom warning is not displayed)
2. The above only implements a method in a class, but a C file can contain a number of different export functions, so the implementation of the method signature using Javah can be placed in a C file. (In fact, the method signature can also write their own, no need to use Javah)
If you have multiple C files that generate a so, such as 1.c, 2.c, you can assign local_src_files to:
Local_src_files: = 1.c 2.c
3. In the end how to write a C program, you can refer to the tutorial "JNI complete Manual" written in more detail.