Android NDK Development from the environment to the demo-level 10-step flow

Source: Internet
Author: User

Write before the text:

A few months did not update the blog, feel a little rusty, so that can not break, a broken person lazy.

In fact, these months are not nothing to do, I am always thinking about this year's mission, 10 books, 30 blog posts ..., these months are also in the study, learning H5, learning design patterns, as well as the NDK JNI development and so on.

Learning JNI is mainly because the company has some Cocos games need to add billing points, and there is no real game developers, this task falls on me, and then is all kinds of abuse, a abuse in the end, miserable, this abuse is not learning technology abuse, but a game for n billing points, non-stop change to change, The need for that demand, and then you wait to be played.

Body

OK, to talk about the NDK, I believe everyone is not unfamiliar, it is Google for Android Development provides a native development set: Native development Kit, but also a containing API, build tools, cross-compilation, debugger, A series of toolsets, such as document examples, can help developers quickly develop C (or C + +) dynamic libraries and automatically package so and Java applications together as APK.

Another word that is closely related to DNK is JNI, which is the hub of DNK development, and the vast majority of Java and underlying interactions are done through it, so let's look at what is JNI?

Jni:java Native Interface is the Java local interface, which is a protocol used to communicate Java code and native code (c + +). With this protocol, some methods of Java classes can be implemented using native implementations, while allowing them to be invoked and used as normal Java methods, while native methods can use Java objects to invoke and use Java methods. In other words, using JNI, this protocol can be implemented: Java code calls C + + code, and C + + code can also invoke Java code.

So why use the NDK to develop it?

    • As we all know, Java is a semi-explanatory language, it is easy to disassemble and get the source code files, in the development of some important protocols, we use C language for security purposes, to write these important parts, to increase the security of the system.

    • In some of the complexities of computing, C + + is more efficient in scenarios where high performance is required, and code is easier to reuse.

Of course there are other advantages that drive us to choose a relatively efficient and secure DNK to develop our applications.

OK, so many ndk, so how do you use the NDK to develop applications?

As the saying goes, 工欲善其事 its prerequisite, want to use the NDK development, must first polish the good tool. First of all, let's take a look at DNK's environment.

DNK Environment Construction
    1. Installation Configuration NDK

      First download DNK, here I use is android-ndk-r14b-windows-x86_64, can choose independently.

      1). Unzip the NDK Zip package, note that the path directory does not appear spaces and Chinese , it is recommended that you unzip the package into the SDK directory, and named Ndk-bundle, the advantage is that Start as will check it and add directly to the Ndk.dir, reduce our configuration work;

      2). Configure path: Add the extracted path to the environment variable path;

      3). NDK-BUILD:CD to the root directory of the extracted NDK, execute the ndk-build command.

    2. For the as configuration associated NDK, here I am using Androidstudio, using eclipse will be different, please find data to configure it yourself.

      1). Add the following configuration to the Local.properties in the established project
      Ndk.dir=d:\guanmanman\androidstudio\sdk\ndk-bundle, note here that the escape character "\" is used to escape character. If the NDK directory is stored in the SDK and named Ndk-bundle, this configuration is automatically added.

      2). Add configuration to the previous version of NDK support in the project Gradle.properties
      Android.usedeprecatedndk=true

OK, here Our basic DNK environment configuration has been basically completed, then we will start our DNK development journey.

The demo instance calls the local no parameter method directly returns the string one layout

Add a button control directly to layout to invoke the local method:

Two get the control in mainactivity and register its click Listener

Create Java2cjni classes and local methods

Create a Java2cjni class directly under our package and create a java2c local method in the class:

Four through the Javah command to get to the native file

Under the project root directory, go to the Main->java directory, select the file directory column, enter the cmd command directly and press ENTER to enter the Docs command, and execute the javah com.sanhui.ndkdemo.Java2CJNI command in the command:

After executing the Javah command, a. h header file is created in the current directory of Java

Five create a JNI folder in the main directory and transfer the header files in (iv) to the folder

Open the folder to see the local method header file that the system created for us.

VI. Create the implementation header file. C Source File

Create a JAVA2C.C source file under the JNI directory, introduce our header file com_sanhui_ndkdemo_java2cjni.h by # include, and Jniexport jstring jnicall The declaration method under the header file JAVA_COM_SANHUI_NDKDEMO_JAVA2CJNI_JAVA2C (jnienv *, jobject); Copy to our java2c.c, complement the method parameters, and implement a C string "I am from Native C." "The Return of:

OK, now that our code is written, we need to configure the compiled so name and the supported CPU types.

Seven the Build.gradle configuration generated under the project generates the so name and the supported CPU types

Add the following code under Android->defaultconfig:

ndk{     "Java2C" //so文件名     "armeabi""armeabi-v7a""x86" //CPU类型}

Of course, not configured here is also possible, the system will use the default project name as the name of the so file, and the CPU will support all types, but when our project name changes, in our reference to load so file where we need to change, do not change the words can not find the so library exception, so, This is configured only to facilitate the system to generate the so file name that we have created, not based on the project name.

Eight-load So library

The so library is loaded in the Java2cjni class we created, primarily to compile the local source code before we call the local method.

When you load a library with system.loadlibrary ("java2c"), the library name must be the same as the ModuleName name configured in Build.gradle, or the library will not be found.

Nine generate so files

Select Build->rebuild Project in the tool class for the project, recompile the project, and then as will generate the so file for us, the directory where so files are: Ndkdemo\app\build\intermediates\ndk\debug \lib under

Note: So file naming is: lib+modulename+.so

Ten execution Call local method

The local method is called by clicking the button in Mainactivity. and print it out by toast.

OK, now that we have completed a demo-level NDK application development, let's take a look at our execution results:

Here a demo level of NDK development has been completed, through the above ten steps have been vividly demonstrated the NDK development of all the steps of the process, I believe you have been fully learned.

The following chapters will cover examples of Java calls to native C methods and C callback Java methods, as well as the additional use of JNI in the context of this tutorial.

For more information please pay attention to the platform, blog updates will be timely notification. Love to learn love technology.

Android NDK Development from the environment to the demo-level 10-step flow

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.