Skip to content

Commit 8b0c934

Browse files
committed
Added README
1 parent bfb7e32 commit 8b0c934

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Diff for: 29_add_to_android_app/README.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Add Flutter to an Android App
2+
=============================
3+
4+
Simple example of adding Flutter to an Android app, either as a Fragment, or an Activity. Includes pre-warming the Flutter engine.
5+
6+
Notes
7+
-----
8+
9+
* Latest master branch of Flutter must be used
10+
11+
* Flutter module was created using ```flutter create -t module <flutter_module_name>```
12+
13+
* Flutter module is included in Android project by adding the following to settings.gradle
14+
15+
```groovy
16+
setBinding(new Binding([gradle: this]))
17+
evaluate(new File(
18+
settingsDir.parentFile,
19+
'<flutter_dir>/.android/include_flutter.groovy'
20+
))
21+
```
22+
23+
* Flutter module is added as a dependency to the Android app in build.gradle:
24+
25+
```groovy
26+
dependencies {
27+
implementation project(':flutter')
28+
}
29+
```
30+
31+
* The Flutter engine is pre-warmed in a custom Application instance:
32+
33+
```kotlin
34+
class MyApplication : Application() {
35+
lateinit var engine: FlutterEngine
36+
37+
override fun onCreate() {
38+
super.onCreate()
39+
FlutterMain.startInitialization(applicationContext)
40+
FlutterMain.ensureInitializationComplete(applicationContext, arrayOf<String>())
41+
engine = FlutterEngine(this)
42+
val entryPoint = DartExecutor.DartEntrypoint(this.assets,
43+
FlutterMain.findAppBundlePath(this), "main")
44+
engine.dartExecutor.executeDartEntrypoint(entryPoint)
45+
}
46+
}
47+
```
48+
49+
* The Flutter fragment gets its engine instance from the Application instance and doesn't destroy it when the fragment is destroyed:
50+
51+
```kotlin
52+
class MyFlutterFragment : FlutterFragment() {
53+
54+
companion object {
55+
@JvmStatic
56+
fun newInstance() =
57+
MyFlutterFragment().apply {
58+
return FlutterFragment.Builder(MyFlutterFragment::class.java)
59+
.renderMode(FlutterView.RenderMode.surface)
60+
.transparencyMode(FlutterView.TransparencyMode.transparent)
61+
.build<MyFlutterFragment>()
62+
}
63+
}
64+
65+
override fun createFlutterEngine(@NonNull context: Context): FlutterEngine =
66+
(context.applicationContext as MyApplication).engine
67+
68+
69+
override fun retainFlutterEngineAfterFragmentDestruction() = true
70+
}
71+
```
72+
73+
* The Flutter Activity also pulls the engine instance from the custom Application:
74+
75+
```kotlin
76+
class MyFlutterActivity : FlutterActivity(), FlutterFragment.FlutterEngineProvider {
77+
override fun getFlutterEngine(context: Context): FlutterEngine? =
78+
(context.applicationContext as MyApplication).engine
79+
}
80+
```

0 commit comments

Comments
 (0)