Rom*_*ych 15 android android-ndk android-studio android-studio-4.0
将 Android Studio 更新到 4.0 后,项目构建完成并出现错误
在操作系统独立路径“lib/armeabi-v7a/libdlib.so”中发现了不止一个文件。如果您使用 jniLibs 和 CMake IMPORTED 目标,请参阅https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake
该链接指向带有Android Studio 预览版新功能的页面,即 4.1
编辑 实际上,您可以找到在 Google 缓存中链接的信息: CMake 使用的预构建依赖项的自动打包 说明的是:
以前版本的 Android Gradle 插件要求您使用 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"
}
Run Code Online (Sandbox Code Playgroud)
和 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})
Run Code Online (Sandbox Code Playgroud)
Rom*_*ych 13
好的,所以我找到了解决方案,我已将其添加到带有本机库的模块中:
packagingOptions {
pickFirst "**/libdlib.so"
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢它,因为它解决了后果,而不是根本原因。如果有人有更好的解决方案,请在此处发布。
@GavinAndre回答中指出了另一个有效的解决方案
主要的一点是,如果您使用的是 Cmake,则不要将您.so的jniLibs文件夹存储在文件夹中。
例如,将它们移动到另一个文件夹cmakeLibs。
例如:
set_target_properties( dlib
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../cmakeLibs/${ANDROID_ABI}/libdlib.so )
Run Code Online (Sandbox Code Playgroud)
Jos*_*hua 10
我遇到了同样的问题。
我的 gradle 文件就是这样写的:
sourceSets {
main {
jniLibs.srcDirs 'src/main/cpp/libs'
}
}
Run Code Online (Sandbox Code Playgroud)
实际上文件夹中有两个 .so 文件,因为链接 see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake似乎显示了 Andrioid Stuido 将为您自动打包库的信息。
所以我只是在我的 gradle 文件中删除这个内容,一切正常。
小智 8
根据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.
Run Code Online (Sandbox Code Playgroud)
所以你只需要将${ANDROID_ABI}/libdlib.so文件夹移动到其他地方比如新建一个目录名称cmakeLibs
例如:
set_target_properties( dlib
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../cmakeLibs/${ANDROID_ABI}/libdlib.so )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6653 次 |
| 最近记录: |