Android NDK Development (I): build the new NDK environment (no Cygwin, super fast)

Source: Internet
Author: User

Before using NDK for Android projects, you must download NDK, download and install Cygwin (for simulating Linux environments), and download CDT (Eclipse C/C ++ development plug-in ), you also need to configure the compiler, environment variable...

Shamoo checked the information on the Internet and found a super fast way to configure NDK.

Step 1: Download the Android Development Tool ADT (short for Android Development Tool) from the Android official website. This Tool integrates the latest ADT and NDK plug-ins, Eclipse, and the latest SDK version. Decompress the package and you can use it. It's great!

ADT Plugin: used to manage the Android SDK and related development tools

NDK plug-in: used to develop Android NDK plug-ins. If the ADT version is later than 20, you can install the NDK plug-in. In addition, the NDK integrates the CDT plug-in.

Can also be online update ADT, NDK plug-in, but the speed is super slow... so decisive online download integrated development tools ADT, download link see:

Step 2: Go to the Android official website to download the latest NDK. Note: After r7 or above, the NDK version integrates Cygwin, which is also a lite version. It is much easier than downloading Cygwin! See: for download links

After the download is complete, decompress the package!

Step 3: Open Eclipse, click Window-> Preferences-> Android-> NDK, set the NDK path, for example, Shamoo is E: \ android-ndk-r9c

Step 4: Create an Android project, right-click Android Tools> Add Native Support... on the project, and name the. so file, for example, my-ndk.

At this time, the project will be more than a jni folder, jni has Android. mk and my-ndk.cpp files. Android. mk is the Makefile of the ndk project, and the my-ndk.cpp is the source file of the NDK.

Step 5: copy the demo of NDK and write it in the Hello-JNI project. Use Alt + '/' to prompt the code! Great! Do you have? When I used CDT, I was not prompted by the code. I was depressed...

The JNI interface naming convention is: Java _ + package name that calls this method (the package name is replaced by _) + _ + class name that calls this interface + _ + method name, two parameters are required for the instance method. One is the JNI environment pointer JNIEnv *, and the other is the Java instance jobject that calls the method.


  JNIEXPORT jstring JNICALL Java_com_shamoo_activity_TestActivity_stringFromJNI(JNIEnv *env,jobject thiz) {return env->NewStringUTF("Hello jni");}

TestActivity. java:

Public class TestActivity extends Activity {static {System. loadLibrary ("my-ndk");} // declare the native method of the jni layer and use the native keyword public native String stringFromJNI (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); TextView tvText = new TextView (this); tvText. setText (stringFromJNI (); setContentView (tvText );}}

To use the ndk library, you must use System. loadLaibrary to load the. so library in the static code block.

Step 6: complete and run. Compile the NDK before running it, And then compile the JAVA code. Compilation may encounter Unable to launch cygpath. Is Cygwin on the path? The solution is as follows:

1. Right-click the project, click Properties-> C/C ++ Build Building Settings to remove the Use default build command, and then enter $ {NDKROOT}/ndk-build.cmd

2. Click Environment in C/C ++ Build, and click Add... to Add the Environment variable NDKROOT, the root directory with the value of NDK

3. Compile again to solve the problem!

The runtime crashes and java. lang. UnsatisfiedLinkError: stringFromJNI error occurs. Solution: add the extern "C" modifier before function definition in the C ++ file.

extern "C" {JNIEXPORT jstring JNICALL Java_com_shamoo_activity_TestActivity_stringFromJNI(JNIEnv *env,jobject thiz);}JNIEXPORT jstring JNICALL Java_com_shamoo_activity_TestActivity_stringFromJNI(JNIEnv *env,jobject thiz) {return env->NewStringUTF("Hello jni");}

The reason is: with extern "C" modifier, the compiler will compile and connect in C language. In C language, the function name after compilation is different from that after C ++ function compilation. For example, foo (int x, int y) and C may be compiled into the name of _ foo, C ++, because it supports overload, will compile function names with parameters such as _ foo_int_int. If the function is compiled in C language, if the function name of _ foo cannot be found when the foo function is called, an error is returned. Therefore, you must add the extern "C" modifier.

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