The NDK development environment configuration for Android under Eclipse

Source: Internet
Author: User

Groping for a week, go a lot of detours, stumbling, finally is the NDK development environment, here to summarize it.

First, the Android NDK development environment

Download and install the JRE, JDK, first, needless to say. Then download the NDK package, it is recommended to download the R10 version of the compiler, comes with C + +, do not need to use the external compiler trouble. After downloading the NDK package, unzip it and then link it to eclipse. Then download the ADT Bundle, the Android Development Kit, which integrates the Eclipse's Android development environment, but the version may be low and does not have the integrated NDK functionality, and can be updated with SDK Manager to download the new ADT package. The SDK could not be updated due to the Great Firewall. The following settings are available for updating the SDK.

HTTP Proxy Server is set to mirrors.neusoft.edu.cn

HTTP Proxy Port set to 80

Other settings such as

After setting up the reload, then select the required toolkit to install.

This is followed by an ADT update, download the latest ADT package, and then update it offline in Eclipse's Help->install New software.

After the update is complete, the NDK appears in the Android column in preferences, selecting the path to set the NDK.

In this way, our development environment is configured to complete. The NDK that can be expected to develop ~ ~

Second, run the NDK official example Hello-jni

First import hello-jni into the workspace, File->import, select existing Android Code into Workspace, click Next, and then in root Directory Browse to find the Ndk folder under the samples under the Hello-jni selected, and then tick, complete the project import, the entire project is copied to the work space.

After importing, you can see the engineering structure in the Project Browse window, such as

Right-click this project, Android tools->add Native support, enter after the direct point to complete.

So we can compile and run.

Third, write your own application for the first NDK

We will write an NDK application that calls the local method Getnum (), gets an integer, and then displays it.

First new project, named Myfirstjni, I choose the lowest API 16, the other is API 19, the rest of the default settings, set up the project.

Then in the mainactivity of the project, write the following code:

 PackageCom.pplxlee.myfirstjni;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TextView TV=NewTextView ( This);    Tv.settext (String.valueof (Getnum ())); //the int number is obtained by Getnum method and converted to sting to TVSetcontentview (TV);//Display the contents of the TV    }        Private native intGetnum ();//declaring the native method Getnum        Static{system.loadlibrary ("Myfirstjni");//loading the dynamic link library libmyfirstjni.so} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

Then right-click on the project, Android tools->add Native support, enter after the direct point to complete.

We will see the new folder JNI generated and the files MyFirstJni.cpp and android.mk generated. MyFirstJni.cpp is used to write C + + code to define the implementation of the Getnum () method, while Android.mk describes how to compile C + + accomplishment.

In contrast to the official JNI example, we can see that we are missing a application.mk, we can also add a file in our project, the file content is App_abi: = All, this file allows the compiler to generate its supported full-platform so file.

Since the C + + code to write JNI has its own specifications, for convenience, you can use the JDK Javah tool to automatically generate header files, as follows.

Open the External tool configuration, create a new tool javah4android, and make the following settings (note: The build configuration of the Android JNI is different from the Java JNI build configuration and cannot be mixed):

Location:

Browse Jdk\bin directory and find Javah.exe

Working Directory:

${project_loc}

Arguments:

-v-classpath "${PROJECT_LOC}/SRC"
-D "${project_loc}/jni"
-jni ${java_type_name}

Select the app when you're finished setting it up. Then, run the tool with the cursor within the Mainactivity.java. You will see a com_pplxlee_myfirstjni_mainactivity.h file generated within the JNI folder.

You can see the following code when you open it:

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class com_pplxlee_myfirstjni_mainactivity*/#ifndef _included_com_pplxlee_myfirstjni_mainactivity#define_included_com_pplxlee_myfirstjni_mainactivity#ifdef __cplusplusextern "C" {#endif/** class:com_pplxlee_myfirstjni_mainactivity * method:getnum * Signature: () I*/jniexport jint jnicall java_com_pplxlee_myfirstjni_mainactivity_getnum (jnienv*, jobject); #ifdef __cplusplus}#endif#endif

is actually declaring the Jniexport jint jnicall java_com_pplxlee_myfirstjni_mainactivity_getnum (jnienv *, jobject) function, corresponding to the Getnum function in Java code, the remaining thing is to encode the implementation of the Getnum function. However, there may be an eclipse bug here, that is, when you click C + + code, you may produce errors that cannot be resolved by the type, causing the compilation to run. The workaround is to delete these errors before compiling the run.

Next, write the MyFirstJni.cpp file as follows:

" Com_pplxlee_myfirstjni_mainactivity.h " jint java_com_pplxlee_myfirstjni_mainactivity_getnum     *, Jobject) {    return;}

Then compile and run the results on the phone as follows:

Since then, a simple Android NDK program has been completed!

Vi. Summary

As a beginner, must calm down to 1.1 points of study, avoid impatient gas, will go a lot of detours!

Later will learn how to use STL libraries and OPENCV libraries in JNI, and will continue to share them on my blog after learning.

  

The NDK development environment configuration for Android under Eclipse

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.