Flutter项目中找不到.so问题

项目出现couldn't find "libflutter.so"的报错
原因:
项目中使用了so库或者第三方SDK用了so库,Flutter 不能在apk打包时同时引入arm32和arm64的libflutter.so。
默认的打包命令flutter build apk只出现在“armeabi-v7a”文件夹下,这样打包出来的apk是不支持ARMv8的cpu的,所以在arm64的设备中出现couldn't find "libflutter.so"的报错。

解决办法:
参考Android abi与CPU的支持关系表


image.png

flutter 提供了命令来之分别对arm32和arm64分别打包:

flutter build apk --target-platform=android-arm

flutter build apk --target-platform=android-arm64

经过测试发现“flutter build apk --target-platform=android-arm64”打包出来的apk中flutter.so只包含在arm64-v8a的包中,这样armeabi-v7a的硬件就不能打开这个app了。
这样做系统会认为对arm64做了适配,直接获取arm64-v8a下的so文件。
导致报错如下:

2020-08-28 14:31:16.583 13626-13626/com.***.plugin_example E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.***.plugin_example, PID: 13626
    java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.***.plugin_example-OwURoshNkcL3JJTAEXJKmA==/base.apk"],nativeLibraryDirectories=[/data/app/com.***.plugin_example-OwURoshNkcL3JJTAEXJKmA==/lib/arm64, /data/app/com.***.plugin_example-OwURoshNkcL3JJTAEXJKmA==/base.apk!/lib/arm64-v8a, /system/lib64, /hw_product/lib64, /system/product/lib64, /prets/lib64]]] couldn't find "libAndroidIDCard.so"
        at java.lang.Runtime.loadLibrary0(Runtime.java:1067)
        at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
        at java.lang.System.loadLibrary(System.java:1668)
        at kernal.idcard.android.IDCardAPI.(IDCardAPI.java:10)
        at com.***.sdk.recognition.RecognitionService$recogBinder.(RecognitionService.java:663)
        at com.***.sdk.recognition.RecognitionService.onCreate(RecognitionService.java:147)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4634)
        at android.app.ActivityThread.access$2800(ActivityThread.java:308)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2330)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:219)
        at android.app.ActivityThread.main(ActivityThread.java:8347)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
2020-08-28 14:31:16.592 13626-13626/com.***.plugin_example I/Process: Sending signal. PID: 13626 SIG: 9

最终解决办法,不针对arm64做适配,在app的build.gradle文件中配置如下:

defaultConfig {
       ...
        ndk {
            abiFilters 'armeabi-v7a'
        }
}

前提条件是:项目中引用的第三方库有armeabi-v7a的so.

你可能感兴趣的:(Flutter项目中找不到.so问题)