Android NDK Learning Record (i)

Source: Internet
Author: User

I. NDK environment deployed in Mac

1. Prepare the Eclipse,android SDK installation package, Android NDK installation package (HTTP://DL.GOOGLE.COM/ANDROID/NDK/ANDROID-NDK64-R10-DARWIN-X86_64.TAR.BZ2)

2. Deploying the Android Development environment

3. Deploying the NDK Development environment (Configuring environment variables)

Ii. writing the NDK-based HelloWorld

1. Its role is to enter the libraryload that implements the native method that we declared in Java code, or load other dynamic connection libraries.

1 Static {2         System.loadlibrary ("MFIRSTNDK"); 3     }

The method of 2.native key identification, which does not require an abstract method of the method body, is implemented by C/S.

1  Public Static native String Getstr ();

The 3.mfirstndk.cpp file is used to return the operation implementation returned by the Java local method getstr

1#include"jni.h"2#include"Com_threew_ndk_ndkplus.h"3 /*4 * Class:com_threew_ndk_ndkplus5 * Method:getstr6 * Signature: () ljava/lang/string;7  */8 jniexport jstring jnicall java_com_threew_ndk_ndkplus_getstr9(JNIENV *env, Jclass thiz) {Ten     returnEnv->newstringutf ("Hello World JNI"); One}

4. Write the above file code, you can call the local method Getstr () in the Android activity, and get "Hello World JNI"

Iii. using ant tools to quickly generate a. h header file for a Java native method

With ant plug-ins in eclipse, you can see that you write an ant XML file under your project and add it to your ant working directory, double-click or run as to generate the desired header file directly under the Jni folder. The XML content is as follows:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!-- ====================================================================== 3 2014-9-20 Morning 11:27:054 5 MFIRSTNDK6 Description7                    8 Rubert9      ======================================================================  -Ten <Projectname= "MFIRSTNDK"default= "Buildallheaders"> One     <Description> A Description -     </Description> -  the     <!-- =================================  - Target:default -          =================================  - -     <Targetname= "Buildallheaders"> +         <AntcallTarget= "Buildgetstringheader"></Antcall> -         <AntcallTarget= "Buildgetintheader"></Antcall> +     </Target> A  at     <!-- - - - - - - - - - - - - - - - - -  - Target:depends -          - - - - - - - - - - - - - - - - -  - -     <Targetname= "Buildgetstringheader"> -         <JavahDestdir= "./jni"Classpath= "./bin/classes/"class= "Com.threew.ndk.NDKInt"></Javah> -     </Target> in      -     <!-- =================================  to Target:name +          =================================  - -     <Targetname= "Buildgetintheader" > the         <JavahDestdir= "./jni"Classpath= "./bin/classes/"class= "Com.threew.ndk.NDKPlus"></Javah> *     </Target> $     Panax Notoginseng </Project>

Four. Create a new C + + class file and invoke it with a Java local file

1. Create Hello C + + files under the Jni folder, generate Hello.h and Hello.cpp files

2. In the Hello.h file, add a method that reads as follows:

1 /*2 * Hello.h3  *4 * Created on:2014-9-205 * Author:rubert6  */7 8 #ifndef Hello_h_9 #defineHello_h_Ten  One classHello { A  Public: - Hello (); -     char * getwords (); the     Virtual~Hello (); - }; -  - #endif/* Hello_h_ */

3. The Hello.cpp file is implemented in the following ways:

1 /*2 * Hello.cpp3 * Created on:2014-9-204 * Author:rubert5  */6#include"Hello.h"7 Hello::hello () {8     //TODO Auto-generated Constructor9 }Ten Char*hello::getwords () { One     return "Hello"; A } -hello::~Hello () { -     //TODO Auto-generated Constructor

4. The implementation in MFirstNDK.cpp is as follows:

1 jniexport jstring jnicall java_com_threew_ndk_ndkplus_getwords (jnienv *env, jclass) {2     Hello H; 3     return Env->newstringutf ((constChar*) h.getwords ()); 4 }

5. Problems encountered

In the compilation project appears, 4 in the Hello file can not be found, if my header file import, but still cannot run. The last discovery was the cause of the eclipse error: Finally, some hints were canceled.

Http://www.oschina.net/question/1987532_162259?sort=time

Intermediate discovery may be the problem, but it's still the same after import

http://blog.csdn.net/meegomeego/article/details/8109604

V. Based on X86 simulator and arm simulation

Genymotion simulator based on X86 architecture

Android comes with the simulator based on ARM architecture

In general, the NDK is the so file that generates arm if you want to change it to a X86 so file. Then you have to create a new Application.mk file in the project directory with the following contents:

If both can be generated, then these

App_abi: = x86 Armeabi

Vi.. Attached

Terminal related records, MAC Terminal command

1. Garbled problem, open terminal, preferences-settings-advanced-Chinese

2. Use the CD command to enter the folder, open the current folder

3.javah-jni Com.xx.xx.xx.Xxx Generating header file

  

Android NDK Learning Record (i)

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.