android原生集成flutter.aar报错:Failed
2020-01-12 本文已影响0人
Duzzi
问题
flutter
工程如果依赖了第三方插件或本地插件
,打包成aar
集成到Android原生项目中会报Failed to call observer method
,详细日志如下:
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.MainActivity}: java.lang.RuntimeException: Failed to call observer method
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
原因
用
flutter build aar
或者flutter build apk
打包aar
的时候并没有将第三方插件
打包到flutter-release.aar
里面
解决
一、初级解决
- 首先使用命令
flutter build aar
image.png此时flutter工程和依赖的第三方插件会在flutter工程根目录下的build/host/outputs/repo下分别生成aar,如图所示
- 然后将这些
aar
拷贝到Android原生工程进行依赖即可
二、高级解决
利用fat-aar
将这些aar
合并打包成一个aar
参考flutter1.8打包aar上传maven集成到原生android工程这篇文章对打包脚本做了一个简化。
脚本及工程demo:
https://github.com/duzzi/flutter_lib_test
我的运行环境:
os:macOs
gradle plugin: classpath 'com.android.tools.build:gradle:3.2.1'
gradle:gradle-4.10.2-all
java:jdk1.8
flutter:stable, v1.9.1
ps:需要保证flutter工程中.android
下的gradle
、support
包、minSdkVersion
等版本一致。
thanks:
https://www.jianshu.com/p/2258760e9540
https://juejin.im/post/5cd51f61f265da039c056e39
https://github.com/CarGuo/flutter_app_lib