How to add flutter_boost to an existing Android project. This demo was done on macOS. On Windows, change the paths to Windows format.
The Flutter side
Create a new flutter module
flutter create -t module flutter_moduleAdd the flutter_boost dependency in
pubspec.yamldependencies: flutter_boost:Run
flutter build apkWith flutter_boost 0.400 this throws an error. The key part is:* What went wrong: A problem occurred evaluating project ':flutter_boost'. > Plugin with id 'kotlin-android-extensions' not found.It’s not hard to see it’s a missing
kotlindependency. Let’s fix that.From the project root run
flutter make-host-app-editableWhen it finishes you’ll seeandroidandiosfolders appear.Add the
kotlindependency inandroid/build.graldebuildscript { + ext.kotlin_version = '1.3.21' repositories { google() jcenter() @@ -8,6 +9,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.2.1' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }Run
flutter build apkagain and it succeeds.Copy the files under
flutter_boost/example/libinto thelibdirectory
That’s the Flutter side prepared.
The Android side
Create a project with Android Studio
If you already have a project you can skip this step
Add kotlin support to the App module, and add the following to
settings.gradlesetBinding(new Binding([gradle: this])) evaluate(new File('../flutter_module/.android/include_flutter.groovy')) // keep the Android project and the flutter module in the same directoryRun
gralde sync. You should now see three new projects in your build:Flutter-flutter,flutter_boostandxservice_kitIf you hit the error below, change the gradle version to 4.10.2
ERROR: Unable to resolve dependency for ':flutter@dynamicProfile/compileClasspath': Could not resolve project :xservice_kit. Show Details Affected Modules: flutter ERROR: Unable to resolve dependency for ':flutter@dynamicProfile/compileClasspath': Could not resolve project :flutter_boost. Show Details Affected Modules: flutterAdd the dependencies below to the
build.gradlefile in theflutterservicedirectoryimplementation project(path: ':flutter') implementation project(":flutter_boost")Copy everything from
flutter_boost/example/androidand create a newMyApplicationclass:class MyApplication : FlutterApplication(), IPlatform { override fun onCreate() { super.onCreate() FlutterBoostPlugin.init(this) } override fun getApplication(): Application { return this } override fun getMainActivity(): Activity? { return MainActivity.sRef.get() } override fun isDebug(): Boolean { return true } override fun startActivity(context: Context?, url: String?, requestCode: Int): Boolean { return false } override fun getSettings(): MutableMap<Any?, Any?> { return HashMap() } }Then point the manifest at this Application
Add a weak reference in MainActivity to hold
MainActivityclass MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) sRef = WeakReference(this) } companion object { var sRef: WeakReference<Activity?> = WeakReference(null) } }Create FlutterPageActivity, and remember to add it to the manifest
class FlutterPageActivity:BoostFlutterActivity() { override fun getContainerParams(): MutableMap<Any?, Any?> { return HashMap() } override fun onRegisterPlugins(registry: PluginRegistry?) { GeneratedPluginRegistrant.registerWith(registry) } override fun getContainerName(): String { return "flutterPage" } }Give the textview an id in the layout
android:text="Hello World!" android:id="@+id/tvHello" app:layout_constraintBottom_toBottomOf="parent"Add a click listener in MainActivity
tvHello.setOnClickListener { startActivity(Intent(applicationContext, FlutterPageActivity::class.java)) }Run the Android project. If you hit errors, fix them following the prompts.
Click
Hello worldand a Flutter page opens. At this point you’ve gone from a native page to a Flutter page.Change the startActivity function in MyApplication to the code below:
override fun startActivity(context: Context?, url: String?, requestCode: Int): Boolean {
if (url?.contains("sample://nativePage") == true) {
context?.startActivity(Intent(context, MainActivity::class.java))
return true
}
return false
}
Click open native page in the Flutter page and the activity reopens.
That’s the round trip between both sides done. Welcome to the Flutter pit.