从 3.6 更新到 Android Studio 4.0 后构建支持 NDK 的项目时出错 [英] Error when building project with NDK support after updating to Android Studio 4.0 from 3.6

查看:35
本文介绍了从 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".如果您使用 jniLibs 和 CMake IMPORTED 目标,请参阅 https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake

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

该链接指向带有 Android Studio Preview 中的新功能的页面,即 4.1

The link leads to the page with New features in Android Studio Preview which is 4.1

编辑其实你可以在谷歌缓存中找到链接的信息:CMake 使用的预构建依赖项的自动打包那里说的是:

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 )

这篇关于从 3.6 更新到 Android Studio 4.0 后构建支持 NDK 的项目时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆