1 | initial version |
To integrate a Flutter module into an Android native app, follow the steps below:
Create a new Flutter module:
a. Open a terminal window and type: flutter create module_name
b. Rename the MainActivity
to something like ModuleMainActivity
Build the Flutter module:
a. Open the terminal and navigate to the module directory using cd ~/project_dir/module_name
b. Type flutter build aar
to build the module
Import the Flutter module to the Android app:
a. In the Android app build.gradle
file, add the following code to the repositories
section:
repositories {
flatDir {
dirs project(':module_name').file('libs')
}
}
b. Add the following code to the dependencies
section:
implementation(name: 'flutter-release', ext: 'aar')
implementation 'com.android.support:support-compat:27.1.1'
c. Sync the project and rebuild the app
Launch the Flutter module:
a. In the Android app, launch the Flutter module in a FlutterActivity
or a FlutterFragment
b. To use a FlutterActivity
, add the following code to the Android app AndroidManifest.xml
file:
<activity android:name="com.example.module_name.ModuleMainActivity">
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</activity>
c. To use a FlutterFragment
, add the following code to the Android app layout file:
<fragment
android:id="@+id/flutter_fragment"
android:name="io.flutter.embedding.android.FlutterFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:flutterInitialRoute="/"
app:flutterActivity="com.example.module_name.ModuleMainActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
d. To launch the FlutterActivity
or FlutterFragment
, call the startActivity
or startFragment
method from the Android app code:
startActivity(FlutterActivity.createDefaultIntent(this));
getSupportFragmentManager().beginTransaction()
.add(R.id.flutter_fragment, FlutterFragment.createDefault())
.commit();
That's it! You can now use the Flutter module in your Android app.