JNI初识
1.创建C/C++项目
https://developer.android.google.cn/studio/projects/add-native-code?authuser=0&hl=hu#new-project
2.配置cmake文件
https://developer.android.google.cn/studio/projects/configure-cmake?authuser=0&hl=hu
3.CMakeLists.txt文件详解
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.# Sets the minimum CMake version required for this project.
#[翻译]
#有关在Android Studio中使用CMake的更多信息,请阅读文档:https://d.android.com/studio/projects/add-native-code.html.
#有关如何使用CMake的更多示例,请参阅https://github.com/android/ndk-samples.#设置此项目所需的最低CMake版本。
cmake_minimum_required(VERSION 3.22.1)# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
#[翻译]
#声明项目名称。
#项目名称可以通过${project_name}访问。由于这是顶级CMakeLists.txt,因此项目名称也可以
#通过${CMAKE_project_name}访问(这两个CMAKE变量在顶级构建脚本范围内是同步的)。
project("nativec")# 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.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
#[翻译]
#创建并命名一个库,将其设置为STATIC或SHARED,并提供其源代码的相对路径。
#您可以定义多个库,CMake会为您构建它们。
#Gradle会自动将共享库与APK打包在一起。
#在这个顶级CMakeLists.txt中,${CMAKE_PROJECT_NAME}用于定义目标库名称;
#在子模块的CMakeLists.txt中,出于同样的目的,${PROJECT_NAME}是首选。
#为了从Java/Kotlin将库加载到您的应用程序中,您必须调用System.loadLibrary()
#并传递此处定义的库的名称;对于GameActivity/NativeActivity派生的应用程序,
#AndroidManifest.xml文件中必须使用相同的库名称。
add_library(${CMAKE_PROJECT_NAME} SHARED# List C/C++ source files with relative paths to this CMakeLists.txt.native-lib.cpp)# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
#[翻译]
#指定CMake应链接到目标库的库。您可以链接来自不同来源的库,例如此构建脚本中
#定义的库、预构建的第三方库或Android系统库。
target_link_libraries(${CMAKE_PROJECT_NAME}# List libraries link to the target libraryandroidlog)