Integrate Flutter app in native Android app

One way to achieve this is by seamlessly integrating a Flutter app into a native Android application. In this article, we’ll delve into the process of integrating a Flutter module into a native Android app, step by step.

Step 1: Creating your Flutter Module

Let’s dive right into the process. The initial step involves creating your Flutter project as a module. Here’s how you can do it:

  1. Open your terminal and navigate to the desired location using the command: 
```

    $ cd some/path/

   ```
  1. Create the Flutter module using the following command:
 ```

    $ flutter create -t module --org com.example my_flutter

    ```

The optional `–org` parameter allows you to define a reverse domain name notation for your app. Omitting this parameter won’t hinder you, as it can be adjusted later. The name you provide for your Flutter module (in this case, `my_flutter`) is mandatory.

This command generates a Flutter module project with essential Dart code to kickstart your project. Additionally, an `.android/` hidden subfolder is created. Within this folder lies an Android project that not only aids in running a basic version of your Flutter module but also acts as a wrapper to integrate the Flutter module as an embeddable Android library.

Step 2: Adding the Flutter Module Dependency

Assuming you’ve successfully generated your Flutter module inside `some/path/my_flutter/`, the next phase is to build the Android Archive (AAR). The AAR serves as an Android library that can be integrated as a dependency in your Android app module.

AARs offer advantages over JAR files:

  • They can encompass Android resources and a manifest file, enabling the inclusion of shared resources like layouts, drawables, and Java classes.
  • – AARs can accommodate C/C++ libraries for use by the app module’s C/C++ code.

To create the AAR, execute the following commands:

  1. Navigate to your Flutter module directory:
   ```

   $ cd some/path/my_flutter/

   ```


  1.  Build the AAR using Flutter's build command:
   ```

   $ flutter build aar

   ```

The output provides clear instructions for the next steps. It guides you to open the `build.gradle` file within your native Android project. The code provided in the output, including the `String storageUrl` variable and the `repositories { … }` block, should be integrated within the `repositories { … }` block under `dependencyResolutionManagement { … }` in your `settings.gradle` (or similar) file. Additionally, the provided dependencies should be inserted in the corresponding `dependencies { … }` block within the `build.gradle` file. Finally, enabling the profile build type is accomplished by copying the `profile { initWithDebug }` block into the `buildTypes { … }` block of the `build.gradle` file.

 

With these changes in place, rebuild the `build.gradle` file to apply the modifications.

Step 3: Running the Flutter App within the Android App

Flutter simplifies the process of displaying a Flutter experience within an Android app through the `FlutterActivity`. To utilize this feature, you need to follow these steps:

  1. Add the following XML code within the `application` tag of your `AndroidManifest.xml` file:
   ```

   <activity

       android:name="io.flutter.embedding.android.FlutterActivity"

       android:theme="@style/LaunchTheme"

       android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"

       android:hardwareAccelerated="true"

       android:windowSoftInputMode="adjustResize"

       android:resizeableActivity="false" />

   ```
  1. Inside your Kotlin code, initiate your Flutter activity as needed.

And there you have it! You’ve successfully integrated your Flutter module into your native Android app. This seamless integration opens doors to a plethora of possibilities, combining the power of Flutter’s rich UI capabilities with the versatility of native Android development. 

Leave a Reply