JNI basics

Question 1: What is JNI?

Answer: JNI is the mechanism of the Java language. Java can call C/C++ code through JNI, and C/C++ code can also call Java code through JNI.

 

Question 2: What is NDK?

Answer: NDK is a code library with two functions:

(1) Convert .c/.cpp files to .so files

(2) Package the .so file and the Android application together into an apk

 

Question 3: What are we going to do?

Answer: Do the following five steps

(1) Create .cpp/.c files

(2) Use NDK to convert .cpp/.c files into .so files

(3) Use JNI to call the C/C++ function in the .so file in Java

(4) Use NDK to package the .so file into .apk

(5) Run on mobile phone

 

Question 4: What are the CMake and ndk-build methods?

Answer: When we want to convert .cpp /.c into .so files, Android Studio provides two ways:

The first is through the ndk-build tool, at this point we need to edit the Android.mk file.

The second is through CMake, at this time we need to edit the CMakeLists.txt file

The CMake method is more advanced, but requires the support of Android Studio 2.2 and above.

 

Question 5: What is introduced in this article?

Answer: Both are introduced, but the advanced CMake is introduced first, and then the backward ndk-build is introduced. If students have learned CMake, ndk-build can be ignored. However, in a sense, ndk-build has wider applicability, so you can try ndk-build when CMake doesn't work.

 

3. Practice: CMake method

1. Create a new Android Studio project without checking include C++ support

2. Open the SDK Manager and make sure the NDK is installed

3. Create the JNI folder

4. Create a C/C++ Source File in the created cpp folder and name it hello_world.cpp 

5. Go back to MainActivity and declare the native method helloFucker(String name)

 

Guess you like

Origin blog.csdn.net/wzhrsh/article/details/117365975