Flutter 81: 图解 Android Native 集成 FlutterBoost 小尝试 (三)

  1. 云栖社区>
  2. 博客>
  3. 正文

Flutter 81: 图解 Android Native 集成 FlutterBoost 小尝试 (三)

阿策~ 2020-03-28 21:22:12 浏览715

      小菜在一个历史项目中接入了 Flutter Module 并采用 FlutterBoost 作为 Platform Channel 桥接;但实际开发遇到很多问题,仅记录两个印象深刻的小问题;

问题一:.so 文件混淆

问题分析

      小菜的历史项目使用的 NDKarmeabi 而接入 Flutter 之后需要使用 armeabi-v7a,小菜在项目中添加 armeabi-v7a 对应的 .so 文件,使 NDK 支持 armeabi-v7a,小菜测试直接允许或 debug 包是正常的,而 release 包直接崩溃,提示 libc.so 找不到;

** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'Xiaomi/kenzo/kenzo:6.0.1/MMB29M/V8.5.4.0.MHOCNED:user/release-keys'
Revision: '0'
ABI: 'arm'
pid: 2380, tid: 2380, name: com.ace.test  >>> com.ace.test <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: '[FATAL:flutter/shell/platform/android/library_loader.cc(24)] Check failed: result. 
'
    r0 00000000  r1 0000094c  r2 00000006  r3 f748cb7c
    r4 f748cb84  r5 f748cb34  r6 00000002  r7 0000010c
    r8 f517f90c  r9 ab645148  sl 00100019  fp ffc3c58c
    ip 00000006  sp ffc3c3e8  lr f7208f5d  pc f720b358  cpsr 400d0010

backtrace:
    #00 pc 00044358  /system/lib/libc.so (tgkill+12)
    #01 pc 00041f59  /system/lib/libc.so (pthread_kill+32)
    #02 pc 0001ba6f  /system/lib/libc.so (raise+10)
    #03 pc 00018c11  /system/lib/libc.so (__libc_android_abort+34)
    #04 pc 000167d0  /system/lib/libc.so (abort+4)
    #05 pc 0014540b  /data/app/com.ace.test-1/lib/arm/libflutter.so (offset 0x122000)
    #06 pc 0013a091  /data/app/com.ace.test-1/lib/arm/libflutter.so (offset 0x122000) (JNI_OnLoad+764)
    #07 pc 00250143  /system/lib/libart.so (_ZN3art9JavaVMExt17LoadNativeLibraryEP7_JNIEnvRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEP8_jobjectPS9_+1174)
    #08 pc 002c2027  /system/lib/libart.so (_ZN3artL18Runtime_nativeLoadEP7_JNIEnvP7_jclassP8_jstringP8_jobjectS5_+178)
    #09 pc 727b02c5  /data/dalvik-cache/arm/system@framework@boot.oat (offset 0x2465000)

方案解决

      既然只有 release 包有问题,很大可能是混淆文件有问题;小菜查了一些资料将涉及到的 Flutter 文件免混淆,因为小菜对 NDK 等研究还很浅,因此仅提供列出方案;之后打包测试结果正常应用;

#Flutter Wrapper
-dontwarn io.flutter.**
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }

问题二:FlutterBoost 导致 release 包崩溃

问题分析:

      小菜在集成 FlutterBoost 之后,打包 release 包测试时,发现有些手机进退两次应用就会崩溃,小菜也是很崩溃,抓到 Log 提示 Surface 在销毁时空指针;

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.Surface.release()' on a null object reference
    at io.flutter.embedding.android.FlutterTextureView.disconnectSurfaceFromRenderer(SourceFile:198)
    at io.flutter.embedding.android.FlutterTextureView.detachFromRenderer(SourceFile:161)
    at com.idlefish.flutterboost.XFlutterView.detachFromFlutterEngine(SourceFile:713)
    at com.idlefish.flutterboost.containers.FlutterSplashView.onDetach(SourceFile:196)
    at com.idlefish.flutterboost.ContainerRecord.onDisappear(SourceFile:115)
    at com.idlefish.flutterboost.containers.FlutterActivityAndFragmentDelegate.onPause(SourceFile:200)
    at com.idlefish.flutterboost.containers.FlutterFragment.onPause(SourceFile:280)
    at androidx.fragment.app.Fragment.performPause(SourceFile:2879)
    at androidx.fragment.app.FragmentStateManager.pause(SourceFile:373)
    at androidx.fragment.app.FragmentManager.moveToState(SourceFile:1204)
    at androidx.fragment.app.FragmentManager.moveToState(SourceFile:1354)
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(SourceFile:1432)
    at androidx.fragment.app.FragmentManager.moveToState(SourceFile:1495)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(SourceFile:2617)
    at androidx.fragment.app.FragmentManager.dispatchPause(SourceFile:2585)
    at androidx.fragment.app.FragmentController.dispatchPause(SourceFile:280)
    at androidx.fragment.app.FragmentActivity.onPause(SourceFile:419)
    at com.test.ace.BaseActivity.onPause(SourceFile:302)
    at com.test.ace.MainActivity.onPause(SourceFile:360)
    at android.app.Activity.performPause(Activity.java:6415)
    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1312)
    at android.app.ActivityThread.performNewIntents(ActivityThread.java:2588)
    at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2599)
    at android.app.ActivityThread.access$1800(ActivityThread.java:153)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1447)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:5527)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

      小菜分析查阅 FlutterBoost 源码,在 FlutterTexture -> disconnectSurfaceFromRenderer() -> renderSurface 中销毁时报错;

private void disconnectSurfaceFromRenderer() {
    if (flutterRenderer == null) {
      throw new IllegalStateException("disconnectSurfaceFromRenderer() should only be called when flutterRenderer is non-null.");
    }

    flutterRenderer.stopRenderingToSurface();
    renderSurface.release();
    renderSurface = null;
}

      小菜采用的是 Fragment 方式,而使用的方法就是官网中使用的 NewEngineFragmentBuilder() 方式,并没有对生命周期有变更操作;

mFragment = new FlutterFragment.NewEngineFragmentBuilder().url("url").build();

      小菜研究了好几天也没有搞明白,后来请教了一个同事,无意间想到是不是版本不一致导致的;小菜当前采用的 FlutterBoostv1.12.13 而本地 Flutter 版本是 v1.14.6

flutter --version

方案解决:

      小菜猜测可能是 FlutterBoost 未对 Flutter 高版本进行适配,于是小菜准备统一版本尝试一下,即固定当前 projectFlutter 版本为 v1.12.13+hotfix.8

flutter version v1.12.13+hotfix.8

      小菜 clean 之后心惊胆战的打 release 包尝试,在各手机上进行安装测试,一切正常,目前没有出现闪退问题,基本定位为使用的 FlutterBoostFlutter 环境不一致造成的;

flutter clean


      小菜对于 Flutter 的实际开发还很欠缺,遇到很多意想不到的问题,刚处于探索学习阶段,如有错误请多多指导!

来源: 阿策小和尚