Integrate a Flutter module into your Android project

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 the Flutter plugin or manually.

Integrate your Flutter module

#

Integrate with Android Studio

#

The Android Studio IDE can help integrate your Flutter module. Using Android Studio, you can edit both your Android and Flutter code in the same IDE.

You can also use IntelliJ Flutter plugin functionality like Dart code completion, hot reload, and widget inspector.

Android Studio supports add-to-app flows on Android Studio 2022.2 or later with the Flutter plugin for IntelliJ. To build your app, the Android Studio plugin configures your Android project to add your Flutter module as a dependency.

  1. Open your Android project in Android Studio.

  2. Go to File > New > New Project.... The New Project dialog displays.

  3. Click Flutter.

  4. If asked to provide your Flutter SDK path, do so and click Next.

  5. Complete the configuration of your Flutter module.

    • If you have an existing project:

      1. To choose an existing project, click ... to the right of the Project location box.
      2. Navigate to your Flutter project directory.
      3. Click Open.
    • If you need to create a new Flutter project:

      1. Complete the configuration dialog.
      2. In the Project type menu, select Module.
  6. Click Finish.

Integrate without Android Studio

#

To integrate a Flutter module with an existing Android app manually, without using 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 you want your Flutter project as a sibling:

cd some/path/
flutter create -t module --org com.example flutter_module

This creates a some/path/flutter_module/ Flutter module project with some Dart code to get you started and an .android/ hidden subfolder. The .android folder contains an Android project that can both help you run a barebones standalone version of your Flutter module via flutter run and it's also a wrapper that helps bootstrap the Flutter module an embeddable Android library.

Java version requirement

#

Flutter requires your project to declare compatibility with Java 11 or later.

Before attempting to connect your Flutter module project to your host Android app, ensure that your host Android app declares the following source compatibility within your app's build.gradle file, under the android { } block.

MyApp/app/build.gradle
groovy
android {
  //...
  compileOptions {
    sourceCompatibility 11 // The minimum value
    targetCompatibility 11 // The minimum value
  }
}

Centralize repository settings

#

Starting with Gradle 7, Android recommends using centralized repository declarations in settings.gradle instead of project or module level declarations in build.gradle files.

Before attempting to connect your Flutter module project to your host Android app, make the following changes.

  1. Remove the repositories block in all of your app's build.gradle files.

    groovy
    // Remove the following block, starting on the next line
        repositories {
            google()
            mavenCentral()
        }
    // ...to the previous line
  2. Add the dependencyResolutionManagement displayed in this step to the settings.gradle file.

    groovy
    dependencyResolutionManagement {
      repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
      repositories {
        google()
        mavenCentral()
      }
    }
## Add the Flutter module as a dependency

Add the Flutter module as a dependency of your existing app in Gradle. You can achieve this in two ways.

  1. Android archive The AAR mechanism creates generic Android AARs as intermediaries that packages your Flutter module. This is good when your downstream app builders don't want to have the Flutter SDK installed. But, it adds one more build step if you build frequently.

  2. Module source code The source code subproject mechanism is a convenient one-click build process, but requires the Flutter SDK. This is the mechanism used by the Android Studio IDE plugin.

Depend on the Android Archive (AAR)

#

This option packages your Flutter library as a generic local Maven repository composed of AARs and POMs artifacts. This option allows your team to build the host app without installing the Flutter SDK. You can then distribute the artifacts from a local or remote repository.

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

cd some/path/flutter_module
flutter build aar

Then, follow the on-screen instructions to integrate.

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

build/host/outputs/repo
└── com
    └── example
        └── flutter_module
            ├── flutter_release
            │   ├── 1.0
            │   │   ├── flutter_release-1.0.aar
            │   │   ├── flutter_release-1.0.aar.md5
            │   │   ├── flutter_release-1.0.aar.sha1
            │   │   ├── flutter_release-1.0.pom
            │   │   ├── flutter_release-1.0.pom.md5
            │   │   └── flutter_release-1.0.pom.sha1
            │   ├── maven-metadata.xml
            │   ├── maven-metadata.xml.md5
            │   └── maven-metadata.xml.sha1
            ├── flutter_profile
            │   ├── ...
            └── flutter_debug
                └── ...

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

To do that, edit settings.gradle in your host app so that it includes the local repository and the dependency:

groovy
dependencyResolutionManagement {
  repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
  repositories {
    google()
    mavenCentral()

  // Add the new repositories starting on the next line...
    maven {
      url 'some/path/flutter_module/build/host/outputs/repo'
      // This is relative to the location of the build.gradle file
      // if using a relative path.
    }
    maven {
      url 'https://storage.googleapis.com/download.flutter.io'
    }
  // ...to before this line  
  }
}

Kotlin DSL based Android Project

#

After an aar build of a Kotlin DSL-based Android project, follow these steps to add the flutter_module.

Include the flutter module as a dependency in the Android project's app/build.gradle file.

MyApp/app/build.gradle.kts
kotlin
android {
    buildTypes {
        release {
          ...
        }
        debug {
          ...
        }
        create("profile") {
            initWith(getByName("debug"))
        }
}
dependencies {
  // ...
  debugImplementation "com.example.flutter_module:flutter_debug:1.0"
  releaseImplementation 'com.example.flutter_module:flutter_release:1.0'
  add("profileImplementation", "com.example.flutter_module:flutter_profile:1.0")
}

The profileImplementation ID is a custom configuration to be implemented in the app/build.gradle file of a host project.

host-project/app/build.gradle.kts
kotlin
configurations {
    getByName("profileImplementation") {
    }
}
MyApp/settings.gradle.kts
kotlin
include(":app")

dependencyResolutionManagement {
    repositories {
        maven(url = "https://storage.googleapis.com/download.flutter.io")
        maven(url = "some/path/flutter_module_project/build/host/outputs/repo")
    }
}

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 rapidly iterate, 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. This example assumes flutter_module and MyApp exist in the same directory

MyApp/settings.gradle
groovy
// Include the host app project.
include ':app'                                    // assumed existing content
setBinding(new Binding([gradle: this]))                                // new
evaluate(new File(                                                     // new
    settingsDir.parentFile,                                            // new
    'flutter_module/.android/include_flutter.groovy'                   // new
))                                                                     // new

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

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

MyApp/app/build.gradle
groovy
dependencies {
    implementation project(':flutter')
}
Your app now includes the Flutter module as a dependency.

Continue to the Adding a Flutter screen to an Android app guide.