将 Flutter module 集成到 Android 项目

Flutter can be embedded into your existing Android application piecemeal, as a source code Gradle subproject or as AARs.

The integration flow can be done using the Android Studio IDE with theFlutter plugin or manually.

请注意Your existing Android app may support architectures such as mipsor x86/x86_64. Flutter currently only supportsbuilding ahead-of-time (AOT) compiled libraries for armeabi-v7a and arm64-v8a.

Consider using the abiFiltersAndroid Gradle Plugin API to limit the supported architectures in your APK. Doing thisavoids a missing libflutter.so runtime crash, for example:

MyApp/app/build.gradle

  1. android {
  2. //...
  3. defaultConfig {
  4. ndk {
  5. // Filter for architectures supported by Flutter.
  6. abiFilters 'armeabi-v7a', 'arm64-v8a'
  7. }
  8. }
  9. }

The Flutter engine has an x86 and x86_64 version. When using an emulatorin debug Just-In-Time (JIT) mode, the Flutter module still runs correctly.

Using Android Studio

The Android Studio IDE is a convenient way of integrating your Fluttermodule automatically. With Android Studio, you can co-edit both your Androidcode and your Flutter code in the same project. You can also continue to useyour normal IntelliJ Flutter plugin functionalities such as Dart code completion,hot reload, and widget inspector.

Add-to-app flows with Android Studio are only supported on Android Studio 3.6,and only supports integrating using a source code Gradle subproject, rather thanusing AARs. See below for more details on the distinction.

Using the File > New > New Module… menu in Android Studio in your existingAndroid project, you can either create a new Flutter module to integrate, orselect an existing Flutter module that was created previously.

设置项目 - 图1

If you create a new module, you can use a wizard toselect the module name, location, and so on.

设置项目 - 图2

The Android Studio plugin automatically configures your Android projectto add your Flutter module as a dependency, and your app is ready to build.

备忘To see the changes that were automatically made to your Android project by theIDE plugin, consider using source control for your Android project before performingany steps. A local diff shows the changes.

小提示By default, your project’s Project pane is probably showing the ‘Android’ view. Ifyou can’t see your new Flutter files in the Project pane, ensure thatyour Project pane is set to display ‘Project Files’, which shows all fileswithout filtering.

Your app now includes the Flutter module as a dependency. You can jump to theAPI usage documentationsto follow the next steps.

Manual integration

To integrate a Flutter module with an existing Android app manually, withoutusing Flutter’s Android Studio plugin, follow these steps:

Create a Flutter module

Let’s assume that you have an existing Android app at some/path/MyApp, and that youwant your Flutter project as a sibling:

  1. $ cd some/path/
  2. $ flutter create -t module --org com.example my_flutter

This creates a some/path/my_flutter/ Flutter module project with some Dartcode to get you started and a .android/ hidden subfolder that wraps up themodule project in an Android library.

Java 8 requirement

The Flutter Android engine uses Java 8 features.

Before attempting to connect your Flutter module project to your host Android app,ensure that your host Android app declares the following sourcecompatibility within your app’s build.gradle file, under the android { }block, such as:

MyApp/app/build.gradle

  1. android {
  2. //...
  3. compileOptions {
  4. sourceCompatibility 1.8
  5. targetCompatibility 1.8
  6. }
  7. }

Add the Flutter module as a dependency

Next, add the Flutter module as a dependency of your existing app in Gradle.There are two ways to achieve this. The AAR mechanism creates generic AndroidAARs as intermediaries that packages your Flutter module. This is good when yourdownstream app builders don’t want to have the Flutter SDK installed. But,it adds one more build step if you build frequently.

The source code subproject mechanism is a convenient one-click buildprocess, but requires the Flutter SDK. This is the mechanism used by theAndroid Studio IDE plugin.

Option A - Depend on the Android Archive (AAR)

This option packages your Flutter library as a generic local Maven repository composedof AARs and POMs artifacts. This option allows your team to build the host app withoutinstalling the Flutter SDK. You can then distribute the artifacts froma local or remote repository.

Let’s assume you built a Flutter module at some/path/my_flutter, and then run:

  1. $ cd some/path/my_flutter
  2. $ flutter build aar

Then, follow the on-screen instructions to integrate.

设置项目 - 图3

More specifically, this command creates (by default all debug/profile/releasemodes) a local repository, with the following files:

  1. build/host/outputs/repo
  2. └── com
  3. └── example
  4. └── my_flutter
  5. ├── flutter_release
  6. ├── 1.0
  7. ├── flutter_release-1.0.aar
  8. ├── flutter_release-1.0.aar.md5
  9. ├── flutter_release-1.0.aar.sha1
  10. ├── flutter_release-1.0.pom
  11. ├── flutter_release-1.0.pom.md5
  12. └── flutter_release-1.0.pom.sha1
  13. ├── maven-metadata.xml
  14. ├── maven-metadata.xml.md5
  15. └── maven-metadata.xml.sha1
  16. ├── flutter_profile
  17. ├── ...
  18. └── flutter_debug
  19. └── ...

To depend on the AAR, the host app must be able to find these files.

To do that, edit app/build.gradle in your host app such as it includesthe local repository and the dependency:

MyApp/app/build.gradle

  1. android {
  2. // ...
  3. }
  4.  
  5. repositories {
  6. maven {
  7. url 'some/path/my_flutter/build/host/outputs/repo'
  8. // This is relative to the location of the build.gradle file
  9. // if using a relative path.
  10. }
  11. maven {
  12. url 'http://download.flutter.io'
  13. }
  14. }
  15.  
  16. dependencies {
  17. // ...
  18. debugImplementation 'com.example.flutter_module:flutter_debug:1.0
  19. profileImplementation 'com.example.flutter_module:flutter_profile:1.0
  20. releaseImplementation 'com.example.flutter_module:flutter_release:1.0
  21. }

小提示You can also build an AAR for your Flutter module in Android Studio usingthe Build > Flutter > Build AAR menu.

设置项目 - 图4

Your app now includes the Flutter module as a dependency. You can follow thenext steps in the API usage documentations.

Option B - Depend on the module’s source code

This option enables a one-step build for both your Android project and Flutter project.This option is convenient when you work on both parts simultaneously and rapidlyiterate, but your team must install the Flutter SDK to build the host app.

Include the Flutter module as a subproject in the host app’s settings.gradle:

MyApp/settings.gradle

  1. include ':app' // assumed existing content
  2. setBinding(new Binding([gradle: this])) // new
  3. evaluate(new File( // new
  4. settingsDir.parentFile, // new
  5. 'my_flutter/.android/include_flutter.groovy' // new
  6. )) // new

Assuming my_flutter is a sibling to MyApp.

The binding and script evaluation allows the Flutter module to include itself(as :flutter) and any Flutter plugins used by the module (as :package_info,:video_player, etc) in the evaluation context of your settings.gradle.

Introduce an implementation dependency on the Flutter module from your app:

MyApp/app/build.gradle

  1. dependencies {
  2. implementation project(':flutter')
  3. }

Your app now includes the Flutter module as a dependency. You can follow thenext steps in the API usage documentations.