Skip to content

Commit 76a5726

Browse files
amirhmormih
authored andcommitted
Add plugin for Android lifecycle in embedding (flutter#2168)
1 parent 7ddd721 commit 76a5726

File tree

78 files changed

+1736
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1736
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.dart_tool/
3+
4+
.packages
5+
.pub/
6+
7+
build/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 0e605cc4dd83137f785769dea5e8ae7da1afb361
8+
channel: master
9+
10+
project_type: plugin
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## 1.0.0
2+
3+
* Introduces a `FlutterLifecycleAdapter`, which can be used by other plugins to obtain a `Lifecycle`
4+
reference from a `FlutterPluginBinding`.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Google Inc. nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Flutter Android Lifecycle Plugin
2+
3+
[![pub package](https://img.shields.io/pub/v/flutter_android_lifecycle.svg)](https://pub.dartlang.org/packages/flutter_android_lifecycle)
4+
5+
A Flutter plugin for Android to allow other Flutter plugins to access an Android `Lifecycle` object
6+
in the plugin's binding.
7+
8+
The purpose of having this plugin instead of exposing an Android `Lifecycle` object in the engine's
9+
Android embedding plugins API is to force plugins to have a pub constraint that signifies the
10+
major version of the Android `Lifecycle` API they expect.
11+
12+
## Installation
13+
14+
Add `flutter_android_lifecycle` as a [dependency in your pubspec.yaml file](https://flutter.io/using-packages/).
15+
16+
## Example
17+
18+
Use a `FlutterLifecycleAdapter` within another Flutter plugin's Android implementation, as shown
19+
below:
20+
21+
```java
22+
import androidx.lifecycle.Lifecycle;
23+
import io.flutter.embedding.engine.FlutterEngine;
24+
import io.flutter.embedding.engine.plugins.FlutterPlugin;
25+
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding;
26+
import io.flutter.embedding.engine.plugins.lifecycle.FlutterLifecycleAdapter;
27+
28+
public class MyPlugin implements FlutterPlugin {
29+
@Override
30+
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
31+
Lifecycle lifecycle = new FlutterLifecycleAdapter.getLifecycle(binding);
32+
33+
// Use lifecycle as desired.
34+
}
35+
36+
//...
37+
}
38+
```
39+
40+
[Feedback welcome](https://github.com/flutter/flutter/issues) and
41+
[Pull Requests](https://github.com/flutter/plugins/pulls) are most welcome!
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
group 'io.flutter.plugins.flutter_android_lifecycle'
2+
version '1.0'
3+
4+
buildscript {
5+
repositories {
6+
google()
7+
jcenter()
8+
}
9+
10+
dependencies {
11+
classpath 'com.android.tools.build:gradle:3.5.0'
12+
}
13+
}
14+
15+
rootProject.allprojects {
16+
repositories {
17+
google()
18+
jcenter()
19+
}
20+
}
21+
22+
apply plugin: 'com.android.library'
23+
24+
android {
25+
compileSdkVersion 28
26+
27+
defaultConfig {
28+
minSdkVersion 16
29+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
30+
}
31+
lintOptions {
32+
disable 'InvalidPackage'
33+
}
34+
35+
dependencies {
36+
implementation "androidx.annotation:annotation:1.1.0"
37+
}
38+
}
39+
40+
// TODO(amirh): Remove this hack once androidx.lifecycle is included on stable. https://github.com/flutter/flutter/issues/42348
41+
afterEvaluate {
42+
def containsEmbeddingDependencies = false
43+
for (def configuration : configurations.all) {
44+
for (def dependency : configuration.dependencies) {
45+
if (dependency.group == 'io.flutter' &&
46+
dependency.name.startsWith('flutter_embedding') &&
47+
dependency.isTransitive())
48+
{
49+
containsEmbeddingDependencies = true
50+
break
51+
}
52+
}
53+
}
54+
if (!containsEmbeddingDependencies) {
55+
android {
56+
dependencies {
57+
def lifecycle_version = "2.1.0"
58+
api "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
59+
api "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
60+
}
61+
}
62+
}
63+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
org.gradle.jvmargs=-Xmx1536M
2+
android.enableR8=true
3+
android.useAndroidX=true
4+
android.enableJetifier=true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
zipStoreBase=GRADLE_USER_HOME
4+
zipStorePath=wrapper/dists
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'flutter_android_lifecycle'

0 commit comments

Comments
 (0)