/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.
**************************************** **************************************** ************/
1. About Android ndk
Ndk is called the Native Development Kit local language (C & C ++) Development Kit. The android SDK (software development kit) is a commonly used software development kit (only supports Java Development ).
Simply put, ndk can be used to develop pure C & C ++ code, and then compiled into a library for Java program calls developed using Android-SDK. Ndk can be called underlying development or Java Native Interface (JNI) layer development. SDK development can be called Upper-layer development.
2. Why ndk?
2.1 As we all know, the APK generated using the code written by the SDK can be easily decompiled, and the security is extremely low. The library developed using the ndk is not easily decompiled and confidential, security is improved.
2.2. Many open-source projects and large projects are C & C ++ Code. It is obviously impossible to convert them into pure Java languages.
2.3. The code running speed and efficiency of C & C ++ are much faster than that of Java.
3. Establish an ndk Environment
In.
For example, I used cygwin for development in windows. To configure environment variables, see http://blog.csdn.net/conowen/article/details/7518870
Simply put, in the installation directory of Linux or cygwin, jump to/home/yourname/, edit the. bash_profile file, and add the following statement at the end, depending on your situation.
ndk=/home/conowen/android-ndk-r7b export ndk
4. Create an ndk Project
Create a directory named helloworld, and create a directory named JNI in it (the name must be JNI, because the JNI directory will be searched under the helloworld directory during ndk-build, and create the following file helloworld in the jni directory. C and Android. mk.
The helloworld. c file code is as follows (JNI does not have the jni_onload function)
# Include <string. h> # include <JNI. h> values (jnienv * ENV, jobject thiz) {return (* env)-> newstringutf (ENV, "helloworld! I am from JNI! ");}/* Note: Here, jstring indicates the return value java_com_conowen_helloworld_helloworldactivity_helloworldfromjni is written as the Java + Android project package name + Android project activity name + method name. The dot is underlined, this statement is very strict. Package name: com_conowen_helloworldactivity name: helloworldactivity Method Name: helloworldfromjnijnienv * env. jobject thiz is a built-in parameter of the native method and can be used to convert a data type. That is to say, this helloworldfromjni has no form parameter. */
The android. mk code is as follows:
For the compiling format of Android. mk, complete the next blog
Local_path: = $ (call my-DIR) include $ (clear_vars) # local_module indicates the name of the generated library. You do not need to write local_module: = helloworldlocal_src_files: = helloworld In the lib and suffix. cinclude $ (build_shared_library)
In the terminal, go to the helloworld directory and run
$ndk/ndk-build
$ Ndk is the previously configured ndk environment variable. After compilation is successful, the libs and OBJ folders are generated in the directory. libs contains the libhelloworld. So library that has just been compiled.
5. Create an android Project
Create an android project in eclipse. the Java code is as follows, and drag the generated libs folder to the android project directory.
Package COM. conowen. helloworld; import android. app. activity; import android. OS. bundle; import android. widget. textview; public class helloworldactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); textview TV = new textview (this); TV. settext (helloworldfromjni (); setcontentview (TV );} Public native string helloworldfromjni (); // native declaration, indicating that this method comes from the native layer. The implementation process has implemented static {system. loadlibrary ("helloworld"); // load the library, the lib and above, and the suffix does not need to be written} at the native layer }}
: