Ask Your Question
2

How can I integrate a Flutter module into an Android native app?

asked 2023-05-18 01:06:59 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-18 01:31:02 +0000

nofretete gravatar image

To integrate a Flutter module into an Android native app, follow the steps below:

  1. Create a new Flutter module:

    a. Open a terminal window and type: flutter create module_name

    b. Rename the MainActivity to something like ModuleMainActivity

  2. 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

  3. 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

  4. 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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-05-18 01:06:59 +0000

Seen: 16 times

Last updated: May 18 '23