Introduction of NDK and JNI
NDK is all called the native Development Kit local language (c&c++) development package. The corresponding is the frequently contacted ANDROID-SDK, (software Development Kit) software development package (only supports Java language development).
Simply using NDK, you can develop pure c&c++ code, and then compile it into a library that lets you invoke Java programs that are developed using ANDROID-SDK. NDK development can be called low-level development or JNI (Java Native Interface) layer development, SDK development can be called upper level development.
Reasons for using NDK in Android development:
1, it is well-known that the use of the SDK code, generated APK, it is easy to decompile, security is very low, and the use of NDK development of the library, not easy to decompile, confidentiality, security has been improved.
2, many open source projects and large projects are c&c++ code, it is obviously impossible to convert them to a pure Java language.
3, c&c++ code to run faster and more efficient than Java much faster.
JNI is all called Java Native Interface (Java Local call). Starting with Java1.1, JNI became part of the Java platform, allowing Java code to interact with code written in other languages, such as c&c++. The concept of JNI was not introduced from the Android release.
Simply put, Android's NDK provides some cross-compilation tool chains and Android libraries, which can be invoked by developers when writing native language programs. The NDK provided by the Cross compilation tool chain compiles the c&c++ code that has already been written to build the library. Of course, you can also build your own cross compilation environment without NDK tools and libraries. The library is then generated and, as long as the specification operates, it can generate a library file that can make the Java layer successfully invoke.
Second, NDK environment to build
Using the latest NDK, directly discard Cygwin, the previous Android project to use NDK must download NDK, download installation Cygwin (simulation of Linux environment), download the CDT (Eclipse C + + development plug-in), but 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 the Android NDK http://developer.android.com/tools/sdk/ndk/index.html (Note: The NDK version is integrated Cygwin after R7 and is still very compact. )
I use the SDK and NDK for
Eclipse Configuration
Open Eclipse, point window->preferences->android->ndk, set NDK path, for example, mine is f:android-ndk-r10e. Create a new Android project Ndktest, right-click on the Android Tools->add Native Support ..., and then give us a name for our. So file (the default is already, the package name Ndktest). This time the project will be more than one Jni folder, JNI under the Android.mk and NDKTest.cpp files. ANDROID.MK is the NDK project Makefile,ndktest.cpp is the NDK source file.
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!);
}
</string.h></jni.h></span>
Continue to write Mainactivity.java files
<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 declaration that this method is from the native layer. The implementation process has implemented the
static {
system.loadlibrary (ndktest) in the native layer,//loading library, the preceding lib and suffix names do not have to write
}
}
</ Span>
Compile and run, first compile CPP file, right click Project Ndktest,build Project, success results in Libs directory generated. So file
Then compile the Android Java project, right-click the project run application, and deploy to the results on the real machine
Iii. related issues
NDK in the process of building and example, some problems encountered are as follows:
Eclipse error "Unresolved inclusion jni.h"
In the process of doing NDK development, sometimes in eclipse will encounter its inability to handle inclusion led to symbol display error, there are many ways to solve similar "unresolved inclusion jni.h" error method, Including Includepath and other methods, but not for me.
The final solution is to initialize Eclipse's nativesupport for this project:
1. Close the specified project in eclipse
2. Open the project's. project file with another editing tool, and delete the following:
......
Org.eclipse.cdt.managedbuilder.core.genmakebuilder
Clean,full,incremental,
......
Org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder
Full,incremental,
......
Org.eclipse.cdt.core.cnature
Org.eclipse.cdt.core.ccnature
Org.eclipse.cdt.managedbuilder.core.managedBuildNature
Org.eclipse.cdt.managedbuilder.core.ScannerConfigNature
3. Delete the. cproject file
4. In Eclipse, open the original project, refresh, and then right-click->properties->android Tools-> Add Native Support
5. Take care of
Method ' Newstringutf ' could in development of Eclipse NDK under window platform resolved problem
Project right-key-> properties->c/c++ General->code Analysis, select the method in the use project settings cannot be resolved (methods cannot be resolved) deselect, apply-> OK, Then refresh, clean, refresh, build the project.
jni/hellocpp/main.cpp:16:18:error:base operand of '-> ' has non-pointer type ' jnienv {aka _jnienv} '
The error is:
(*env)->newstringutf (env, Hello from JNI!);
This line, this is the writing of C, and mine is a CPP program, need to rewrite into:
Env->newstringutf (Hello from JNI!);
Java.lang.UnsatisfiedLinkError:Native method Not found problem
1, C + + methods in java_xxx_xxx Java first letter must be capitalized
2, if the. cpp file is in extern C {your method here}, in curly braces is your local method.
The above is the Android NDK environment build and sample code, do the collation, follow-up continue to supplement the relevant information thank you for your support!