Android JNI series detailed explanation of the use of ndk-build tool

1. Use the ndk-build tool to compile library files in Android projects

I have introduced the use of CMake compilation tool before. Today I will introduce the use of ndk-build, a compilation tool that comes with ndk.

There are currently two main ways to configure and use ndk-build:

 As shown in the figure above, the first way is to generate the library file using Android.mk+Application.mk+gradle; the second way is to generate the library file using Android.mk+Application.mk+command line. The following is an introduction to this Used in two ways.

1. ndk-build configuration build 1 (Android.mk+Application.mk+gradle)

Android.mk: used to describe source files and library information to the ndk-build compilation tool, such as what type of library (dynamic, static) to generate, the name of the library, and which C/C++ source code files are used to generate the library, or Which third-party library files are used, etc.

Application.mk: Project level, used to specify project-wide settings for ndk-build, such as C/C++ standard library support, Android platform version settings, abi settings corresponding to the CPU instruction set, etc.

Android.mk is a file that is required for ndk-build compilation. Application.mk is optional (if the compiled target library does not use the C/C++ standard library, there is no need to specify the abi corresponding to the cpu instruction set). If not Configuring Application.mk will use these default configurations. Another reason why Application is optional is that the configuration inside can be configured in gradle.

1. LOCAL_PATH indicates the location of the source file. call my-dir means that the macro function my-dir provided by ndk-build will be called. This method will return the directory path where the Android.mk file itself is located.

2.include $(CLEAR_VARS) is used to clean up some variables starting with LOCAL, but it will not clean up the above LOCAL_PATH.

3.LOCAL_MODULE indicates the name of the configuration library.

4.LOCAL_SRC_FILES indicates the configured source files, which source files or third-party libraries are needed to generate the library.

5.include $(BUILD_SHARED_LIBRARY) means specifying the compilation target library type (static and dynamic)

1.APP_STL means configuring C++ standard library support

2.APP_ABI means configuring abi filtering

3.APP_PLATFORM indicates the specified Android platform version

2. Create a new project and use ndk-build to compile the source code.

Different from the CMake project, we created a new project

haha.cpp native source file:

The C++ source code files are similar to those in CMake

Android.mk:

Application.mk:

build.gradle:

After configuring these files, we make the project and generate the library file in the build directory:

Use the library file generated above in Android:

run:

3. ndk-build configuration build 2 (Android.mk+Application.mk+command terminal)

This method is more convenient and does not rely on IDE tools such as AS. You can compile the library files in the folder.

This method requires changing the above files placed in the cpp folder to the jni file. I don’t know the reason at the moment.

In addition, I deleted all the previous configurations in gradle:

Start using the terminal command to compile: Now enter the jni directory, enter ndk-build and press Enter.

The generated library file is: automatically placed in the libs folder under the same level folder as jni: there is also an additional obj folder, don't worry about it.

If you run the project at this time, there will be a crash. The error is that libhaha.so cannot be found.

Use the library file generated above in Android: To use the library file in the android project, the library file must be placed in the Android default system jni directory: jniLibs, so we change the above libs to jniLibs and re-run, normal:

If you just want to use the libs folder generated above, it is also possible. You need to configure it in gradle:

This method means that when compiling in the future, if you look for jniLibs, it will automatically go to src/main/libs to find our library files and package them into apk.

2. Output path of configuration library file

In the above ndk-build compilation process, we did not specify the path we want to output. We all used the default directory, either under build or under the libs directory, which is the output directory of our configuration file.

The configuration under Android.mk is as follows

../jniLibs is the incense burner path, relative to the location of Android.mk. ../ represents the upper-level directory of Android.mk, that is, the jniLibs directory is created in the directory of the same level above the mk file.

Use the ndk-build command to see that the jniLibs directory is generated. In this way, jniLibs is the directory that is searched by default when compiling apk.

If it is this directory, we do not need to specify this in our build.gradle

Guess you like

Origin blog.csdn.net/sunbinkang/article/details/132529465