赞
踩
CMake 构建脚本是一个纯文本文件,您必须将其命名为 CMakeLists.txt。
注意:您可以在所需的任何位置创建构建脚本。不过,在配置 build 脚本时,原生源代码文件和库的路径将与 build 脚本的位置相关。
推荐位置:src/main/cpp/CMakeLists.txt
# Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.18.1) # Declares and names the project. project("MyNatives") # 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. ${PROJECT_NAME} # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). MyNatives.cpp Calculate.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. ${PROJECT_NAME} # Links the target library to the log library # included in the NDK. ${log-lib})
1、右键点击您想要关联到原生库的模块(例如 app 模块),然后从菜单中选择 Add C++ to Module,如下图所示:
2、然后您将会看到类似于下图的对话框:
3、如果选择的 module 已有 CMake 配置请选择第二项(Link an existing Cmakelists.txt…)并指定文件位置 ,反之选择第一项(Create CMakeListes.txt…),然后点击 OK。
如需手动配置 Gradle 以关联到您的原生库,您需要将 externalNativeBuild 块添加到模块级 build.gradle 文件中,并使用 cmake 或 ndkBuild 块对其进行配置:
android { ... defaultConfig {...} buildTypes {...} // Encapsulates your external native build configurations. externalNativeBuild { // Encapsulates your CMake build configurations. cmake { // Provides a relative path to your CMake build script. path "src/main/cpp/CMakeLists.txt" } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。