Android NDK Development (I) Environment setup and running example, androidndk

Source: Internet
Author: User

Android NDK Development (I) Environment setup and running example, androidndk

/*************************************** ************

* TODO: description.

* @ Author: gao_chun

* @ Since: 2015-3-30

* @ Version: 1.0.0

* @ Remark: indicates the source for reprinting.

**************************************** **********/


Download the Android development tool. Eclipse is used here. If the tool cannot be downloaded, View

The Android Website Cannot download eclipse-adt-bundle:Http://blog.csdn.net/gao_chun/article/details/41010629


If the Android SDK cannot be updated normally after the download, you can view

Android SDK cannot update the ultimate solution:Http://blog.csdn.net/gao_chun/article/details/37971461


After android ndk r7, you do not need to download and install Cygwin (simulating the Linux environment). You must download it in the previous NDK development, you also need to download the CDT (Eclipse C/C ++ development plug-in), and configure the compiler, environment variables, and so on. Too much trouble.

I. Introduce what NDK is.

1. NDK is a collection of tools
NDK provides a series of tools to help developers quickly develop C (or C ++) dynamic libraries, and can automatically package so and java applications into apk together. These tools are of great help to developers. NDK integrates with the cross compiler and provides mk files to isolate differences such as CPU, platform, and ABI, developers can create so simply by modifying the mk file (indicating "which files need to be compiled" and "Compilation feature requirements. NDK can automatically package so and Java applications, greatly reducing developers' packaging work.
2. NDK provides a stable and functional API header file statement.
Google explicitly states that this API is stable and supports the currently released API in all subsequent versions. From the NDK version, we can see that these Apis support very limited functions, including: C standard library (libc), standard Math Library (libm), and compression library (libz), Log Library (liblog ).


Ii. Why NDK

1. Code Protection. Because the java-Layer Code of apk is easily decompiled, it is difficult to reverse the C/C ++ library.

2. You can easily use existing open source libraries. Most of the existing open-source libraries are written in C/C ++ code.

3. Improve program execution efficiency. The high-performance application logic is developed using C to improve the application execution efficiency.

4. Ease of transplantation. The C/C ++ library can be easily used on other embedded platforms.


3. Download The NDK and decompress it.


Note: 1. There is a samples directory under the android-ndk-r10c Directory, which stores examples for learning.


2. Pay attention to the environment variables of NDK, such:



4. In Eclipse Window-> Preferences-> Android-> NDK, set the NDK path, for example:


Note: Check whether Android Native Delelopment Tools is installed in the ADT plug-in (check in Eclipse-Help-Install new Software)


V,After creating an Android Project, right-click the new Project and choose Android Tools> Add Native Support... to name the library file (. so), for example, hello


At this time, you will find that the project will have another jni folder, which contains Android. mk and hello. cpp files.

Android. mk is the Makefile of the ndk project, and hello. cpp is the source file of the NDK.

Introduction to the Android. mk file: http://blog.csdn.net/gao_chun/article/details/44656169


6. Write the following code in hello. cpp. You can refer to the hello-jni example in the samples directory.

#include <jni.h>extern "C" {JNIEXPORT jstring JNICALL Java_com_hello_Main_stringFromJNI(JNIEnv *env,        jobject thiz) {    return env->NewStringUTF("Hello jni");}}

In C code:

Return (* env)-> NewStringUTF (env, "Hello jni! ");

The C ++ code is:

Return env-> NewStringUTF ("Hello jni! ");


The naming rules for JNI interfaces are as follows:Java _+The package name that calls this method (replace _ with the package name)+_+Class Name That calls this interface+_+Method NameTwo parameters are required for the instance method. a jni environment pointerJNIEnv *The other is the Java instance that calls this method.Jobject

Note that an error is reported after the program runs as follows (Note: An error is reported after the program runs, not because the file is incorrect, this file is wrong because the java editor does not install a plug-in that supports C/C ++. After the file is written, you can open the colse project again, or clean ):



The error message is as follows:



Summary:With extern "C" modifier, the compiler will compile and connect in C language. In C language, the function name after compilation is different from that after C ++ function compilation, for example, foo (int x, int y ),CMay be compiled_ FooAndC ++Because heavy load is supported, imaging will be compiled._ Foo_int_intThis type of function name with parameters. If the function is compiled in C language, if the function name of _ foo cannot be found when the foo function is called, an error is returned. So add extern"C"Modifier.


VII,Main. java:

Package com. hello; import android. app. activity; import android. OS. bundle; import android. widget. textView; public class Main extends Activity {// The static method preferentially loads static {System. loadLibrary ("hello");} // declare the native method of the jni layer. Use the native keyword public native String stringFromJNI (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); TextView TV = new TextView (this); TV. setText (stringFromJNI (); setContentView (TV );}}

Note: In the static code block, System. loadLaibrary is used to load the. so library after compilation by the compiler, not the hello. cpp file.


8,Complete, run OK.



Compile the NDK before running it, And then compile the JAVA code. Compilation may encounter Unable to launch cygpath. Is Cygwin on the path? The solution is as follows:

1. Right-click the project, click Properties-> C/C ++ Build Building Settings to remove the Use default build command, and then enter $ {NDKROOT}/ndk-build.cmd



2. Click Environment in C/C ++ Build, and click Add... to Add the Environment variable NDKROOT, the root directory with the value of NDK


3. compile it again!


If you think the article is useful to you, clicking a thumbs up will be my greatest motivation!

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.