问题
After updating Android Studio to 4.0 project build finishes with error
More than one file was found with OS independent path 'lib/armeabi-v7a/libdlib.so'. If you are using jniLibs and CMake IMPORTED targets, see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake
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:
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:
But it is not the case for me
Here are 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"
}
and 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})
回答1:
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.
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.
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 )
回答2:
I faced the same problem.
That is how my gradle file written:
sourceSets {
main {
jniLibs.srcDirs 'src/main/cpp/libs'
}
}
Actually there is two .so files in the folder and since the link see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake
seems to show the infomation That Andrioid Stuido will automatic packaging libs for you.
So I just DELETE this content inner my gradle file and everything works fine.
回答3:
On my side seemed that jniLibs as name of the folder was triggering erroneously the error. changing the name of the folder to something else (i used 'libraries') both in the path of filesystem and the cmakelists.txt solved the problem.
cmakelists.txt fragment
# 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"
)
nothing had to be done on the gradle file, it will find automatically the libs and put them into the aar file. you can unzip the aar file to check that. (libs are in {nameofaar}/jni/{arch_type}/{nameoflib}.so)
回答4:
According to https://developer.android.com/studio/projects/gradle-external-native-builds#jniLibs
If you are using Android Gradle Plugin 4.0, move any libraries that are used by IMPORTED CMake targets out of your jniLibs directory to avoid this error.
So you only need to move the ${ANDROID_ABI}/libdlib.so
folder to another place such as creating a new directory name cmakeLibs
eg:
set_target_properties( dlib
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../cmakeLibs/${ANDROID_ABI}/libdlib.so )
来源:https://stackoverflow.com/questions/62088079/error-when-building-project-with-ndk-support-after-updating-to-android-studio-4