Android---NDK development HelloWorld (JNI)

Source: Internet
Author: User

1) Open the Android Developer's website http://developer.android.com/find develop click.


2) Enter and then click Tools


3) Enter after the left to find the NDK click, you can see the NDKr10 of various platforms, select the required download.


5) Unzip the downloaded Android-ndk32-r10-windows-x86_64.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.

6) Implementation of NDK instances

Start Eclipse, create a new Android project (named Testndk here)


7) Create a new folder Jni at the root of the TESTNDK project, then locate the two files Android.mk and HELLO-JNI.C under the NDK's installation directory (SAMPLESHELLO-JNIJNI) 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.


8) Locate the Hellojni.java file under the NDK's installation directory (SAMPLESHELLO-JNISRCCOMEXAMPLEHELLOJNI) and copy it to the package corresponding to the TESTNDK Project Src folder.

9) Modify the project file Androidmanifest.xml, the Activity tab under Android:name = "COM.ZHW.TESTNDK. Mainactivity "modified to Android:name =" Com.example.hellojni.HelloJni ". (Note: the

Testndkactivity are automatically generated for the project, different projects, and here are different)

10) Create and configure a builder

Click Project->properties->builders->new to create a new builder. Click program,ok! on the dialog box that pops up



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. :

Note: Don't miss out.



Configuration Tab "Refresh",:

Tick "Refresh Resources upon completion",

Tick "The entire workspace",

Tick "recuresively include sub-folders".


The 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!




When you see the information shown in eight in the console, it means that everything is OK, and it is already automatically compiled code.

Note: If unsuccessful:project->clean-> Select project, click OK





10) Right click on Project Testndk,run as->android application, run the program. When the information shown in the diagram appears, the NDK instance runs successfully!



"Application * * (process: com.example.***) stops unexpectedly, please retry" error message if the program is running in error

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.


Android---NDK development helloworld (JNI)

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.