Android JNI adolescent Cmake (android studio)

origin

Life is not only about the present, but also poetry and the distance. If you are a rookie and want to become a great god admired by thousands of people, then you might as well set a small goal for yourself first, for example, live it for 200 years first, and then borrow another 500 years from the sky. Well, when I heard it for the first time, my heart trembled non-stop, as if we had each other in the previous life, but in this life we ​​have waited for a long, long time. The script of cross-platform operation, simple style, cmake in adolescence, vigorous vitality and vitality, people can't help but be fascinated by it.

introduce

What is Cmake? The giant Baidu said:
CMake is a cross-platform installation tool that can describe the installation (compilation process) of all platforms with simple sentences. It can output various makefiles or project files, and can test the supported C++ features, similar to automake under UNIX. CMake can compile, make, generate adapters (wrappers), and build in any order. CMake supports in-place builds (binary files and source code in the same directory tree) and out-of-place builds (binary files in other directories), so it is easy to build from the same source code directory tree Construct multiple binary files. CMake also supports the construction of static and dynamic libraries. The name "CMake" is short for "cross platform make".
Simply put, for us, it is to generate so.

Preparation

For cmake, as support came a bit late, and it didn't start until after as 2.2. So if you have a lower version of as, please upgrade. If there is any inconvenience, please contact Google customer service. After the upgrade is complete, open the SDKManager, and there are still a few small plug-ins that need to be installed. For example, the cmake plug-in, you can’t install it if you don’t install it. If, if you say if, I’m sorry that there is no if here. For example, the NDK plug-in, in the official words, the native development kit, allows you to use C and C++ code for Android, and provides many platform libraries. For example, LLDB plug-in, debugging native code, optional, recommended.
Check the situation map
The picture above is taken from the official website document, please check the required items independently. If you find it bothersome, check all of them with one click and wait patiently for three days and three nights. I will support you with all my hands and feet.

journey begins

Died before leaving the teacher, ah bah, this, this, let's get back to the topic and start our journey of hello jni. One target (the project supports cmake), two paths. For one of the shortcuts, create a new project and check the Include C++ Support checkbox. For this way, there is nothing to explain, next step, next step.
Include C++ Support checkbox
The other one is to support cmake in existing projects in several steps.
Step 1. Create a directory under the src/main directory and name it cpp. This name is arbitrary, as long as you like it. Put the code that needs to be imported into the created directory, don't be random here, and don't get emotional.
Step 2. Create a CMake build script, a plain text file, named CMakeLists.txt, this name has a fixed format, no temper is allowed. As for where you put this file, it's up to you.
my choice
Step 3. In the build.gradle file in the app module, provide cmake support.

android{
    ...
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

The first configuration declares that the Native Build method is cmake, and specifies the cmake script path.

android{
    ...
    defaultConfig{
      ...
      externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"
            }
        }
    }
}

Yo, don't miss it when you pass by. The second configuration sets the conditions for cmake compilation, such as the c11++ standard. The same is externalNativeBuild, here is the defaultConfig domain, and the cmake path is the android domain. Sometimes I think, why not combine the two into one, and have to be written twice, which is a headache, and I don't understand it here.

The key to the journey, cmake configuration

The documentation of cmake, E-text, makes people intoxicated and unable to extricate themselves. There are so few APIs, which are more important.
Important 1. add_library, generate lib. For us, System.loadLibrary("xxx"), where xxx is our lib. Examples are as follows:

# 创建一个 lib,静态 or 动态,可创建多个
add_library( # lib名称
             JniDemo

             # lib类型
             SHARED

             # .c/cpp目录
             jni_dynamic.cpp )

Important 2. set_target_properties, used to use the built lib such as .a. Examples are as follows:

# 添加自己的 lib 库
add_library( libHello
             SHARED
             # 由于.a 已经属于已经构建好的库,使用 IMPORTED 标志,告诉 cmake 不需要编译了
             IMPORTED )

set_target_properties(
                    #名称
                    libHello

                    # 指定要定义的参数.
                    PROPERTIES IMPORTED_LOCATION

                    # 导入 lib 的路径
                    ${CMAKE_SOURCE_DIR}/sources/${ANDROID_ABI}/libHello.a)

Important 3. find_library, find lib. Examples are as follows:

# 搜索 NDK 指定 lib
find_library( # lib 路径变量的名称。
              log-lib

              # 查找指定 lib 名称
              log )

Important 4. target_link_libraries, link all libs to generate new libs, examples are as follows:

# 链接库
target_link_libraries( # 指定目标 lib
                       JniDemo

                       # 自己的 lib
                       libHello

                       # NDK log lib  //这里放 find 的路径 ${log-lib} 也是可以
                       log )

Let's just talk about these few for the time being, basically it should be almost enough. If so, please feel free to check out the cmake documentation, I think you'll like it. There is one thing to mention here. Regarding the above four points, two of them are the most important. Key point 1 is to generate lib and build a bridge between java and c/c++. Key point 4 is to link all libs to build a complete runnable lib. These two can basically solve the problem. As for the redundant two key points, for the sake of a clearer structure, it’s just intuitive.

Share the joy of the journey

At this point, jni has come to an end. As for what I thought before, it can be regarded as a gratifying and happy ending. The reason why I take jni so seriously is because I am willing and I like it. It would certainly be gratifying if I could use this in exchange for my knowledge and understanding of jni. If not, I have nothing to regret.
A github ticket is attached to take you to experience the vastness of the world

Guess you like

Origin blog.csdn.net/youyi300200/article/details/73477654