Getting Started with NDK Development

When the project requires C/C++ intervention, the Android project uses NDK (Native Development Kit) development

There are two cases
1. Create a new project
2. Newly add NDK development to an existing project

create new project

When using Android studio to create a project, you can choose NDK development project, the editor will automatically configure the environment for you, and there will also be a sample code, the steps are as follows:

first step
insert image description here
click next
insert image description here

After the creation is complete, the project directory structure is as follows. By
insert image description here
default, a cpp is created in the main directory to store C++ code, and the key CmakeLists.txt file is also here.
The CmakeLists file must have this name, which is fixed and the content is as follows

insert image description here
explain what's inside

add_library( # 设置so库的名称,随意起名
    myndkapplication

    # 设置lib的权限.
    SHARED

    # 关连c++文件.这里必须是绝对路径,跟标签是CMakeLists.txt存放位置,由于此时位置和native-lib.cpp同级,所以这里直接名称即可
    native-lib.cpp)

The key is add_library, the latter two are needed when using the Android library, here is the associated log library, not used much, the log is enough, no explanation

The generated native-lib.cpp file is to write the native code C++ file
insert image description here
How to use:
first statically import the so library

 companion object {
    // Used to load the 'myndkapplication' library on application startup.
    init {
        System.loadLibrary("myndkapplication")
    }
}

The library name is the name defined in the CmakeLists file

Then define the native method

external fun stringFromJNI(): String

This is defined in kotlin, and the following is the java definition method

public static native String stringFromJNI();

Then you can directly call the native method in the Android code
insert image description here

What methods need to be added later, the same process
For example, to define a getUserName method, first define the native method
insert image description here
first in the Android code, and then click the alt+enter shortcut key to automatically create the C++ method
insert image description here

Add native methods to existing projects

If an existing non-NDK project wants to intervene in C++ code, first configure the development environment

insert image description here
Download NDK and CMake

Create a CMakeLists.txt file.
The file name must be this name, and the location can be placed anywhere in the app directory. However, when gradle is configured, the path should correspond to the storage location here.
Place it in the same directory as the app.

insert image description here
gradle configuration

	  externalNativeBuild {
    cmake {
        path = file("CMakeLists.txt")
    }
}

If CMakeLists.txt is placed in the src directory, it is

	  externalNativeBuild {
    cmake {
        path = file("src/CMakeLists.txt")
    }
}

Create c++ class file

Here, create a cpp directory under main, and then put the c++ file into the directory
insert image description here
CMakeLists file to write configuration information

cmake_minimum_required(VERSION 3.10.2)//添加版本

	
add_library(
myndkapp//自己定义的名称
 SHARED//so的类型
src/main/cpp/firstNdk.cpp
) 

After the environment is configured, it is time to write the c++ method, which is introduced above and will not be repeated

Guess you like

Origin blog.csdn.net/ligaoyuan8030/article/details/123575306