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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-18 01:06:59 +0000
Seen: 16 times
Last updated: May 18 '23
Can a transitive dependency in gradle be made exclusively for runtime purposes?
Why can't IntelliJ IDEA detect the Kotlin multiplatform library?
How do compileSdkVersion and targetSdkVersion differ from each other?
What does the error message "Failed to apply plugin [id 'com.jfrog.artifactory']" mean?
Why doesn't the Android Studio display the layout preview once MaterialCalendarView has been added?
How to update the outdated WebSecurityConfigurerAdapter in Spring Boot?
How can we add animation to the user interface using jetpack compose?