-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Multiple flutters sample android #677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# multiple_flutters_android | ||
|
||
This is an add-to-app sample that uses the Flutter engine group API to host | ||
multiple instances of Flutter in the app. | ||
|
||
## Getting Started | ||
|
||
```sh | ||
cd ../multiple_flutters_module | ||
flutter pub get | ||
cd - | ||
open -a "Android Studio" multiple_flutters_android/ # macOS command | ||
# (build and run) | ||
``` | ||
|
||
For more information see | ||
[multiple_flutters_module](../multiple_flutters_module/README.md). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
plugins { | ||
id 'com.android.application' | ||
id 'kotlin-android' | ||
} | ||
|
||
android { | ||
signingConfigs { | ||
self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea. Android studio added it, maybe when I was trying to solve my signing issue for release? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove it |
||
} | ||
} | ||
compileSdkVersion 30 | ||
|
||
defaultConfig { | ||
applicationId "dev.flutter.multipleflutters" | ||
minSdkVersion 24 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
signingConfig debug.signingConfig | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'androidx.core:core-ktx:1.2.0' | ||
implementation 'androidx.appcompat:appcompat:1.1.0' | ||
implementation 'com.google.android.material:material:1.1.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
testImplementation 'junit:junit:4.+' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
implementation project(':flutter') | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="dev.flutter.multipleflutters"> | ||
|
||
<application | ||
android:name=".App" | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.MultipleFlutters"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<activity | ||
android:name=".SingleFlutterActivity" | ||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" | ||
android:hardwareAccelerated="true" | ||
android:windowSoftInputMode="adjustResize" | ||
/> | ||
<activity | ||
android:name=".DoubleFlutterActivity" | ||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" | ||
android:hardwareAccelerated="true" | ||
android:windowSoftInputMode="adjustResize" | ||
/> | ||
</application> | ||
|
||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.flutter.multipleflutters | ||
|
||
import android.app.Application | ||
import io.flutter.embedding.engine.FlutterEngineGroup | ||
|
||
/** | ||
* Application class for this app. | ||
* | ||
* This holds onto our engine group. | ||
*/ | ||
class App : Application() { | ||
lateinit var engines: FlutterEngineGroup | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
engines = FlutterEngineGroup(this) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package dev.flutter.multipleflutters | ||
|
||
import java.lang.ref.WeakReference | ||
|
||
/** | ||
* Interface for getting notifications when the DataModel is updated. | ||
*/ | ||
interface DataModelObserver { | ||
fun onCountUpdate(newCount: Int) | ||
} | ||
|
||
/** | ||
* A singleton/observable data model for the data shared between Flutter and the host platform. | ||
* | ||
* This is the definitive source of truth for all data. | ||
*/ | ||
class DataModel { | ||
companion object { | ||
val instance = DataModel() | ||
} | ||
|
||
private val observers = mutableListOf<WeakReference<DataModelObserver>>() | ||
|
||
public var counter = 0 | ||
set(value) { | ||
gaaclarke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
field = value | ||
for (observer in observers) { | ||
observer.get()?.onCountUpdate(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you want to do the same thing as iOS here? (e.g. cull the dead references)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. though for fwiw, if you use the engine cache, then things will never be gc'ed under the weak reference. I'd probably just stick to explicit destructions which make things more manageable in the JVM anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm doing the same thing I'm doing on iOS. Skip on notify, cull on removeObserver. This should handle anybody that is being GC'd or manually deleted. |
||
} | ||
} | ||
|
||
fun addObserver(observer: DataModelObserver) { | ||
observers.add(WeakReference(observer)) | ||
} | ||
|
||
fun removeObserver(observer: DataModelObserver) { | ||
observers.removeIf { | ||
if (it.get() != null) it.get() == observer else true | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package dev.flutter.multipleflutters | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.FrameLayout | ||
import android.widget.LinearLayout | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.fragment.app.FragmentManager | ||
import io.flutter.embedding.android.FlutterFragment | ||
import io.flutter.embedding.engine.FlutterEngineCache | ||
|
||
/** | ||
* An activity that displays 2 FlutterFragments vertically. | ||
*/ | ||
class DoubleFlutterActivity : FragmentActivity(), EngineBindingsDelegate { | ||
private val topBindings: EngineBindings by lazy { | ||
EngineBindings(activity = this, delegate = this, entrypoint = "topMain") | ||
} | ||
private val bottomBindings: EngineBindings by lazy { | ||
EngineBindings(activity = this, delegate = this, entrypoint = "bottomMain") | ||
} | ||
private val numberOfFlutters = 2 | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val root = LinearLayout(this) | ||
root.layoutParams = LinearLayout.LayoutParams( | ||
LinearLayout.LayoutParams.MATCH_PARENT, | ||
LinearLayout.LayoutParams.MATCH_PARENT | ||
) | ||
root.orientation = LinearLayout.VERTICAL | ||
root.weightSum = numberOfFlutters.toFloat() | ||
|
||
val fragmentManager: FragmentManager = supportFragmentManager | ||
|
||
setContentView(root) | ||
|
||
val app = applicationContext as App | ||
|
||
for (i in 0 until numberOfFlutters) { | ||
val flutterContainer = FrameLayout(this) | ||
root.addView(flutterContainer) | ||
flutterContainer.id = 12345 + i | ||
flutterContainer.layoutParams = LinearLayout.LayoutParams( | ||
FrameLayout.LayoutParams.MATCH_PARENT, | ||
FrameLayout.LayoutParams.MATCH_PARENT, | ||
1.0f | ||
) | ||
val engine = if (i == 0) topBindings.engine else bottomBindings.engine | ||
FlutterEngineCache.getInstance().put(i.toString(), engine) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #762 is probably this. The |
||
val flutterFragment = | ||
FlutterFragment.withCachedEngine(i.toString()).build<FlutterFragment>() | ||
fragmentManager | ||
.beginTransaction() | ||
.add( | ||
12345 + i, | ||
flutterFragment | ||
) | ||
.commit() | ||
} | ||
|
||
topBindings.attach() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we do this for iOS too for consistency? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It already is happening, but in a slightly different way. On iOS SingleFlutterViewController bundles the bindings with itself so it's not necessary. I wouldn't want to add a concept that isn't necessary. I don't think that would make the sample more clear. A more similar approach would be to have Fragment that bundles the communication with a FlutterFragment, then have SigqleFlutterActivity and DoubleFlutterActivity embed that Fragment. Unfortunately for me, converting an Activity to a Fragment isn't trivial. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya, I just meant generally where the platform channel handlers sit between the 2 platforms if someone was looking for them in each of the platforms. If they need to be in different sounding files, maybe add a comment cross-referencing the difference? |
||
bottomBindings.attach() | ||
} | ||
|
||
override fun onDestroy() { | ||
for (i in 0 until numberOfFlutters) { | ||
FlutterEngineCache.getInstance().remove(i.toString()) | ||
} | ||
|
||
super.onDestroy() | ||
|
||
bottomBindings.detach() | ||
topBindings.detach() | ||
} | ||
gaaclarke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
override fun onNext() { | ||
val flutterIntent = Intent(this, MainActivity::class.java) | ||
startActivity(flutterIntent) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package dev.flutter.multipleflutters | ||
|
||
import android.app.Activity | ||
import io.flutter.FlutterInjector | ||
import io.flutter.embedding.engine.FlutterEngine | ||
import io.flutter.embedding.engine.dart.DartExecutor | ||
import io.flutter.plugin.common.MethodChannel | ||
|
||
/** | ||
* This interface represents the notifications an EngineBindings may be receiving from the Flutter | ||
* instance. | ||
* | ||
* What methods this interface has depends on the messages that are sent over the EngineBinding's | ||
* channel in `main.dart`. Messages that interact with the DataModel are handled automatically | ||
* by the EngineBindings. | ||
* | ||
* @see main.dart for what messages are getting sent from Flutter. | ||
*/ | ||
interface EngineBindingsDelegate { | ||
fun onNext() | ||
} | ||
|
||
/** | ||
* This binds a FlutterEngine instance with the DataModel and a channel for communicating with that | ||
* engine. | ||
* | ||
* Messages involving the DataModel are handled by the EngineBindings, other messages are forwarded | ||
* to the EngineBindingsDelegate. | ||
* | ||
* @see main.dart for what messages are getting sent from Flutter. | ||
*/ | ||
class EngineBindings(activity: Activity, delegate: EngineBindingsDelegate, entrypoint: String) : | ||
DataModelObserver { | ||
val channel: MethodChannel | ||
val engine: FlutterEngine | ||
val delegate: EngineBindingsDelegate | ||
|
||
init { | ||
val app = activity.applicationContext as App | ||
// This has to be lazy to avoid creation before the FlutterEngineGroup. | ||
val dartEntrypoint = | ||
DartExecutor.DartEntrypoint( | ||
FlutterInjector.instance().flutterLoader().findAppBundlePath(), entrypoint | ||
) | ||
engine = app.engines.createAndRunEngine(activity, dartEntrypoint) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah that's where engines is lazy built first. I think this is an example of the init lifecycle not leaking outside the class and being more deterministic using lateinit vs lazy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I switched engines to lateinit. If this was code that I was writing for me I would prefer lazy evals, but having a lazy eval for a variable is a bit much for sample code. I kept lazy for the engine bindings because they are actually used before onCreate in the case of SingleFlutterActivity, so the code is cleaner this way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SG |
||
this.delegate = delegate | ||
channel = MethodChannel(engine.dartExecutor.binaryMessenger, "multiple-flutters") | ||
} | ||
|
||
/** | ||
* This setups the messaging connections on the platform channel and the DataModel. | ||
*/ | ||
fun attach() { | ||
DataModel.instance.addObserver(this) | ||
channel.invokeMethod("setCount", DataModel.instance.counter) | ||
gaaclarke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
channel.setMethodCallHandler { call, result -> | ||
when (call.method) { | ||
"incrementCount" -> { | ||
DataModel.instance.counter = DataModel.instance.counter + 1 | ||
result.success(null) | ||
} | ||
"next" -> { | ||
this.delegate.onNext() | ||
result.success(null) | ||
} | ||
else -> { | ||
result.notImplemented() | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This tears down the messaging connections on the platform channel and the DataModel. | ||
*/ | ||
fun detach() { | ||
// TODO: Uncomment after https://github.com/flutter/engine/pull/24644 is on stable. | ||
// engine.destroy(); | ||
DataModel.instance.removeObserver(this) | ||
channel.setMethodCallHandler(null) | ||
} | ||
|
||
override fun onCountUpdate(newCount: Int) { | ||
gaaclarke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
channel.invokeMethod("setCount", newCount) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.