This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Add plugin for Android lifecycle in embedding #2168
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a24c43b
plugin template
amirh ad027b0
xcodeproj auto update
amirh b6058a0
use new pubspec schema
amirh 4fc704b
implementation
amirh b9724c9
add e2e test
amirh ba87bfe
remove unused Dart unit tests
amirh e748eb1
format
amirh 137188f
remove the example app's widget test
amirh c47c388
remove unused import
amirh 80fa536
review fixes
amirh 88c8461
conditionally include androidx gradle dep
amirh 22941f2
more cleanups
amirh b52fed6
update readme
amirh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| .DS_Store | ||
| .dart_tool/ | ||
|
|
||
| .packages | ||
| .pub/ | ||
|
|
||
| build/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # This file tracks properties of this Flutter project. | ||
| # Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
| # | ||
| # This file should be version controlled and should not be manually edited. | ||
|
|
||
| version: | ||
| revision: 0e605cc4dd83137f785769dea5e8ae7da1afb361 | ||
| channel: master | ||
|
|
||
| project_type: plugin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ## 1.0.0 | ||
|
|
||
| * Introduces a `FlutterLifecycleAdapter`, which can be used by other plugins to obtain a `Lifecycle` | ||
| reference from a `FlutterPluginBinding`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Flutter Android Lifecycle Plugin | ||
|
|
||
| [](https://pub.dartlang.org/packages/flutter_android_lifecycle) | ||
|
|
||
| A Flutter plugin for Android to allow other Flutter plugins to access an Android `Lifecycle` object | ||
| in the plugin's binding. | ||
|
|
||
| The purpose of having this plugin instead of exposing an Android `Lifecycle` object in the engine's | ||
| Android embedding plugins API is to force plugins to have a pub constraint that signifies the | ||
| major version of the Android `Lifecycle` API they expect. | ||
|
|
||
| ## Installation | ||
|
|
||
| Add `flutter_android_lifecycle` as a [dependency in your pubspec.yaml file](https://flutter.io/using-packages/). | ||
|
|
||
| ## Example | ||
|
|
||
| Use a `FlutterLifecycleAdapter` within another Flutter plugin's Android implementation, as shown | ||
mklim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| below: | ||
|
|
||
| ```java | ||
| import androidx.lifecycle.Lifecycle; | ||
| import io.flutter.embedding.engine.FlutterEngine; | ||
| import io.flutter.embedding.engine.plugins.FlutterPlugin; | ||
| import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding; | ||
| import io.flutter.embedding.engine.plugins.lifecycle.FlutterLifecycleAdapter; | ||
|
|
||
| public class MyPlugin implements FlutterPlugin { | ||
| @Override | ||
| public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { | ||
| Lifecycle lifecycle = new FlutterLifecycleAdapter.getLifecycle(binding); | ||
|
|
||
| // Use lifecycle as desired. | ||
| } | ||
|
|
||
| //... | ||
| } | ||
| ``` | ||
|
|
||
| [Feedback welcome](https://github.com/flutter/flutter/issues) and | ||
| [Pull Requests](https://github.com/flutter/plugins/pulls) are most welcome! | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| *.iml | ||
| .gradle | ||
| /local.properties | ||
| /.idea/workspace.xml | ||
| /.idea/libraries | ||
| .DS_Store | ||
| /build | ||
| /captures |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| group 'io.flutter.plugins.flutter_android_lifecycle' | ||
| version '1.0' | ||
|
|
||
| buildscript { | ||
| repositories { | ||
| google() | ||
| jcenter() | ||
| } | ||
|
|
||
| dependencies { | ||
| classpath 'com.android.tools.build:gradle:3.5.0' | ||
| } | ||
| } | ||
|
|
||
| rootProject.allprojects { | ||
| repositories { | ||
| google() | ||
| jcenter() | ||
| } | ||
| } | ||
|
|
||
| apply plugin: 'com.android.library' | ||
|
|
||
| android { | ||
| compileSdkVersion 28 | ||
|
|
||
| defaultConfig { | ||
| minSdkVersion 16 | ||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
| } | ||
| lintOptions { | ||
| disable 'InvalidPackage' | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation "androidx.annotation:annotation:1.1.0" | ||
| } | ||
| } | ||
|
|
||
| // TODO(amirh): Remove this hack once androidx.lifecycle is included on stable. https://github.com/flutter/flutter/issues/42348 | ||
| afterEvaluate { | ||
| def containsEmbeddingDependencies = false | ||
| for (def configuration : configurations.all) { | ||
| for (def dependency : configuration.dependencies) { | ||
| if (dependency.group == 'io.flutter' && | ||
| dependency.name.startsWith('flutter_embedding') && | ||
| dependency.isTransitive()) | ||
| { | ||
| containsEmbeddingDependencies = true | ||
| break | ||
| } | ||
| } | ||
| } | ||
| if (!containsEmbeddingDependencies) { | ||
| android { | ||
| dependencies { | ||
| def lifecycle_version = "2.1.0" | ||
| api "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" | ||
| api "androidx.lifecycle:lifecycle-runtime:$lifecycle_version" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| org.gradle.jvmargs=-Xmx1536M | ||
| android.enableR8=true | ||
| android.useAndroidX=true | ||
| android.enableJetifier=true |
5 changes: 5 additions & 0 deletions
5
packages/flutter_android_lifecycle/android/gradle/wrapper/gradle-wrapper.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rootProject.name = 'flutter_android_lifecycle' |
3 changes: 3 additions & 0 deletions
3
packages/flutter_android_lifecycle/android/src/main/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="io.flutter.plugins.flutter_android_lifecycle"> | ||
| </manifest> |
52 changes: 52 additions & 0 deletions
52
.../src/main/java/io/flutter/embedding/engine/plugins/lifecycle/FlutterLifecycleAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.embedding.engine.plugins.lifecycle; | ||
|
|
||
| import static io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.lifecycle.Lifecycle; | ||
| import io.flutter.Log; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
|
|
||
| /** Provides a static method for extracting lifecycle objects from Flutter plugin bindings. */ | ||
| public class FlutterLifecycleAdapter { | ||
|
Contributor
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. nit: Needs documentation. https://google.github.io/styleguide/javaguide.html#s7.3-javadoc-where-required
Contributor
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. Just a nit, but this is still needed for this class to comply with Google Java style.
Contributor
Author
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. done |
||
| private static final String TAG = "FlutterLifecycleAdapter"; | ||
|
|
||
| /** | ||
| * Returns the lifecycle object for the given Flutter plugin binding. | ||
| * | ||
| * <p>Returns null if the Flutter engine version does not include the lifecycle extraction code. | ||
| * (this probably means the Flutter engine version is too old). | ||
| */ | ||
| @Nullable | ||
| public static Lifecycle getLifecycle(@NonNull FlutterPluginBinding pluginBinding) { | ||
| try { | ||
| Object reference = pluginBinding.getLifecycle(); | ||
| Class hiddenLifecycleClass = | ||
| Class.forName("io.flutter.embedding.engine.plugins.lifecycle.HiddenLifecycleReference"); | ||
|
|
||
| if (!reference.getClass().equals(hiddenLifecycleClass)) { | ||
| throw new IllegalArgumentException( | ||
| "The reference argument must be of type HiddenLifecycleReference. Was actually " | ||
| + reference); | ||
| } | ||
|
|
||
| Method getLifecycle = reference.getClass().getMethod("getLifecycle"); | ||
| return (Lifecycle) getLifecycle.invoke(reference); | ||
| } catch (ClassNotFoundException | ||
| | NoSuchMethodException | ||
| | IllegalAccessException | ||
| | InvocationTargetException e) { | ||
| Log.w( | ||
mklim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TAG, | ||
| "You are attempting to use Flutter plugins that are newer than your" | ||
| + " version of Flutter. Plugins may not work as expected."); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
...main/java/io/flutter/plugins/flutter_android_lifecycle/FlutterAndroidLifecyclePlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
| // | ||
| package io.flutter.plugins.flutter_android_lifecycle; | ||
|
|
||
| import io.flutter.plugin.common.PluginRegistry.Registrar; | ||
|
|
||
| /** | ||
| * Plugin class that exists because the Flutter tool expects such a class to exist for every Android | ||
| * plugin. | ||
| * | ||
| * <p><strong>DO NOT USE THIS CLASS.</strong> | ||
| */ | ||
| public class FlutterAndroidLifecyclePlugin { | ||
| public static void registerWith(Registrar registrar) { | ||
| // no-op | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Miscellaneous | ||
| *.class | ||
| *.log | ||
| *.pyc | ||
| *.swp | ||
| .DS_Store | ||
| .atom/ | ||
| .buildlog/ | ||
| .history | ||
| .svn/ | ||
|
|
||
| # IntelliJ related | ||
| *.iml | ||
| *.ipr | ||
| *.iws | ||
| .idea/ | ||
|
|
||
| # The .vscode folder contains launch configuration and tasks you configure in | ||
| # VS Code which you may wish to be included in version control, so this line | ||
| # is commented out by default. | ||
| #.vscode/ | ||
|
|
||
| # Flutter/Dart/Pub related | ||
| **/doc/api/ | ||
| .dart_tool/ | ||
| .flutter-plugins | ||
| .packages | ||
| .pub-cache/ | ||
| .pub/ | ||
| /build/ | ||
|
|
||
| # Web related | ||
| lib/generated_plugin_registrant.dart | ||
|
|
||
| # Exceptions to above rules. | ||
| !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # This file tracks properties of this Flutter project. | ||
| # Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
| # | ||
| # This file should be version controlled and should not be manually edited. | ||
|
|
||
| version: | ||
| revision: 0e605cc4dd83137f785769dea5e8ae7da1afb361 | ||
| channel: master | ||
|
|
||
| project_type: app |
7 changes: 7 additions & 0 deletions
7
packages/flutter_android_lifecycle/example/android/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| gradle-wrapper.jar | ||
| /.gradle | ||
| /captures/ | ||
| /gradlew | ||
| /gradlew.bat | ||
| /local.properties | ||
| GeneratedPluginRegistrant.java |
61 changes: 61 additions & 0 deletions
61
packages/flutter_android_lifecycle/example/android/app/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| def localProperties = new Properties() | ||
| def localPropertiesFile = rootProject.file('local.properties') | ||
| if (localPropertiesFile.exists()) { | ||
| localPropertiesFile.withReader('UTF-8') { reader -> | ||
| localProperties.load(reader) | ||
| } | ||
| } | ||
|
|
||
| def flutterRoot = localProperties.getProperty('flutter.sdk') | ||
| if (flutterRoot == null) { | ||
| throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") | ||
| } | ||
|
|
||
| def flutterVersionCode = localProperties.getProperty('flutter.versionCode') | ||
| if (flutterVersionCode == null) { | ||
| flutterVersionCode = '1' | ||
| } | ||
|
|
||
| def flutterVersionName = localProperties.getProperty('flutter.versionName') | ||
| if (flutterVersionName == null) { | ||
| flutterVersionName = '1.0' | ||
| } | ||
|
|
||
| apply plugin: 'com.android.application' | ||
| apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" | ||
|
|
||
| android { | ||
| compileSdkVersion 28 | ||
|
|
||
| lintOptions { | ||
| disable 'InvalidPackage' | ||
| } | ||
|
|
||
| defaultConfig { | ||
| // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). | ||
| applicationId "io.flutter.plugins.flutter_android_lifecycle_example" | ||
| minSdkVersion 16 | ||
| targetSdkVersion 28 | ||
| versionCode flutterVersionCode.toInteger() | ||
| versionName flutterVersionName | ||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| // TODO: Add your own signing config for the release build. | ||
| // Signing with the debug keys for now, so `flutter run --release` works. | ||
| signingConfig signingConfigs.debug | ||
| } | ||
| } | ||
| } | ||
|
|
||
| flutter { | ||
| source '../..' | ||
| } | ||
|
|
||
| dependencies { | ||
| testImplementation 'junit:junit:4.12' | ||
| androidTestImplementation 'androidx.test:runner:1.1.1' | ||
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' | ||
| } |
17 changes: 17 additions & 0 deletions
17
...st/java/io/flutter/plugins/flutter_android_lifecycle_example/EmbeddingV1ActivityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.flutter_android_lifecycle_example; | ||
|
|
||
| import androidx.test.rule.ActivityTestRule; | ||
| import dev.flutter.plugins.e2e.FlutterRunner; | ||
| import org.junit.Rule; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| @RunWith(FlutterRunner.class) | ||
| public class EmbeddingV1ActivityTest { | ||
| @Rule | ||
| public ActivityTestRule<EmbeddingV1Activity> rule = | ||
| new ActivityTestRule<>(EmbeddingV1Activity.class); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.