Sub topic 4
Integrating a Flutter Module into an Android App
Prerequisites and the integration model
This chapter assumes that you know basic Dart and Flutter, can run commands with the Flutter CLI, and understand Kotlin, Gradle, and Android activities. You also need an existing Android app and a Flutter SDK installed on the development machine.
Add-to-app integration keeps the Android app as the host. The Flutter module supplies selected screens or features. The Android app decides when to open a Flutter screen and can exchange data with Dart code.
A Flutter module contains a hidden .android host project. That project generates the Gradle files needed to build Flutter code as part of the Android app.
Create the module
Create the module beside the existing Android project, not inside its app directory.
flutter create --template module my_flutter_module
cd my_flutter_module
flutter pub getThe generated module has a normal Dart entrypoint in lib/main.dart. You can develop and test it independently with flutter run, but the Android host uses the generated Android wrapper when it builds the complete app.
Add the module to Gradle
Assume this directory layout:
workspace/
android_host/
my_flutter_module/Open android_host/settings.gradle and include the Flutter module's generated Gradle script:
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "android_host"
include(":app")
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
"my_flutter_module/.android/include_flutter.groovy"
))Then add the generated :flutter project as a dependency of the Android application. In android_host/app/build.gradle, add:
plugins {
id "com.android.application"
id "org.jetbrains.kotlin.android"
}
android {
namespace "com.example.host"
compileSdk 35
defaultConfig {
applicationId "com.example.host"
minSdk api 21
targetSdk 35
versionCode 1
versionName "1.0"
}
}
dependencies {
implementation(project(":flutter"))
}The exact plugin versions and SDK values must match the existing Android project. Do not replace its complete Gradle file with this small example. Add the Flutter dependency to the existing dependencies block.
| Location | Change | Purpose |
|---|---|---|
| `settings.gradle` | Evaluate `include_flutter.groovy` | Registers the generated Flutter project |
| `app/build.gradle` | Add `implementation(project(":flutter"))` | Packages the module with the app |
| Activity source | Use `FlutterActivity` | Displays Flutter content |
After changing Gradle files, synchronize the project and build the Android app. The module's plugins and Dart code are then compiled as part of the host build.
Launch a Flutter screen
The Android embedding is the Android-side API that connects an activity to a Flutter engine. FlutterActivity supplies a ready-made activity for displaying Flutter UI.
Add this method to an existing Kotlin activity:
import android.content.Intent
import io.flutter.embedding.android.FlutterActivity
private fun openFlutter() {
val intent = FlutterActivity
.withNewEngine()
.initialRoute("/cart")
.build(this)
startActivity(intent)
}Call openFlutter() from a button click or another native event. The route must match a route configured by the Dart application. If the module does not define /cart, Flutter may show an unknown-route screen or its fallback route.
For a basic module, withNewEngine() creates an engine when the activity opens. This is simple and isolates the screen, but engine startup can add visible latency. A production app that opens Flutter often should pre-warm and cache an engine, then use FlutterActivity.withCachedEngine(...). Configure that optimization after the basic integration works.
Common integration errors
Common failures include:
- The
include_flutter.groovypath is wrong. Check the directory layout and spelling. - The
:flutterdependency is missing. Gradle then cannot resolve the Flutter project. - The host uses an old Android embedding. Use
io.flutter.embedding.android.FlutterActivity, not the deprecated v1 embedding classes. - The requested initial route is not defined in Dart.
- Native code calls a platform channel before the Dart side has registered its handler. Register handlers during engine setup and define the same channel name on both sides.
Exercise
Your host app is in workspace/android_host, and the module is in workspace/flutter_features. Which path should settings.gradle pass to include_flutter.groovy?
Show answer
Use settingsDir.parentFile followed by flutter_features/.android/include_flutter.groovy, because settingsDir points to android_host.