Android NDK Environment build + test routines

Source: Internet
Author: User

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>                 <action android:name= "Android.intent.action.MAIN"/>                <category android: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:

Package com.example.testndk; Import Android.app.activity;import Android.widget.textview;import android.os.Bundle; public class Testndk extends activity{    @Override public     void OnCreate (Bundle savedinstancestate)     {         Super.oncreate (savedinstancestate);         TextView  Mytextview = new TextView (this);         Mytextview.settext (STRINGTESTNDK ());         Setcontentview (Mytextview);     }      Public native String stringtestndk ();     Public native String 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.h>//public native string Stringtestndk (); The C signature of the method, in the format Java_ package Name _ Class Name _ Method name Jniexport jstring jnicall java_com_example_testndk_testndk_stringtestndk (jnienv *env, Jobject this) {     return (*env)->newstringutf (env, "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 $ (buil D_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.    only one method in a class is implemented, but a C file can contain many different export functions, so the implementation code of the method signature using Javah can be placed in a C file.                  (Actually, the method signature can also be written by itself , 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.   about how to write the C program, you can refer to the tutorial "JNI full Manual" written in more detail.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.