Android JNI series detailed explanation of the use of CMake compilation tool

1. Introduction to CMake tools

As shown in the figure, the main function of the CMake tool is to compile and package native source files written in C/C++ to generate library files (including dynamic libraries or static library files), and integrate them into Android for use.

2. Use of CMake compilation tool

 The main use is to configure two files: CMakeList.txt and build.gradle

1. Introduction to CMakeList.txt

The file path is as follows:

 The default content of the file is as follows: (Anything without the # sign is configuration)

cmake_minimum_required(VERSION 3.22.1)

# Declares and names the project.

project("testnative")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        testnative

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        testnative

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

1.cmake_minimum_required(VERSION 3.22.1)

Indicates that the minimum cmake tool version supported by the project is 3.22.1

2.

add_library( # Sets the name of the library.
        testnative

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

It means adding a library and defining the name, type and specified source code of the generated library.

testnative is the name of the specified library;

SHARED specifies the type of generated library: dynamic | static;

native-lib.cpp is the path relative to CMakeList.txt of the source code to be compiled, a relative path;

3.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

Indicates the introduction of a library. Here, the log library in ndk is specified, and the path of the library is assigned to the log-lib variable, which is equivalent to defining a variable log-lib in Java, and the value is log.

4.

target_link_libraries( # Specifies the target library.
        testnative

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

It's the link library. Link a specific library to the target library. This is to link the log-lib defined above to our testnative. The ${log-lib} here is to take out the value of log-lib and link it to our testnative. In this way, we You can use the log library.

2. Build.gradle under module is configured with the introduction of CMake

The file path is as follows:

 Open the file and look at the following configuration:

 CMake has made the following configuration in the android{} closure:

externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.22.1'
        }
    }

This configuration here configures the path to the CMakeList.txt file (the path is relative to the project) and the version of cmake used for compilation.

In addition, we can also configure other information (supported versions of the C/C++ standard library) under the defaultConfig closure, as follows:

 You don’t need to write the above configuration, just use the default one. The following is a sample configuration:

The C++ standard library configured here is of dynamic type (static libraries do not need to be specified), so a standard library file will be generated in the build after compilation.

Before adding it is:

After adding:

 By the way, here is an introduction to what the C++ standard library is:

 3. How to generate the so library in Android

 The above code can load the so library into the program, and then call the relevant native api, thereby calling the functions in the so library based on JNI.

Write relevant code:

On the Java side, we write a native method:

 The code corresponding to C/C++ is:

 The Java side can call this code on the C/C++ side by calling stringFromJNI(). This is the use of JNI.

Effect: The Android interface displays the string in the C++ function.

 

Guess you like

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