创建Android Native Library项目

添加.a文件和头文件

假设这里有一个tboxtest的C/C++项目,

  1. 在cpp文件夹下新建include文件夹(为方便管理而已,文件夹名随意),在include文件夹下创建指向tboxtest项目中存放头文件的文件夹的链接(也可以整个复制过来,但如此同步比较麻烦);
  2. 在cpp文件夹下新建jniLibs文件夹,在jniLibs文件夹下新建arm64-v8a文件夹(如果需要其他架构的.a文件,则添加对应架构的文件夹,如需要armeabi-v7a架构,则添加armeabi-v7a文件夹),在arm64-v8a文件夹下创建指向tboxtest项目编译后的arm64-v8a架构的.a文件的链接(同理也可以复制.a文件过来)。

效果大概如图:

编写CMakeLists.txt文件

  1. 配置头文件路径
include_directories(include)
  1. 配置库文件
add_library(tboxtest STATIC IMPORTED)
set_target_properties(
        tboxtest
        PROPERTIES
        IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libtboxtest.a)
  1. 将库添加的链接目标中
target_link_libraries( # Specifies the target library.
        nativelib2

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

整体上CMakeLists.txt如下:

# 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.

cmake_minimum_required(VERSION 3.18.1)

# Declares and names the project.

project("nativelib2")

include_directories(include)

# 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.
        nativelib2

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        nativelib2.cpp)

add_library(tboxtest STATIC IMPORTED)
set_target_properties(
        tboxtest
        PROPERTIES
        IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libtboxtest.a)

# 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.
        nativelib2

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

如果有多个.a文件,重复上述2和3步骤即可;

启用JNI调试

在引用该模块的项目的build.gradle文件中添加如下代码:

buildTypes {
        release {
            ......
        }
        debug {
            jniDebuggable true
            packagingOptions {
                doNotStrip '**/*.so'
            }
        }
    }