@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// Emulates installation of future on demand modules using SplitCompat.
SplitCompat.install(this);
}
- 将 `SplitCompatApplication` 声明为 application 的子类, 并将 `FlutterApplication` 中的 flutter 兼容性代码添加到你的 application 类中:
<application
…
android:name=“com.google.android.play.core.splitcompat.SplitCompatApplication”>
嵌入层依赖注入的 DeferredComponentManager
实例来处理延迟组件的安装请求。 通过在应用程序的初始流程中添加以下代码,将 PlayStoreDeferredComponentManager
添加到 Flutter 嵌入层中:
import io.flutter.embedding.engine.dynamicfeatures.PlayStoreDeferredComponentManager;
import io.flutter.FlutterInjector;
…
layStoreDeferredComponentManager deferredComponentManager = new
PlayStoreDeferredComponentManager(this, null);
FlutterInjector.setInstance(new FlutterInjector.Builder()
.setDeferredComponentManager(deferredComponentManager).build());
- 通过将 `deferred-components` 依赖添加到应用程序的 `pubspec.yaml` 中的 `flutter` 下,并选择延迟组件:
…
flutter:
…
deferred-components:
…
flutter
工具会在 pubspec.yaml
中查找 deferred-components
, 来确定是否应将应用程序构建为延迟加载。 除非你已经知道所需的组件和每个组件中的 Dart 延迟库,否则可以暂时将其留空。 当 gen_snapshot
生成加载单元后,你可以在后面的 步骤 3.3
中完善这部分内容。
步骤 2:实现延迟加载的 Dart 库
接下来