Android NDK Usage Analysis

Source: Internet
Author: User

Android NDK Usage Analysis:

In Android Application Development, we generally use C/C ++ code to develop modules with high performance and security requirements, or when Referencing some implemented C/C ++ libraries, the JNI mechanism is required. As described above, we can also write a JNI-based mechanism to access C/C ++ library files.

Android NDK is a development tool set provided by Google. We can use it to develop JNI-based programs quickly. Its important functions are as follows:

A. provides cost-effective library files (compilers, connectors, etc.) for compiling C/C ++ source code)

B inserts compiled library files into androidpackage (.apk)

C. Provide documents, instances, and specifications related to NDK development.

Note:

Google does not recommend developers to use NDK to develop Android local applications. Of course, this can be implemented because the main application of Android is a Java program running on the dalvik virtual machine, we recommend that you develop programs running on dalvik.

Functions of NDK:

As shown in, Android ndk first compiles C/C ++ local code, generates a local library file, and inserts it into the Android Application Package, however, we still need to call JNI to compile local code.

NDK installation:

Download the latest NDK package from the Google official website and decompress it to the specified directory. This directory is used as the name of the environment variable, that is, NDK_HOME, add the NDK_HOME environment variable to the path of the environment variable.

Cygwin installation:

On the above page, we have installed and installed Cygwin. Here we will briefly introduce its role. Cygwin is mainly used to process conversions between different platforms. For example, Android is a framework set based on Linux, while the system we often use is a windows system, therefore, it is used to convert library files from windows to Linux-based systems. Of course, this step is not required if the system is Linux.

Since we have introduced it here, we will undoubtedly use it. Back to the configuration of the NDK environment, how can I verify whether the environment variable is configured successfully? The answer is to use this Cygwin tool. Open it and enter ndk-build in it. If this result is not reported, the environment variable is successfully configured:

Oh, the error here is because the project path is not set, so ignore it here.

Steps for using NDK:

In fact, the use process of NDK is very similar to the process and content of the previously introduced "analysis on the use of Android Local interface JNI". Here, I will only introduce something different from the previous one, the following is the development process of NDK:

1. Create a Java local method class

2. Compile the Java local method to generate the C/C ++ header file

3. Compile and implement the above C/C ++ implementation file

4. compile and generate a dynamic Runtime Library (. so)

5. Run the application

Next, we will introduce the use and precautions of NDK based on the above process. My project directory:

The running effect is as follows:

Click the button to print the LOG.

Create a Java local method:

The local method for creating Java is the same as that in the previous article, except that the LOG Content here is "Hello Ndk !" The Code is as follows:

Public class VerifyNdkJniMethod {

Static {

System. loadLibrary ("hello_ndk ");

}

Public static native String helloNdk ();

}

Compile the Java local method:

Similar to the content in the previous article, the Javah command is used to compile the generated class file to generate the corresponding C/C ++ header file. My header file:

/* Do not edit this file-it is machine generated */

# Include

/* Header for class com_demo_verifyndk_jni_ndk_VerifyNdkJniMethod */

# Ifndef _ Included_com_demo_verifyndk_jni_ndk_VerifyNdkJniMethod

# Define _ Included_com_demo_verifyndk_jni_ndk_VerifyNdkJniMethod

# Ifdef _ cplusplus

Extern "C "{

# Endif

/*

* Class: com_demo_verifyndk_jni_ndk_VerifyNdkJniMethod

* Method: helloNdk

* Signature: () Ljava/lang/String;

*/

JNIEXPORT jstring JNICALL Java_com_demo_verifyndk_1jni_ndk_VerifyNdkJniMethod_helloNdk

(JNIEnv *, jclass );

# Ifdef _ cplusplus

}

# Endif

# Endif

Similarly, as with the previous header file writing method, my implementation file is as follows:

# Include

# Include "VerifyNdkJniMethod. h"

JNIEXPORT jstring JNICALL Java_com_demo_verifyndk_1jni_ndk_VerifyNdkJniMethod_helloNdk

(JNIEnv * env, jclass obj)

{

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

}

Compile and generate a dynamic library file (. so ):

Here, it is slightly different from what we have previously introduced. We still need to write the Android. mk file. My mk file is as follows (at the bottom of the article, we will introduce the mk file ):

# Specify the location of the source file

LOCAL_PATH: = $ (call my-dir)

# Initialize environment variables related to Make

Include $ (CLEAR_VARS)

# Information about library compilation, including the Library name and source code

LOCAL_MODULE: = hello_ndk

LOCAL_SRC_FILES: = VerifyNdkJniMethod. c

# Generate a shared library

Include $ (BUILD_SHARED_LIBRARY)

We also need to put the header file and the implementation file and the mk file together, but here we must create a jni directory under the root directory of the project, cd to this directory, use ndk-build or $ NDK/ndk-build to compile and generate dynamic library files. This is a little different from the previous one. We used $ NDK/ndk-build to compile it because we didn't implement it through NDK, the tool Cygwin is used to generate shared library files (this is true, we can use $ NDK/NDK-build or ndk-build to compile and generate environment variables with ndk configured. The difference is that NDK can be used for compiling, we must cd to the jni directory under the project root directory, and the above compilation command will take effect. The reason is that when NDK is used for compilation, the NDK itself has a compiler system. During compilation, the compilation system searches for AndroidManifest in the current directory. xml file. If the file exists, view the current directory as the Android project directory. Then go to the jni directory and compile NDK Based on Android. mk.

Run the application:

This process is basically the same as the previous article "JNI Usage Analysis of Android local interfaces". My code is as follows:

Public class VerifyNdkJniActivity extends Activity {

Private static String TAG = "VerifyNdkJniActivity ";

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. activity_verify_ndk_jni );

}

Public void helloNdk (View v ){

String result = VerifyNdkJniMethod. helloNdk ();

Log (result );

}

Private void log (String log ){

Log. d (TAG, log );

}

}

Android. mk file description:

In fact, the Help file of the Android. mk file is In/docs/ANDROID_MK.html (only in English), let's introduce it below!

LOCAL_PATH: = $ (param ...):

This variable is used to find the source file in the Development tree and must be defined at the beginning of the mk file. Generally, the following compiling format is used:

LOCAL_PATH: = $ (call my-dir)

My-dir is a macro function, and $ (call my-dir) is used to return the value of a macro function. Generally, the Android. mk file is in the same directory as the source file.

Include $ (CLEAR_VARS ):

It is used to initialize the LOCAL_XXX variable in the mk file, except for the LOCAL_PATH above, because the Android system regards the LOCAL_XXX variable as a global variable, so it will be initialized first.

LOCAL_MODULE: = XXX:

It is used to mark every module described in the mk file, that is, the name of the library to be generated. This name must be unique and cannot contain spaces. The compilation system will automatically generate the appropriate prefix and suffix, such as the libhello_jni.so file.

LOCAL_SRC_FILES: = XXX. c/. cpp:

Used to include the resource files to be compiled into the packaging module. At the same time, these source files are located in the directory specified by LOCAL_PATH.

Include $ (BUILD_SHARED_LIBRARY ):

Use the variable values such as LOCAL_PATH and LOCAL_SRC_FILES to create a library file named lib $ (LOCAL_MODULE). so.

At this point, some basic information about NDK has been introduced. If you want to use NDK more flexibly, you can refer to the simples self-contained in NDK and study it carefully. Its position is as follows: \ Samples \

/**

* Welcome to the Technical Group: 179914858

* If you have any questions, please discuss them in the comments.

*/

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.