Android NDK environment setup + test routine

Source: Internet
Author: User

I am too tired to talk about a lot of concepts. If you don't understand the ADT and NDK concepts, how can you find them here? Therefore, you only need to follow the steps below to build the NDK environment.

Step: (if you have not installed any related development tools, skip this step if you have already installed them)

1. Environment Construction

1. Install jdk
Configure environment variables after installation (I installed JDK)
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 to check whether the installation is successful.

2. Install eclipse and adt
The official Android website has integrated adt into eclipse, and the online installation of adt is terrible.
Download the integration package on the android official website, I download is adt-bundle-windows-x86_64-20140624.zip
I shared these two files on Baidu cloud disk can download: http://pan.baidu.com/s/1kTA4vn5
Decompress the package to 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, which can be used to download Development kits of other versions. Access to sdks in China is often rejected. Bypass methods can be searched online.

3. Install NDK
Download and decompress the NDK package on the android website. Note that the NDK directory must not contain spaces. We recommend that you decompress the directory to D: \ Android.
The version I downloaded is android-ndk-r9d-windows-x86_64.zip
After r7, you can directly use the ndk-build command to complete the compilation, so you do not have to install the cygwin environment.
Configure the environment variable path:
Add the NDK installation directory in path, such as: D: \ Android \ android-ndk-r9d

After cmd is enabled, enter ndk-build to display the following prompt.

    
 
Instead of finding the ndk-build command!

 

Ii. TestNDK

1. Create a new Android project: TestNDK
The default package prefix of the wizard when creating an application is com. example, which does not need to be changed. So the final package name is com. example. testndk.
Create a startup Activity called TestNDK (if you want to call MainActivity, remember to change it later)
Ps: When I create an Activity, the result of the Activity creation does not respond. Only one java file can be created. Remember to add the following under the application tag in AndroidManifest. xml:

 <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 using the wizard or directly creating a java file, use the following code to overwrite the TestNDK. java file:

 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 using eclipse to generate the related. class file, because the dynamic link library has not yet been created and will crash even if it is installed on your mobile phone.
I don't know if it is a version issue. The tutorials I found are messy and do not correspond to my actual situation.
On my computer, eclipse stores the generated class files in bin \ classes (compared with the project directory, all subsequent operations are based on the root directory)
The program that generates the. h header file is javah (if you do not know about it, you can search for relevant information to see it)
Start the command prompt in the root directory (or open the command prompt to switch to the root directory, depending on whether the command prompt is opened in your right-click menu ).
Run the following command:
Javah-classpath bin \ classes; D: \ Android \ sdk \ platforms \ android-20 \ android. jar-d jni com. example. testndk. TestNDK
The-classpath contains two directories, one of which is the bin \ classes mentioned above, which can be changed according to the actual situation.
The second is the jar package of Android. If it is not added, an error will be reported. If you install the sdk in another directory, make the corresponding changes. If the directory contains spaces, enclose the strings.
-D specifies the directory to be generated. The header file to be generated will be stored in the jni directory under the project directory.

After the preceding command is executed, a com_example_testndk_TestNDK.h file is added to the jni directory. You do not need to worry about the content for the moment.

 

3. compile and generate the so file
Create two files under the jni directory
The first one is testNDK. c.

# Include <jni. h> # include <string. h> // public native String stringTestNdk (); Method c signature, in the format of Java _ package name_classname_method name JNIEXPORT jstring JNICALL encode (JNIEnv * env, jobject this) {return (* env)-> NewStringUTF (env, "Hello, test NDK! ");}

The second 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, return to the project directory and open the command prompt and execute: ndk-build
After the command is executed, a libtestNDK. so file should be added to the libs \ armeabi directory.

 

4. Install apk
Re-compile and run eclipse, a program will be started without accident, and the display is: Hello, test NDK!
If the program is running, and the Log shows: WARNING: cocould not load library testndk !.
It indicates that the shared library fails to be loaded. If the code is not changed, use Winrar to open the apk file under the bin directory and check whether libtestNDK. so exists in lib/armeabi. If not, open the java bulid path under the property of the android project, add libs/armeabi to the Source path, and run it again.

 

Iii. Description

1. I have defined two native methods in the TestNDK class. The generated header file contains signatures for these two methods, but I only implement the first method.
If you call the second method in the program, the native method cannot be found.
If the shared library is loaded successfully (that is, the custom warning is not displayed)

2. the above only implements one method in a class, but a c file can contain multiple different export functions, therefore, the implementation code of the method signature obtained using javah can be put in a c file. (In fact, you can write the method signature by yourself. You don't have to use javah)
If multiple c files generate one so, such as 1.c and 2.c, you can assign LOCAL_SRC_FILES:
LOCAL_SRC_FILES: = 1.c 2.c

3. For details about how to write c Programs, refer to the JNI full manual.

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.