Android 开发 NDK 调用第三方so以及编译自己的so时,
报错:2 files found with path 'lib/arm64-v8a/libaesutil.so'. 几费周折解决了,记录下。
我的工程结构如图,在独立的module里写native层代码,依赖了一个外部的so , 并且通过cmake 编译自己的cpp文件和外部依赖的so.
cmake文件如下:
#构建库文件所需要CMake最小版本
cmake_minimum_required(VERSION 3.4.1)
# 设置生成的so动态库输出的路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/jniLibs/${ANDROID_ABI})
#添加自己的C/C++源文件
add_library( # Sets the name of the library.
native_test
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native_test.cpp)
#添加依赖的NDK 库
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 )
#添加外部依赖的so
add_library(
aesutil
SHARED
IMPORTED)
set_target_properties(
aesutil
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libaesutil.so
)
include_directories(src/main/cpp/include)
#将目标库与NDK 中的库链接
target_link_libraries( # Specifies the target library.
native_test
# Links the target library to the log library
# included in the NDK.
aesutil
${log-lib} )
自己的nativelib module的build.gradle:
android {
compileSdkVersion 30
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
//cmake ndk
externalNativeBuild {
cmake {
cppFlags ""
arguments "-DANDROID_STL=c++_shared"
}
}
//选择生成的CPU 架构, android defaultConfig下定义
ndk{
abiFilters "armeabi-v7a", "arm64-v8a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
//cmake ndk
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
整个项目Android Gradle Plugin 是4.2.2版本。
网络上搜索了不少帖子,无效。
最后在自己的nativelib module的build.gradle 的android{}加上
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
重新 make project 解决。