text
stringlengths
1
474
described.Fundamentally, the Flutter framework on the Dart side requires access to various
activity-level events and lifecycles to function. Since the FlutterView (which
is an android.view.View)
can be added to any activity which is owned by the developer’s application
and since the FlutterView doesn’t have access to activity level events, the
developer must bridge those connections manually to the FlutterEngine.How you choose to feed your application’s activities’ events to the FlutterView
will be specific to your application.<topic_end>
<topic_start>
A sample
Unlike the guides for FlutterActivity and FlutterFragment, the FlutterView
integration could be better demonstrated with a sample project.A sample project is at https://github.com/flutter/samples/tree/main/add_to_app/android_view
to document a simple FlutterView integration where FlutterViews are used
for some of the cells in a RecycleView list of cards as seen in the gif above.<topic_end>
<topic_start>
General approach
The general gist of the FlutterView-level integration is that you must recreate
the various interactions between your Activity, the FlutterView
and the FlutterEngine
present in the FlutterActivityAndFragmentDelegate
in your own application’s code. The connections made in the FlutterActivityAndFragmentDelegate
are done automatically when using a FlutterActivity
or a FlutterFragment,
but since the FlutterView
in this case is being added to an Activity or Fragment in your application,
you must recreate the connections manually. Otherwise, the FlutterView
will not render anything or have other missing functionalities.A sample FlutterViewEngine
class shows one such possible implementation of an application-specific
connection between an Activity, a FlutterView
and a FlutterEngine.<topic_end>
<topic_start>
APIs to implement
The absolute minimum implementation needed for Flutter to draw anything at all
is to:The reverse detachFromFlutterEngine and other lifecycle methods on the LifecycleChannel
class must also be called to not leak resources when the FlutterView or Activity
is no longer visible.In addition, see the remaining implementation in the FlutterViewEngine
demo class or in the FlutterActivityAndFragmentDelegate
to ensure a correct functioning of other features such as clipboards, system
UI overlay, plugins etc.
<topic_end>
<topic_start>Manage plugins and dependencies in add-to-app
This guide describes how to set up your project to consume
plugins and how to manage your Gradle library dependencies
between your existing Android app and your Flutter module’s plugins.<topic_end>
<topic_start>
A. Simple scenario
In the simple cases:There are no additional steps needed. Your add-to-app
module will work the same way as a full-Flutter app.
Whether you integrate using Android Studio,
Gradle subproject or AARs,
transitive Android Gradle libraries are automatically
bundled as needed into your outer existing app.<topic_end>
<topic_start>
B. Plugins needing project edits
Some plugins require you to make some edits to the
Android side of your project.For example, the integration instructions for the
firebase_crashlytics plugin require manual
edits to your Android wrapper project’s build.gradle file.For full-Flutter apps, these edits are done in your
Flutter project’s /android/ directory.In the case of a Flutter module, there are only Dart
files in your module project. Perform those Android
Gradle file edits on your outer, existing Android
app rather than in your Flutter module.info Note
Astute readers might notice that the Flutter module
directory also contains an .android and an
.ios directory. Those directories are Flutter-tool-generated
and are only meant to bootstrap Flutter into generic
Android or iOS libraries. They should not be edited or checked-in.
This allows Flutter to improve the integration point should
there be bugs or updates needed with new versions of Gradle,
Android, Android Gradle Plugin, etc.For advanced users, if more modularity is needed and you must
not leak knowledge of your Flutter module’s dependencies into
your outer host app, you can rewrap and repackage your Flutter
module’s Gradle library inside another native Android Gradle
library that depends on the Flutter module’s Gradle library.
You can make your Android specific changes such as editing the
AndroidManifest.xml, Gradle files or adding more Java files
in that wrapper library.<topic_end>
<topic_start>
C. Merging libraries
The scenario that requires slightly more attention is if
your existing Android application already depends on the
same Android library that your Flutter module
does (transitively via a plugin).For instance, your existing app’s Gradle might already have:
<code_start>…
dependencies {
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
}
…<code_end>
And your Flutter module also depends on
firebase_crashlytics via pubspec.yaml:
<code_start>…
dependencies:
firebase_crashlytics: ^0.1.3
…<code_end>
This plugin usage transitively adds a Gradle dependency again via
firebase_crashlytics v0.1.3’s own Gradle file:
<code_start>…