Android Gradle 插件 7.0.0 和 NDK:UnsatisfiedLinkError

Android Gradle plugin 7.0.0 and NDK: UnsatisfiedLinkError

提问人:Ettore Gallina 提问时间:8/2/2021 更新时间:1/15/2022 访问量:712

问:

我最近将 Android Gradle 插件更新到了 7.0.0 版(Gradle 版本 7.0.2)。 自从我做了这个更新后,我的原生库继续定期编译,但我的最终apk中没有生成.so文件。

事实上,运行应用程序会引发异常:

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/it.Ettore.raspcontroller-2/base.apk"],nativeLibraryDirectories=[/data/app/it.Ettore.raspcontroller-2/lib/x86, /data/app/it.Ettore.raspcontroller-2/base.apk!/lib/x86, /system/lib, /vendor/lib]]] couldn't find "libf-native-lib.so"

降级到 Android Gradle 插件版本 4.2.2(Gradle 版本 6.7.1),一切正常。

这可能是 Android Gradle 插件错误还是我的错误?

build.gradle

android {

    defaultConfig {

        externalNativeBuild {
            cmake {
                cFlags "-fvisibility=hidden"
                cppFlags "-fvisibility=hidden"
            }
        }

        ndk {
            moduleName "f-native-lib"
        }
        sourceSets.main {
            jni.srcDirs = ['src/main/c']
        }
    }


    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

            ndk {
                debugSymbolLevel 'SYMBOL_TABLE'
            }
        }
    }


    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

CMakeList:.txt:

cmake_minimum_required(VERSION 3.4.1)

add_library(    # Sets the name of the library.
                f-native-lib

                # Sets the library as a shared library.
                SHARED

                # Provides a relative path to your source file(s).
                src/main/c/mydir/myfile.c
                )


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
              )

find_library( # Sets the name of the path variable.
            z-lib

            # Specifies the name of the NDK library that you want CMake to locate.
            z
            )

target_link_libraries( # Specifies the target library.
       f-native-lib

       # Links the target library to the log library included in the NDK.
       ${log-lib}
       ${z-lib}
       )

活动:

static {
        System.loadLibrary("f-native-lib");
    }
Android cmake android-ndk android-gradle-plugin

评论

0赞 Michael 8/3/2021
是否尝试过显式列出要构建的 ABI 以使用 abiFilters
0赞 Ettore Gallina 8/3/2021
是的,@Michael,我试图添加,但不幸的是问题仍然存在。abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
0赞 Rene 8/4/2021
同样的问题在这里。Gradle 4.2.2 一切正常,但在升级到 7.0.0 后,我遇到了同样的错误。
0赞 xmjx 8/9/2021
同样在这里。降级 Gradle 可以解决这个问题。谢谢,@Rene的提示。
0赞 Orange 9/13/2021
你好!你有关于这个问题的更新吗?似乎 Gradle 插件 7 只构建了我的原生库的 32 位版本。Gradle 插件 4.2.2 未发现任何问题

答:

1赞 eSDe 9/14/2021 #1

有同样的问题,gradle 插件版本 7.0.2 解决了这个问题

评论

0赞 javdromero 9/15/2021
这并不能真正回答这个问题。如果您有其他问题,可以点击“提问”提问。要在该问题获得新答案时收到通知,您可以关注此问题。一旦你有足够的声誉,你还可以添加赏金来吸引更多人关注这个问题。- 来自评论
0赞 Anuvrat Chitranshi 1/15/2022 #2

您是否在 gradle 文件中使用 tasks.whenTaskAdded?gradle 插件 7.0.0 存在问题。参考这个。使用 7.0.2 或更高版本作为分辨率。

评论

0赞 Ettore Gallina 1/15/2022
是的,实际上我正在使用 tasks.whenTaskAdded
0赞 Anuvrat Chitranshi 1/19/2022
这就是 7.0.2 补丁发行说明(上面给出的链接)中提到的此错误的原因。