FlutterBoost 3.0出现 Activity无法转换为ExclusiveAppComponent<Activity>的解决办法

Posted 一叶飘舟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FlutterBoost 3.0出现 Activity无法转换为ExclusiveAppComponent<Activity>的解决办法相关的知识,希望对你有一定的参考价值。

在使用FlutterBoost 3.0过程中,一不小心升级了flutter的版本,会出现下面问题:

/flutter_boost-master/android/src/main/java/com/idlefish/flutterboost/containers/FlutterBoostActivity.java:167: 错误: 不兼容的类型: Activity无法转换为ExclusiveAppComponent
getFlutterEngine().getActivityControlSurface().attachToActivity(getActivity(), getLifecycle());
 

这是由于新版本的flutter 改动了api的原因,导致attachToActivity里面的参数转换出错;

void attachToActivity(
      @NonNull ExclusiveAppComponent<Activity> exclusiveActivity, @NonNull Lifecycle lifecycle);

flutterboost官方给出的临时解决方案如下:

diff --git a/android/src/main/java/com/idlefish/flutterboost/containers/FlutterBoostActivity.java b/android/src/main/java/com/idlefish/flutterboost/containers/FlutterBoostActivity.java
index ab28533..1608eb5 100644
--- a/android/src/main/java/com/idlefish/flutterboost/containers/FlutterBoostActivity.java
+++ b/android/src/main/java/com/idlefish/flutterboost/containers/FlutterBoostActivity.java
@@ -152,7 +152,7 @@ public class FlutterBoostActivity extends FlutterActivity implements FlutterView
     private void performAttach() 
         if (!isAttached) 
             // Attach plugins to the activity.
-            getFlutterEngine().getActivityControlSurface().attachToActivity(getActivity(), getLifecycle());
+            getFlutterEngine().getActivityControlSurface().attachToActivity(delegate, getLifecycle());
 
             if (platformPlugin == null) 
                 platformPlugin = new PlatformPlugin(getActivity(), getFlutterEngine().getPlatformChannel());
diff --git a/android/src/main/java/com/idlefish/flutterboost/containers/FlutterBoostFragment.java b/android/src/main/java/com/idlefish/flutterboost/containers/FlutterBoostFragment.java
index b4ead66..5f954e6 100644
--- a/android/src/main/java/com/idlefish/flutterboost/containers/FlutterBoostFragment.java
+++ b/android/src/main/java/com/idlefish/flutterboost/containers/FlutterBoostFragment.java
@@ -43,6 +43,17 @@ public class FlutterBoostFragment extends FlutterFragment implements FlutterView
     private boolean isAttached = false;
     private boolean isFinishing = false;
 
+    class FakeExclusiveAppComponent implements io.flutter.embedding.android.ExclusiveAppComponent<Activity> 
+        public void detachFromFlutterEngine() 
+            // do nothing.
+        
+
+        public Activity getAppComponent() 
+            return getActivity();
+        
+    
+    private FakeExclusiveAppComponent fakeAppComponent = new FakeExclusiveAppComponent();
+
     // @Override
     public void detachFromFlutterEngine() 
         /**
@@ -303,7 +314,7 @@ public class FlutterBoostFragment extends FlutterFragment implements FlutterView
     private void performAttach() 
         if (!isAttached) 
             // Attach plugins to the activity.
-            getFlutterEngine().getActivityControlSurface().attachToActivity(getActivity(), getLifecycle());
+            getFlutterEngine().getActivityControlSurface().attachToActivity(fakeAppComponent, getLifecycle());
 
             if (platformPlugin == null) 
                 platformPlugin = new PlatformPlugin(getActivity(), getFlutterEngine().getPlatformChannel());

简化下就是:

FlutterBoostActivity中:

//getFlutterEngine().getActivityControlSurface().attachToActivity(getActivity(), getLifecycle());
            getFlutterEngine().getActivityControlSurface().attachToActivity(delegate, getLifecycle());

FlutterBoostFragment中:

//getFlutterEngine().getActivityControlSurface().attachToActivity(getActivity(), getLifecycle());
            getFlutterEngine().getActivityControlSurface().attachToActivity(fakeAppComponent, getLifecycle());



class FakeExclusiveAppComponent implements io.flutter.embedding.android.ExclusiveAppComponent<Activity> 
        public void detachFromFlutterEngine() 

        public Activity getAppComponent() 
                return getActivity();
        
    
private FakeExclusiveAppComponent fakeAppComponent = new FakeExclusiveAppComponent();

 相关链接:

[android] Failed to call |ActivityControlSurface.attachToActivity| due to the parameter type change · Issue #93905 · flutter/flutter · GitHub

https://github.com/alibaba/flutter_boost/issues/1451

以上是关于FlutterBoost 3.0出现 Activity无法转换为ExclusiveAppComponent<Activity>的解决办法的主要内容,如果未能解决你的问题,请参考以下文章

「FlutterBug」FlutterBoost启动黑屏处理(升级空安全版本)

「FlutterBug」FlutterBoost启动黑屏处理(升级空安全版本)

「FlutterBug」FlutterBoost启动黑屏处理(升级空安全版本)

Flutter Boost 3.0混合开发

Flutter 专题79 图解 Android Native 集成 FlutterBoost 小尝试 #yyds干货盘点#

FlutterBoost3.0发布preview版本