Talha Balaj
Talha Balaj

Reputation: 319

native dependencies don't get copied to their destination in flutter plugin

I'm creating a Flutter Plugin that uses native libraries. The problem I am facing is, The native dependencies I added don't get copied to Android's context.nativeLibraryDir. Following are my configurations.

This is my file structure for the android folder of the plugin (not the example app).
File Structure

In the build.gradle of my the plugin, I have the following. I also tried added jni.srcDirs = [] after researching, but it didn't work either.

sourceSets {
    main {
       jniLibs.srcDirs += 'src/main/jniLibs'
       java.srcDirs += 'src/main/kotlin'
    }
}

I think all of the above is correct, because the following is my APK (of the example app) that is built, and it has the libraries. Apk analysis

But after the example app is installed (on Android x86 Emulator, also tested on arm64 device), It crashes when I try to execute the native libs, I want to use them as executables (It worked in a standalone android project, now I just copied the libs & some code in the plugin)

no libs found

The interesting thing I found, flutter's libflutter.so don't get copied either which I guess is required for proper functionality. The app should crash without libflutter.so, but it doesn't.

I have also tried adding ndk.abiFilters to the example app's build.gradle but no luck.

Upvotes: 1

Views: 1262

Answers (1)

Lukas Zaugg
Lukas Zaugg

Reputation: 101

Just stumbled upon the same problem and found the following solution in my case (Flutter plugin with native libs):

android/src/main/AndroidManifest.xml

...
<application
  android:extractNativeLibs="true"
  ...
>
...

Details:

Since Android 6.0 system libs can be stored uncompressed within the APK and do not have to be extracted and stored somewhere else (https://medium.com/androiddevelopers/smallerapk-part-8-native-libraries-open-from-apk-fc22713861ff). Loading system libs within the APK is supported through (Java):

static { System.loadLibrary(..); }

see https://developer.android.com/reference/java/lang/System#loadLibrary(java.lang.String).

If an extraction of the system libs for the Flutter plugin is required (e.g. because you cannot just build upon System.loadLibrary), one can use the android:extractNativeLibs="true" in the plugin's Manifest application element (https://developer.android.com/guide/topics/manifest/application-element).

Important to note, that the DSL way of doing it (https://developer.android.com/studio/releases/gradle-plugin?buildsystem=ndk-build#compress-native-libs-dsl) doesn't work in my case (Flutter plugin).

Upvotes: 1

Related Questions