Issue
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).
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.
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)
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.
Solution
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).
Answered By - Lukas Zaugg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.