Embedding Flutter in existing Android project

Great innovations come out of necessity.

Being a good CTO, Sumit M introduced Flutter at Nonstop IO Technologies and very well supported by CEO Saurabh Gawande which made our work faster, better and smoother. Thanks!

I have worked in Native Android as well as in Flutter. Since, Flutter saves about 70%-80% of the time if chosen as an alternative for Native Android, my almost all recent projects are developed in Flutter.

Since, I’m working on flutter for a long time, I got used to write-less-do-more. Hence, I am feeling damn lazy to go back to Native Android(RecyclerView, AsyncTask…, blah blah blah..) BUT!! I have an existing Android project which needs some additional features/screens. This is where I had to search and discover that it is possible to embed Flutter in Native Android

Okay! So, let’s start…

Primary aim is to launch FlutterActivity from Android Activity.

Since this is the very first time so create a very fresh new Android project and Flutter project separately. (Make sure flutter version is 0.4.4 or +)

Create a Flutter project: flutter create flutter_part

We have to convert this Application to a Library. To do so, change android/app/build.gradle file as follows:

@@ -11,7 +11,7 @@ if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

-apply plugin: 'com.android.application'
+apply plugin: 'com.android.library'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
@@ -22,8 +22,6 @@ android {
}

defaultConfig {
- // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
- applicationId "com.example.flutterpart"
minSdkVersion 16
targetSdkVersion 27
versionCode 1

Update android/app/src/main/AndroidManifest.xml as follows:


- <application
- android:name="io.flutter.app.FlutterApplication"
- android:label="flutter_part"
- android:icon="@mipmap/ic_launcher">
+ <application>
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
<meta-data android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
- <intent-filter>
- <action android:name="android.intent.action.MAIN"/>
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
</activity>
</application>
</manifest>

Update android/app/src/main/java/com/example/flutterpart/MainActivity.java as follows:

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
io.flutter.view.FlutterMain.startInitialization(this.getApplicationContext()); // <= New.
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}

For simplicity we assume a fresh Android application (with a button) that has been created using Android Studio. The minimal requirement for the Android application must be set to the same (or higher) minimal requirement as Flutter (minSdkVersion: 16).

Make sure your flutter and native android project is in adjacent folders

Update settings.gradle as:

include ':app', ':flutter_part'
project(':flutter_part').projectDir = new File(settingsDir, '../flutter_part/android/app')

At this point the project won’t compile anymore because the Flutter module needs to know where the Flutter project is located. We fix this by updating the local.properties file:

flutter.sdk=/usr/local/google/home/floitsch/code/flutter/flutter

The above is the installed flutter root path

Now add implementation project(“:flutter_part”) in app/build.gradle dependencies

Now, simply call the below method on a click:

public void startFlutterActivity(View view) {
ComponentName componentName = new ComponentName(this, "com.example.flutterpart.MainActivity");
Intent intent = new Intent().setComponent(componentName);
startActivity(intent);
}

“Run the project.. There you go!!”

There are few tweaks that are required to make this production ready, I will post them later.

Upcoming posts:

  • How to use single flutter library for multiple flutter screens.
  • How to embed Flutter in existing iOS project
Do let me know your experience, reviews and opinion on embedding Flutter in existing Android Project