"Turn" 10 minutes to build the NDK's Android development environment

Source: Internet
Author: User

Original URL: http://blog.csdn.net/u012176591/article/details/23018913

Jinliang ([email protected]) CSDN Blog: http://blog.csdn.net/u012176591

1. Install the JDK.

1.1 Go to official website http://www.oracle.com/technetwork/java/javase/downloads/index.html Download JDK7 software package, I download is jdk-7u51-windows-i586.exe.

1.2 Double-click Install Jdk-7u51-windows-i586.exe, the installation directory of JDK and JRE is selected by default.

1.3 Configuring the JDK environment variables

See the description of Baidu experience http://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html.

2. Go to official website http://www.eclipse.org/downloads/download eclipse, I download the eclipse-java-kepler-sr2-win32.zip, unzip can use.

3. Build an Android development environment

3.1 Installing the Android SDK

3.1.1 to website http://developer.android.com/sdk/ index.html download Adt-bundle-windows-x86-20140321.zip. First talk about this adt-bundle-windows-x86-20140321, in fact, it can be directly used to do Android application development, but the eclipse tools inside Not good, so I only use the SDK package inside.

3.1.2 will download the Adt-bundle-windows-x86-20140321.zip decompression, we want to use the SDK package, the SDK package to the directory you want to use, after the results of the decompression shown in Figure I.

(Figure I)

3.2 Install Eclipse's ADT plugin (Android development Tools).
3.2.1 Start Eclipse, select the Help->install New software for the menu item
3.2.2 Click Add, enter a name, and then enter it in the URL:
http://dl-ssl.google.com/android/eclipse/,ok!
3.2.3 Select the tool that needs to be updated, finish, restart Eclipse when the installation is complete.

3.3 Install Eclipse's NDK plugin (Android Native development tools), this plugin package has C language related tools, installation method with 3.2.
3.4 Configuring the ADT plug-in
3.4.1 Start Eclipse, select the menu item, window, and Preferences
3.4.2 Select Android, select the SDK installation directory in the SDK location, ok!

3.5 Creating a virtual machine

3.5.1 Choose to create an Android analog device. Select the menu item's window->AVD manager-> new, create a new analog device, and choose the plugin's own phone simulator configuration, as shown in step two:

First step: AVD Name: Simulator named, no specific name required.

The second step: device: In the drop-down list to select the phone's value, you can see that some parameters have been configured, do not need us to set.

Part III: Skin: Select Skins in the drop-down list.

Fourth step (optional): Set the memory size

After a few steps have been completed, click Confirm and a phone simulator is built.

(Figure II)

4. Installing the NDK

4.1 Go to official website http://developer.android.com/tools/sdk/ndk/ index.html Download the latest NDK Development Kit, the version must be R7 above, such as 32-bit Windows downloadable android-ndk-r9d-windows-x86.zip.

4.2 Unzip the downloaded Android-ndk-r9d-windows-x86.zip and put it in the directory you want.

Note: Prior to R7, you must install Cygwin to use the NDK. Starting with R7, the Windows version of the NDK provides a ndk-build.cmd script that can be compiled directly with this script. To do this, you can implement Eclipse's auto-compiling NDK, as long as you add a builder to the Eclipse Android project, which is described in a later step.

5. Implementation of NDK instances
5.1 Start Eclipse, create a new Android project (named Testndk here), as shown in three:


(Figure III)
5.2 Create a new folder Jni under the root of the TESTNDK project, then locate the two files Android.mk and HELLO-JNI.C under the NDK's installation directory (\SAMPLES\HELLO-JNI\JNI) and copy them to testndk-> Under the Jni folder.

Note: If you are curious about the HELLO-JNI.C function name in the JNI folder is so tricky, how it is written, and if you think farther, already thinking about how to write this kind of file later, then I tell you that the function name inside it is directly paste the contents of a header file. This header file can be obtained by deserializing the Hellojni.java mentioned below. If you want to do NDK development in the future, you must master the anti-compilation method. In order to you can less detours, recommended my other article, "1 minutes to conquer the NDK development Javah can not decompile the problem", link URL http://blog.csdn.net/golden1314521/article/details/23039417.
5.3 Locate the Hellojni.java file under the NDK's installation directory (\SAMPLES\HELLO-JNI\SRC\COM\EXAMPLE\HELLOJNI) and copy it to the package corresponding to the TESTNDK Project Src folder.
5.4 Modify the project file Androidmanifest.xml, the Activity tab under Android:name = ". Testndkactivity "modified to Android:name =". Hellojni ". (Note: the
Testndkactivity are automatically generated for the project, different projects, and here are different)
5.5 Create and configure a builder
5.5.1 Click project->properties->builders->new to create a new builder. Click program,ok! on the dialog box that pops up Four of the following:


(Figure IV)
5.5.2 in the popup dialog "Edit Configuration", configure Tab "Main":
The path in the location where the nkd-build.cmd is required (under the NDK installation directory).
Working diretcoty need to fill in the testndk of the project root directory.
Five is shown below:

(Figure V)
5.5.3 Configuration Tab "Refresh", shown in six:
Tick "Refresh Resources upon completion",
Tick "The entire workspace",
Tick "recuresively include sub-folders".

(Figure VI)
The 5.5.4 Configuration tab, "Build Options", is shown in seven:
Tick "After a" and "clean",
Tick "During Manual builds",
Tick "During Auto Builds",
Tick "Specify working set of relevant resources".
Click "Specify Resources ..." to check the "JNI" directory of the TESTNDK project, finish!

Save Settings, ok!

(Fig. Seven)

5.5.5 project->clean-> Select Project, click OK. When you see the information shown in eight in the console, it means that everything is OK, and it is already automatically compiled code.

(Fig. Eight)

If you are using an example from the NDK-R9 version, you will see more compilation information in the console, as follows

(Fig. Eight)

At this time you have to check the Libs directory under the project root directory, to delete the extra subfolders in the Libs, leaving only the Armeabi folder and jar-suffix jar package.

If you are a novice, to avoid unnecessary hassles, please select the R7 version (Android-ndk-r7-windows.zip) when installing the NDK.


                  
5.5.6 Right click on the project Testndk,run as---Android application, run the program. When the information shown in Figure Nine appears, the NDK instance runs successfully!

(Fig. Nine)

If the program is running in error, display the "Application * * (process: com.example.***) stop unexpectedly, retry", as shown in (10)

(Fig. 10)

The cause of this error is most likely the name of your JNI file or the statement of the package was incorrectly written. Check the program running log logcat, the error is "Java.lang.UnsatisfiedLinkError:stringFromJNI", said the Java layer called the local method Stringfromjni, but did not find this local method, So apart from the wrong.

So how does an Android virtual machine find this local method? Let's talk about the Android Project environment: The package name is COM.EXAMPLE.TESTNDK, the application name is Hellojni, if the Java layer calls the local method string Stringfromjni (), Then the Android virtual machine will be generated by these three variables a name: jstring java_com_example_testndk_hellojni_stringfromjni (jnienv* env, Jobject thiz), A local method with the same name is found strictly in the local method. If it is not found, the error is returned. So verify that the name of the Jni file or the package declaration is consistent with the Java file.

After correcting this error in a local file (i.e. a C or C + + file), it runs and displays normally.

"Turn" 10 minutes to build the NDK's Android development environment

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.