Android NDK Environment Setup and simple example

Source: Internet
Author: User

I. Introduction to NDK and JNI

The NDK is all called the native Development Kit native language (c&c++) Development Kit. And the corresponding is the frequent contact with the ANDROID-SDK, (software Development Kit) software Development Kit (Java language Development only support).

In a nutshell, using the NDK, you can develop code that is pure c&c++ and then compile it into a library so that Java programs developed using ANDROID-SDK are called. The NDK development can be called the development of the underlying or JNI (Java Native Interface) layer, and SDK development can be called upper-level development.

Reasons to use the NDK in Android development:

1, it is well known that the use of code written by the SDK, the generated apk, it is easy to decompile, security is very low, and the use of NDK Development library, not easy to be anti-compilation, confidentiality, security has improved.

2, many open source projects and large projects are c&c++ code, it is obviously impossible to convert them into a pure Java language.

3. c&c++ code runs much faster and more efficiently than Java.


JNI is all called Java Native Interface (Java Local call). Starting with Java1.1, JNI becomes part of the Java platform, which allows Java code to interact with code written in other languages, such as c&c++. The concept of JNI was not introduced from Android.

In short, the Android NDK offers a number of cross-compilation toolchain and Android-brought libraries that allow developers to invoke programs in their native languages. The cross-compilation toolchain provided by the NDK compiles the c&c++ code that has been written, creating a library. Of course, you can also build your own cross-compilation environment without the NDK tools and libraries. The library is then generated, as long as the canonical operation can generate a library file that allows the Java layer to be successfully called.
Second, NDK environment construction

Using the latest NDK, directly discard Cygwin, previously done Android projects to use the NDK must download the NDK, download install Cygwin (for simulation of the Linux environment), download the CDT (Eclipse C + + development plug-in), also configure the compiler, environment variables, Special Trouble, the new version will not be needed.

Download Android SDK http://developer.android.com/sdk/index.html

Download Android NDK http://developer.android.com/tools/sdk/ndk/index.html (Note: The NDK version integrates Cygwin after R7 and is a very lite version.) )


I use the SDK and NDK for


Eclipse Configuration

  • Open Eclipse, point window->preferences->android->ndk, set the NDK path, for example my is F:\android-ndk-r10e.
  • Create a new Android project Ndktest, right-click on the project on Android Tools->add Native support ..., and then give us a name for the. so file (by default, the package name Ndktest).
  • At this time the project will be more than one Jni folder, JNI under the Android.mk and NDKTest.cpp files. Android.mk is the makefile,ndktest.cpp of the NDK project, which is the source file of the NDK.
  • Next, write the NDKTest.cpp file
  • <span style= "FONT-SIZE:14PX;" > #include <jni.h> #include <string.h>extern "C" jstring java_com_example_ndktest_mainactivity_ Ndktestfromjni (jnienv* env,jobject thiz) {return Env->newstringutf ("Hello from JNI!");} </span>
  • Continue writing the Mainactivity.java file
  • <span style= "FONT-SIZE:14PX;" >package Com.example.ndktest;import Android.app.activity;import Android.os.bundle;import Android.widget.textview;public class Mainactivity extends Activity {protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); TextView TV = new TextView (this); Tv.settext (Ndktestfromjni ()); Setcontentview (TV);} Public native String Ndktestfromjni ();//native declares that this method is from the native layer. The implementation process has been implemented in the native layer of static {system.loadlibrary ("ndktest");//load library, the preceding lib and suffix names do not have to write}}</span>
  • Compile run, first compile CPP file, right click Project Ndktest,build Project, successful result in Libs directory generated. So file

  • Then compile Android Java project, right click on Project Run application, deploy to real machine results
Iii. Related Issues

Some of the issues encountered during the NDK build and instance process are as follows:


    • Eclipse Error "unresolved inclusion jni.h"

In the NDK development process sometimes encountered in eclipse it can not handle inclusion cause symbol display error, there are many ways online to solve a similar "unresolved inclusion jni.h" error method, Including Includepath and other methods, but it doesn't work for me.

The final solution is to initialize Eclipse's nativesupport of the project:

1. Close the specified project in eclipse

2. Open the project's. project file with another editing tool and delete the following:

......
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
......
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>

......

<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>

3. Delete the. cproject file

4. In Eclipse, open the original project, refresh, and then right-click->properties->android Tools, ADD Native support

5. Done


    • Method ' Newstringutf ' could not being resolved problem in Eclipse NDK Development under window platform

Project Right--Properties->c/c++ General->code Analysis, selecting Methods in "Use Project Settings" cannot be resolved (method cannot be resolved) deselect, apply--OK , and then refresh, clean, refresh, and build the project.


    • Jni/hellocpp/main.cpp:16:18:error:base operand of ' a ' has non-pointer type ' jnienv {aka _jnienv} '

The error is:

(*env)->newstringutf (env, "Hello from JNI!");

This line, this is the C notation, and my is the CPP program, need to be rewritten as:

Env->newstringutf ("Hello from JNI!");


    • Java.lang.UnsatisfiedLinkError:Native method Not found problem

1. Methods in C + + java_xxx_xxx The first letter in Java must be capitalized

2. If the. cpp file is used with extern "C" {"Your method is Here"}, the curly brace is your local method.

Iv. Reference Reference

Http://jingyan.baidu.com/article/5d6edee22d908799eadeec9f.html

Http://www.cnblogs.com/devinzhang/archive/2012/02/29/2373729.html

http://my.oschina.net/liusicong/blog/311886

HTTP://BLOG.CSDN.NET/CONOWEN/ARTICLE/CATEGORY/1127720 (classic must SEE)



Android NDK Environment Setup and simple example

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.