# Flutter Module in Android ###### tags: `Flutter` `Work` [TOC] ## Step 1: Set Flutter Module and Android Project New a **Flutter Module** and an **Android Project** in the same folder at **same level**. 1. **New a Flutter Module** `flutter create -t module --org com.example flutter_module` 2. **New an Android Project** >![](https://i.imgur.com/pwZhn3x.png)[color=lightblue] ## Step 2: Build Flutter Module aar Run `flutter build aar` to get the aar that Android Project is going to use. ## Step 3: **Android Project:** Edit settings.gradle 1. Make sure it is `RepositoriesMode.PREFER_PROJECT` 2. Add storageUrl for flutter download and direction to your flutter module. ```java= dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.PREFER_PROJECT) repositories { google() mavenCentral() String storageUrl = "https://storage.googleapis.com" maven { url '../../name_of_module/build/host/outputs/repo' } maven { url "$storageUrl/download.flutter.io" }// Warning: this repository is going to shut down soon } } ``` ## Step 4: **Android Project:** Edit build.gradle(:app) & build.gradle(project level) In **build.gradle(:app)**, you must : 1. Copy the settings.gradle repository section and add it here. (Same in build.gradle Project Level) ```java= repositories { google() mavenCentral() String storageUrl = "https://storage.googleapis.com" maven { url '../../name_of_module/build/host/outputs/repo' } maven { url "$storageUrl/download.flutter.io" } } ``` 2. Add profile setting in buildTypes: ```linux= profile { initWith debug } ``` 3. Add the following dependencies: ```linux= debugImplementation 'com.example.name_of_module:flutter_debug:1.0' profileImplementation 'com.example.name_of_module:flutter_profile:1.0' releaseImplementation'com.example.name_of_module:flutter_release:1.0' ``` ## Step 5: **Android Project:** Link to Flutter Module Activity ### 5.1 Set up Flutter Engine Set up Flutter Engine which connects the aar and the Android Project. It is recommended to set it up in **Application class**, which must also be set up in **AndroidManifest.xml**. ```linux= <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zc.testandroid"> <application android:name=".AppSettings"> <activity android:name="io.flutter.embedding.android.FlutterActivity" android:theme="@style/MyTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" /> </application> </manifest> ``` Take below as example: ```java= public class AppSettings extends Application { static String FLUTTER_ENGINE_ID = "flutter_engine"; FlutterEngine flutterEngine; @Override public void onCreate() { super.onCreate(); flutterEngine = new FlutterEngine(this); flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault()); FlutterEngineCache.getInstance().put(FLUTTER_ENGINE_ID, flutterEngine); } } ``` ### 5.2 Set up Manifest: Add FlutterActivity ```linux= <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zc.testandroid"> <application android:name=".AppSettings"> <activity android:name="io.flutter.embedding.android.FlutterActivity" android:theme="@style/MyTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" /> </application> </manifest> ``` ### 5.3 Jump to Flutter Module main.dart Must make sure that your `FLUTTER_ENGINE_ID` is the same one as you set in `FlutterEngine` in Application Class. ```java= startActivity(FlutterActivity .withCachedEngine(FLUTTER_ENGINE_ID) .build(MainActivity.this)); ``` # Reference * https://levelup.gitconnected.com/how-to-add-flutter-to-android-app-4d80d9820686 * https://blog.codemagic.io/integrating-flutter-module-to-your-native-app/