Android Gradle 插件 7.0.0 和 NDK:UnsatisfiedLinkError

Android Gradle plugin 7.0.0 and NDK: UnsatisfiedLinkError

我最近将 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}
       )

Activity:

static {
        System.loadLibrary("f-native-lib");
    }

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

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