Start of building the Android NDK environment NDK HelloWorld

Source: Internet
Author: User

Start of building the Android NDK environment NDK HelloWorld

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 above 20, you can install the NDK plug-in. In addition, the NDK integrates the CDT plug-in (this article describes the ADT version above 20)

Download the latest NDK from the Android official website. Note: Cygwin is integrated with NDK Versions later than r7 and is still very lite. It is much easier than downloading Cygwin (Cygwin had to be installed and configured separately before and has been integrated now )!
 
After the download is complete, decompress the package.

Shows the directory structure after decompression:

Open Eclipse, click Window-> Preferences-> Android-> NDK, set the NDK path, for example, my is: D: Androidandroid-ndk_r10-windows

Create an Android project, right-click the project and choose Android Tools> Add Native Support... And then give our. so file a name. For example, my name is XS_NDK_01.

At this time, the project will have another jni folder, which contains the Android. mk and XS_NDK_01.cpp files. Android. mk is the Makefile of the ndk project, and XS_NDK_01.cpp is the source file of the NDK.
(About Android. mk instructions can refer to: http://www.cnblogs.com/luxiaofeng54/archive/2011/08/13/2137577.html)

The JNI interface naming convention is: Java _ + the package name that calls this method (the point of the package name is) ++ The class name + _ + method name that calls this interface. For instance methods, two parameters are required. a jni environment pointer JNIEnv *, the other is the Java instance jobject that calls this method.

Next, edit the XS_NDK_01.cpp file and write a HelloWorld demo. The Code is as follows:

# Include
  
   
# Include
   
    
Extern C {jstring encode (JNIEnv * env, jobject thiz) {// return a string to the Java layer return env-> NewStringUTF (small ticket JNI HellWorld program .);}}
   
  

Finished, and then 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? How can this problem be solved?
Eclipse-> Window-> Preperences-> C/C ++-> Build-> Environment add an Environment variable NDKROOT, for example:

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, such:

Then create an Activity to call the HelloWorld we just compiled. The Code is as follows:

Package com. shanhy. example. ndk01; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button;/*** HelloWorld NDK development *** @ author SHANHY (365384722@QQ.COM) * @ date November 19, 2015 */public class MainActivity extends Activity {// LOCAL_MODULE in your mk configuration file: = XS_NDK_01 private static final String libSoName = XS_NDK_01; private Context mContext = null; private Button btnClick = null; private String mStrMSG = null;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mContext = this; btnClick = (Button) findViewById (R. id. btn_click); btnClick. setOnClickListener (new OnClickListener () {public void onClick (View v) {// call the JNI layer implementation method mStrMSG = getStringFromJNI (); if (mStrMSG = null) {mStrMSG = failed to call JNI;} LogUtils. toastMessage (mContext, mStrMSG) ;}}) ;}/ *** declare local method ** this method is a native method. implemented by C ** @ return JNI information */public native String getStringFromJNI ();/*** load the so library file generated by JNI */static {System. loadLibrary (libSoName );}}

So far, we have completed a simple example. If you need to further develop the NDK, please refer to the relevant materials.

============================================

The following solutions may cause errors ):
1. Method 'newstringutf' cocould not be resolved (the Method "NewStringUTF" cannot be resolved)
Cause:

I had this issue. Based on my solution, it seems to be something funny going on in Eclipse, since I had another project open with (as far as I was able to tell) the exact some properties, paths, etc., besides for being labeled a Library Project.Just by observing the corresponding struct in jhi.h, the callback prototypes are all there! Ctrl-click the include statement and Eclipse will even link you the reference!Go to the project's Properties -> C/C++ General -> Code Analysis. Click the Use project settings radio button (or Configure Workspace Settings... button). Disable (uncheck) the Method cannot be resolved checkbox. Click Apply, OK. Then for your project, refresh, clean, refresh, build.There must have been something I did differently in creating the new project. Or maybe it was because of the locations of the projects, or the fact that the previous was a Library. Maybe it really is an Eclipse bug? For reference, I'm using ADT v21.1.0-569685 and NDK r8e for Windows。

Right-click the project-> Properties-> C/C ++ General-> Code Analysis, and select Method cannot be resolved in "Use project settings or Use workspace settings" (The Method cannot be parsed) deselect select, apply-> Save, And then refresh, clean, refresh, and build the project.

2. jni/hellocpp/main. cpp: 16: 18: error: base operand of '-> 'has non-pointer type' JNIEnv {aka _ JNIEnv }'
This error is returned when android java calls the c ++ program.
File XS_NDK_01.cpp:

# Include
  
   
# Include
   
    
Encode (JNIEnv * env, jobject thiz) {// return a string to the Java layer return (* env)-> NewStringUTF (env, HelloWorld from JNI !!); // Return env-> NewStringUTF (JNI HellWorld program for small orders .);}
   
  

The error is:
I started with (* env)-> NewStringUTF (env, "small ticket JNI HellWorld program. "); this is written in c (the file should be. c extension), while my cpp Program (. cpp extension), which must be rewritten:

Env-> NewStringUTF ("small ticket JNI HellWorld program .");

3. java. lang. UnsatisfiedLinkError: Native method not found: com. shanhy. example. ndk01.MainActivity. getStringFromJNI :() Ljava/lang/String; Error
It may be:
1. In c ++, the Java initials in Java_xxx_xxx must be capitalized.
2. If yes. the cpp file uses extern "C" {"Your method here"}, and the braces are your local method (as in my example above ).

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.