从3.6更新到Android Studio 4.0后,使用NDK支持构建项目时出错
将Android Studio更新为4.0项目后,构建完成并出现错误
After updating Android Studio to 4.0 project build finishes with error
使用操作系统独立的路径'lib/armeabi-v7a/libdlib.so'找到了多个文件.如果您正在使用jniLib和CMake导入目标,请参见 https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake
该链接指向带有 Android Studio Preview中的新功能(为4.1
The link leads to the page with New features in Android Studio Preview which is 4.1
EDIT Actually you can find information that is linked in Google cache: Automatic packaging of prebuilt dependencies used by CMake What is stated there is:
Android Gradle插件的早期版本要求您使用jniLibs显式打包CMake外部本机内部版本使用的所有预构建库.使用Android Gradle Plugin 4.0时,不再需要上述配置,并且会导致构建失败:
Prior versions of the Android Gradle Plugin required that you explicitly package any prebuilt libraries used by your CMake external native build by using jniLibs. With Android Gradle Plugin 4.0, the above configuration is no longer necessary and will result in a build failure:
但对我而言并非如此
这是 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})
好,所以我找到了解决方案,我将其与本机库一起添加到了模块中:
Ok, So I have found the solution, I have added this to the module with my native libraries:
packagingOptions {
pickFirst "**/libdlib.so"
}
我不喜欢它,因为它可以解决后果,而不是根本原因.如果有人有更好的解决方案,请在此处发布.
I don't like it as it, as it fixes the consequences, not the root cause. If somebody has a better solution please post it here.
另一个可行的解决方案在@GavinAndre answer 中指出要点是,如果您使用的是Cmake,请不要将 .so
存储在 jniLibs
文件夹中.
Another solution that worked is pointed in @GavinAndre answer
The main point is that if you are using Cmake, then don't store your .so
in jniLibs
folder.
将它们移动到另一个文件夹,例如 cmakeLibs
.
例如:
Move them to another folder for example cmakeLibs
.
For example:
set_target_properties( dlib
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../cmakeLibs/${ANDROID_ABI}/libdlib.so )