cmake method compile and generate so library CMAKE So

2023-03-18  

Before the CMAKE construction tool was released, it was ndk-build to compile and generate the so library, but the NDK-BUILD is more tedious and easy to make mistakes. We use CMAKE method. Attentive buddies may have noticed that when the Android Studio creates a project, you can check this method:

Look at the picture:
这里写图片描述

This is the most time -saving. I first look at what the project has generated something special:
cmakelists.txt This is the most conspicuous file. Yes, the system helps us to complete everything. The most important thing is this file. The content of the file is as follows:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library. 
 #Minimum requirements version number 
 cmake_minimum_required (version3.4.1)

# 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. Set the name of the so library. 
              native-lib 

              # Sets thelibrary as a shared library. Dynamic library will be dynamic and loaded at runtime 
              Shared 

              #Provides a Relative PathtoYour Source File (s). The path of the source code file to be compiled, if you are not under CPP, remember to change it here synchronously 
              src/main/cpp/native-lib.cpp) 

 # Searchesfor 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 ofthe path variable. The log support library is stored and the path is stored in the log-lib 
               log-lIB 

               # Specifies the nameof 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. In order to ensure that your native library can call the function in the log library 
                        native-lib 

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

Then look at the built.gradle file under the module (remember not under the engineering):

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.administrator.myapplication"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

This is generated by default. It is basically gone here, so it is very simple. However, this is to support CMAKE when the new project is built, but this does not conform to our normal development. Many of them are developed in JNI \ NDK in the middle. What should I do? I think we can create a new project, then copy the cmakelist.txt file to our existing projects, then modify the configuration, and edit it again. Is it very witty QAQ?

Everyone is sitting, watch my performance ~
First of all, create a new C folder (in fact, it doesn’t matter, because I will write C language under the folder later, so it is named C)
Then, create a new C file, C’s suffix is .c and C ++ is .cpp
native-lib.c code:

#include <jni.h>

jstring Java_com_example_administrator_kotlinapp_ui_JniActivity_sayHello(JNIEnv* env,jobject jobj){
          char* text="I am from C";
          return (*env)->NewStringUTF(env,text);
 }

If you don’t understand, you want to make up for C language T^T

Then we copy the cmakelist.txt file, as follows:
这里写图片描述

Note:The path of the source file must be correct

Look again at Build.gradle:

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.administrator.kotlinapp"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }

        ndk {
            // Specifies the ABI configurations of your native
            // libraries Gradle should build and package with your APK.
            abiFilters 'armeabi', 'x86'
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

This way we can call the C language in Java:

public class JniActivity extends AppCompatActivity {
    

     {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jni);
        ((TextView)findViewById(R.id.textView)).setText(sayHello());
    }

    public native String sayHello();
}

At this time, you can see the generated so library under Rebuild:
这里写图片描述

source

Random Posts

spring02 core annotation, Bean, Primary, Dependson, Lazy, Scope, Componentscan, detailed Import, LOOOKUP annotation

[java] -Base-thread

Calculate the depth and width of the binary tree

Three -dimensional human gesture Estimated annual progress review (Professor Zhou Xiaowei) Highlight

Ruby basic knowledge (1)