开发环境: mac OS:11.4 Big Sur JDK:11.0.14 下载地址 亲测1.8版本会报异常 Andriod Stdio:3.6.3 下载地址 Flutter:2.5.3 下载地址 注意不要下载最新版会有坑,见issues 1451
- 配置flutter+andriod开发环境:
1.1 解压后配置环境变量。 1.2 执行flutter doctor。检测环境配置。 1.3 Andriod Stdio安装Flutter插件。 - 创建Andriod空项目,测试空项目启动。
- 在项目下,新增flutter module。
3.1 找到 flutter_module/pubspec.yaml 目录,在dependencies下配置flutter_boost。dependencies:
flutter:
sdk: flutter
flutter_boost:
git:
url: 'https://github.com/alibaba/flutter_boost.git'
ref: 'v3.0-release.1'
cupertino_icons: ^1.0.2
3.2 在flutter_module目录下执行:flutter build apk 命令。会发生以下异常 这是由于新版本的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(delegate, getLifecycle());
FlutterBoostFragment中:
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();
FlutterTextureHooker 中:
flutterRenderer.startRenderingToSurface(surface,false);
再次执行,flutter build apk 出现:Built build/host/outputs/apk/release/app-release.apk (18.0MB). 编译成功。
- 回到AndriodStdio打开目录,将flutter_boost加入到app中。
配置SDK和NDK
|