The NDK development steps for Android

Source: Internet
Author: User

A super quick config NDK approach: (NDK DEMO)

The first step:

Download the Android Developer ADT (abbreviation for Android development tool) to the Android website, which integrates the latest ADT and NDK plugins, as well as eclipse, with the latest version of the SDK. It can be used after decompression.

ADT Plugins: Managing the Android SDK and related development tools

NDK plugin: For the development of Android NDK plug-ins, ADT version of more than 20, you can install the NDK plug-ins, in addition to the NDK integrated CDT plugin. ADT and NDK plugins can also be updated online

Step Two:

To download the latest NDK on the Android website, note: The NDK version integrates Cygwin after R7 and is a very lite version. More convenient than downloading Cygwin! Download Link interview: http://developer.android.com/tools/sdk/ndk/index.html

Step Three:

Open Eclipse, point window->preferences->android->ndk, set the NDK path, for example e:/android-ndk-r9c

Fourth Step:

Create a new Android project, right-click on the Android Tools->add Native support ..., and then give us a name for the. So file, for example: MY-NDK

At this time the project will be more than one Jni folder, JNI under the Android.mk and My-ndk.cpp files. Android.mk is the makefile,my-ndk.cpp of the NDK project, which is the source file of the NDK.

Fifth Step:

Then write about the NDK Demo,hello-jni project. Use ALT + '/' to code hints

The naming specification for the JNI interface is: Java_ + Call the method's package name (the point of the package name with _ instead) + _ + Call the interface's class name + _ + method name, for instance method, there are two parameters is necessary, a JNI environment pointer jnienv *, and the other is to call the method Java instance Jobje Ct

My-ndk.cpp:

#include 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");} Declares the native method of the JNI layer, uses 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);}}


The NDK library must be loaded with System.loadlaibrary in the static code block. So library

Sixth step:

Finished, and then run. Compile the NDK before running, and then compile the Java code. Compilation may encounter unable to launch Cygpath. is Cygwin on the path? Error, the solution is as follows:

1. Project Right-click properties->c/c++ Build Building Settings Remove the Use default Build command and enter ${ndkroot}/ndk-build.cmd

2. Click on environment in the C + + build, click Add ... Add environment variable ndkroot, the value of the NDK root directory

3. Re-compile, the problem is solved!

Run-time crash, encountered Java.lang.UnsatisfiedLinkError:stringFromJNI error, Workaround: add extern "C" modifier before function definition in 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 that with extern "C" adornments, the compiler compiles and connects in C language. In the C language,After the function is compiled, the function name differs from the C + + function, such as foo (int x, int y), C may be compiled into a _foo name, and C + + because it supports overloading, it compiles the _foo_int_int function name with the parameter. If you are compiling the C language, calling the Foo function will not find the function name of _foo and the function name cannot be found. So add the extern "C" modifier.

Reference article:

http://blog.csdn.net/pi9nc/article/details/28855737
Http://www.2cto.com/kf/201403/288157.html
Http://www.th7.cn/Program/Android/201404/189197.shtml

The NDK development steps for Android

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.