在更新Android Studio到4.0后,项目构建完成后出现以下错误
发现不止一个与操作系统无关的文件,路径为'libarmeabi-v7alibdlib.so'。如果你正在使用 jniLibs 和 CMake IMPORTED 目标,请参见 https:/developer.android.comstudiopreviewfeatures#automatic_packaging_of_prebuilt_dependencies_used_by_cmake。
该链接指向的页面有 Android Studio预览版的新功能 这是4.1
编辑其实你可以找到谷歌缓存中链接的信息。CMake所使用的预制依赖的自动打包。那里说的是。
之前版本的Android Gradle Plugin要求你通过使用jniLibs显式打包任何CMake外部原生构建所使用的预建库。在Android Gradle Plugin 4.0中,上述配置不再需要,并且会导致构建失败。
但我的情况并非如此
以下是 build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
externalNativeBuild {
cmake {
cFlags "-O3"
cppFlags "-std=c++11 -frtti -fexceptions -mfpu=neon"
arguments "-DANDROID_PLATFORM=android-16",
"-DANDROID_TOOLCHAIN=clang",
"-DANDROID_STL=c++_shared",
"-DANDROID_ARM_NEON=TRUE",
"-DANDROID_CPP_FEATURES=rtti exceptions"
}
}
}
buildTypes {
debug {}
stage {
debuggable true
minifyEnabled false
}
release {
minifyEnabled false
}
}
kotlinOptions {
jvmTarget = "1.8"
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
packagingOptions {
pickFirst "**/libc++_shared.so"
pickFirst "**/libdlib.so"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.annotation:annotation:1.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
和 CMakeLists.txt
set(LIB_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs)
#
cmake_minimum_required(VERSION 3.4.1)
add_library(dlib SHARED IMPORTED)
# sets the location of the prebuilt dlib .so
set_target_properties( dlib
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libdlib.so )
# ------------------------------------------------------------------
add_library( # Sets the name of the library.
face-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
face-lib.cpp)
target_include_directories(
face-lib PRIVATE
${CMAKE_SOURCE_DIR}/include
)
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)
target_link_libraries( # Specifies the target library.
face-lib
dlib
# Links the target library to the log library
# included in the NDK.
${log-lib})
好的,我找到了解决方案,我把这个和我的本地库一起添加到了模块中。
packagingOptions {
pickFirst "**/libdlib.so"
}
我不喜欢这样,因为它只是解决了后果,而不是根本原因。如果有人有更好的解决方案,请在这里发布。
我也遇到了同样的问题。
我的gradle文件是这样写的。
sourceSets {
main {
jniLibs.srcDirs 'src/main/cpp/libs'
}
}
事实上,文件夹里有两个.so文件,而且由于这个链接... ... see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake
似乎显示了信息 那个Andrioid Stuido会自动为你打包liBs。.
所以我只是 删去 这些内容在我的gradle文件中,一切都很好。
在我这边,似乎是jniLibs作为文件夹的名字错误地触发了错误。在文件系统和cmakelists.txt的路径中把文件夹的名字改成其他的名字(我用了 "库")就解决了这个问题。
cmakelists.txt片段
# import library and set path
add_library(ixxs-plugin SHARED IMPORTED) # or STATIC instead of SHARED
set_target_properties(ixxs-plugin PROPERTIES
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/../libraries/${CMAKE_ANDROID_ARCH_ABI}/libixxs-plugin.so"
)
不需要在gradle文件上做任何事情,它将自动找到lib并把它们放到aar文件中,你可以解压aar文件来检查。(lib在{nameofaar}jni{arch_type}{nameoflib}.so中)