Android Gradle plugin 7.0.0 and NDK: UnsatisfiedLinkError

Viewed 587

I recently updated Android Gradle Plugin to version 7.0.0 (Gradle version 7.0.2). Since I did this update, my native library continues to be compiled regularly, but no .so files are generated in my final apk.

In fact, running the app the exception is thrown:

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"

By downgrading to Android Gradle Plugin version 4.2.2 (Gradle version 6.7.1), everything works fine.

Could it be an Android Gradle Plugin bug or is it my mistake?

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");
    }
2 Answers

Had the same issue, gradle plugin version 7.0.2 fixed this

Are you using tasks.whenTaskAdded in your gradle files? There is an issue with gradle plugin 7.0.0. Refer this. Use 7.0.2 or greater for the resolution.

Related