flutter run lost libflutter.so when using abiFilters
Reproduce
Add abiFilters to android/app/build.gradle
:
1 | android { |
Run command:
1 | $ flutter run |
Flutter app crashed by java.lang.UnsatisfiedLinkError
:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file “/data/app/xxxx/base.apk”],nativeLibraryDirectories=[/data/app/xxxx/lib/arm, /data/app/xxxx/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib, /product/lib]]] couldn’t find “libflutter.so”
at java.lang.Runtime.loadLibrary0(Runtime.java:1011)
at java.lang.System.loadLibrary(System.java:1660)
at io.flutter.view.FlutterMain.startInitialization(FlutterMain.java:156)
at io.flutter.view.FlutterMain.startInitialization(FlutterMain.java:133)
at io.flutter.app.FlutterApplication.onCreate(FlutterApplication.java:22)…
As you can see, flutter run
seems like forgot to put “libflutter.so” into apk.
Why?
Run command for log details flutter run —verbose
, and then following logs show me the reason:
…
[ +7 ms] Building APK
[ +17 ms] Running Gradle task ‘assembleDebug’…
[ +1 ms] executing: [/flutter/] /flutter/android/gradlew -Pverbose=true -Ptarget=/flutter/lib/main.dart-Ptrack-widget-creation=false -Pcompilation-trace-file=compilation.txt -Ptarget-platform=android-arm64 assembleDebug
…
Flutter detected my Android phone is arm64 device, so it executes gradle task assembleDebug
with -Ptarget-platform=android-arm64
.
And flutter.gradle
($flutterRoot/packages/flutter_tools/gradle/flutter.gradle) will take this property to find flutter engine prebuilt binary output which named flutter.jar
:
1 | ... |
Solution
Run with specified target-platform
to workaround (flutter version < v1.5.x):
1 | $ flutter run --target-platform=android-arm |
And, furthermore, the option target-platform
has been removed when updated to v1.7.x. (see #34369 )
In this case, You can run build apk
before run
:
1 | $ flutter build apk --debug --target-platform=android-arm |