Ndk --- use and development steps

Source: Internet
Author: User

Scenarios where ndk is used:

1. Some methods are implemented using local code in C, C ++. Then, I want to call these methods in Java. At this time, we need to use JNI technology.

Why is C and C ++ code used for implementation?

The reason is:

1. some functions need to be implemented using third-party class libraries. Third-party class libraries are written in C or C ++. Only C or C ++ code can use this class library. So,

It is implemented using C, C ++ code. Then, the implemented method is called by Java code.

2. Some functions are implemented using C and C ++ code, which results in high speed and performance. For example, the Code for video transmission. In this case, these functions are implemented in C, C ++ and then exposed to Java code calls in the form of methods.

 

Ndk development steps:

Development steps:

1. in a Java class, such as mainactivity. java: declare local methods implemented using C, C ++ code, such as stringfromjni; and the line of code that loads the dynamic library, although the dynamic library has not yet been generated.

2. Compile the Java class mainactivity. java. Obtain the mainactivity. Class file. (This step is not required when using Android ndk)

3. Use javah mainactivity. Class to generate a header file that can be used in C, C ++ code. (This step is not required when using Android ndk)

4. Use the C, C ++ code to implement the methods declared in mainactivity. java. In this implementation code, it should contain the header file generated in 3 (this step is not required when Android ndk is used). Then, the code, syntax, and method in the Code comply with the JNI interface.

5. Compile Android. mk to show the compiling system how to compile C and C ++ code into a dynamic library (. So ).

Procedure:

After the above development steps are completed, the next step is to compile:

1. In the JNI directory, enter the ndk-build command. In this way, the compilation system will automatically compile the dynamic libraries on various platforms.

2. After completing step 2, you can compile the android project and use the generated APK to obtain the dynamic library.

This is done.

 

Summary:

1. On the Java code side, you only need to perform two steps: declare the local method to be called and load the dynamic library. This is represented in the code when no dynamic library is generated.

2. The next step is to use C, C ++ code to implement local methods. When using variables and data types, follow the methods defined in the JNI interface. Here, we need to use the knowledge of JNI, such as the correspondence between the data types in JNI and the data types in Java, and the conversion between the strings in JNI and those in Java.

Others, after implementing C or C ++ code, compile an android. mk file to describe how to compile C and C ++ code. Then, compile and generate a dynamic library, and then compile the android project. The project contains the generated dynamic library (. So ).

Steps:

1.

2.

Java implementation code:

3. Write a C program that implements the local method to be used in Java. Create a JNI directory to store the implemented C code:

4. Compile Android. mk to describe how to compile the source file in JNI into a module.

As follows:

The above code indicates: 1. The final module name is myhello-JNI; 2. the compiled source file is hellojni. C.

5. Use the ndk tool to compile and generate a dynamic library:

Then, the entire android project will become:

Among them: The OBJ folder and. So in libs are generated after the ndk tool is compiled.

6. Compile the android project and generate the APK as usual

The final result is as follows:

Ndk --- use and development steps

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.