Skip to content

Commit d277542

Browse files
authored
[firebase_analytics] Support Android v2 embedding (#1266)
* Support Android v2 embedding for firebase_analytics plugin.
1 parent 98c0dab commit d277542

File tree

14 files changed

+167
-19
lines changed

14 files changed

+167
-19
lines changed

packages/firebase_analytics/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.0.3
2+
3+
* Support the v2 Android embedding.
4+
15
## 5.0.2
26

37
* Fixed `setAnalyticsCollectionEnabled` on iOS.

packages/firebase_analytics/android/build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,29 @@ android {
5050
}
5151
}
5252

53+
// TODO(kroikie): Remove this hack once androidx.lifecycle is included on stable. https://github.com/flutter/flutter/issues/42348
54+
afterEvaluate {
55+
def containsEmbeddingDependencies = false
56+
for (def configuration : configurations.all) {
57+
for (def dependency : configuration.dependencies) {
58+
if (dependency.group == 'io.flutter' &&
59+
dependency.name.startsWith('flutter_embedding') &&
60+
dependency.isTransitive())
61+
{
62+
containsEmbeddingDependencies = true
63+
break
64+
}
65+
}
66+
}
67+
if (!containsEmbeddingDependencies) {
68+
android {
69+
dependencies {
70+
def lifecycle_version = "2.1.0"
71+
api "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
72+
api "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
73+
}
74+
}
75+
}
76+
}
77+
5378
apply from: file("./user-agent.gradle")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
org.gradle.jvmargs=-Xmx1536M
2+
android.useAndroidX=true
3+
android.enableJetifier=true

packages/firebase_analytics/android/src/main/java/io/flutter/plugins/firebaseanalytics/FirebaseAnalyticsPlugin.java

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
package io.flutter.plugins.firebaseanalytics;
66

77
import android.app.Activity;
8+
import android.content.Context;
89
import android.os.Bundle;
10+
import androidx.annotation.NonNull;
911
import com.google.firebase.FirebaseApp;
1012
import com.google.firebase.analytics.FirebaseAnalytics;
13+
import io.flutter.embedding.engine.plugins.FlutterPlugin;
14+
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
15+
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
16+
import io.flutter.plugin.common.BinaryMessenger;
1117
import io.flutter.plugin.common.MethodCall;
1218
import io.flutter.plugin.common.MethodChannel;
1319
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
@@ -16,22 +22,54 @@
1622
import java.util.Map;
1723

1824
/** Flutter plugin for Firebase Analytics. */
19-
public class FirebaseAnalyticsPlugin implements MethodCallHandler {
20-
private final PluginRegistry.Registrar registrar;
21-
private final FirebaseAnalytics firebaseAnalytics;
25+
public class FirebaseAnalyticsPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware {
26+
private FirebaseAnalytics firebaseAnalytics;
27+
private MethodChannel methodChannel;
28+
private Activity activity;
2229

2330
public static void registerWith(PluginRegistry.Registrar registrar) {
24-
final MethodChannel channel =
25-
new MethodChannel(registrar.messenger(), "plugins.flutter.io/firebase_analytics");
26-
channel.setMethodCallHandler(new FirebaseAnalyticsPlugin(registrar));
31+
FirebaseAnalyticsPlugin instance = new FirebaseAnalyticsPlugin();
32+
instance.setActivity(registrar.activity());
33+
instance.onAttachedToEngine(registrar.context(), registrar.messenger());
2734
}
2835

29-
private FirebaseAnalyticsPlugin(PluginRegistry.Registrar registrar) {
30-
this.registrar = registrar;
31-
FirebaseApp.initializeApp(registrar.context());
32-
this.firebaseAnalytics = FirebaseAnalytics.getInstance(registrar.context());
36+
@Override
37+
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
38+
onAttachedToEngine(
39+
binding.getApplicationContext(), binding.getFlutterEngine().getDartExecutor());
40+
}
41+
42+
private void onAttachedToEngine(Context applicationContext, BinaryMessenger binaryMessenger) {
43+
FirebaseApp.initializeApp(applicationContext);
44+
firebaseAnalytics = FirebaseAnalytics.getInstance(applicationContext);
45+
methodChannel = new MethodChannel(binaryMessenger, "plugins.flutter.io/firebase_analytics");
46+
methodChannel.setMethodCallHandler(this);
47+
}
48+
49+
@Override
50+
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
51+
firebaseAnalytics = null;
52+
methodChannel = null;
3353
}
3454

55+
@Override
56+
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
57+
setActivity(binding.getActivity());
58+
}
59+
60+
@Override
61+
public void onDetachedFromActivityForConfigChanges() {
62+
setActivity(null);
63+
}
64+
65+
@Override
66+
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
67+
setActivity(binding.getActivity());
68+
}
69+
70+
@Override
71+
public void onDetachedFromActivity() {}
72+
3573
@Override
3674
public void onMethodCall(MethodCall call, Result result) {
3775
switch (call.method) {
@@ -62,6 +100,10 @@ public void onMethodCall(MethodCall call, Result result) {
62100
}
63101
}
64102

103+
private void setActivity(Activity activity) {
104+
this.activity = activity;
105+
}
106+
65107
private void handleLogEvent(MethodCall call, Result result) {
66108

67109
final String eventName = call.argument("name");
@@ -78,7 +120,6 @@ private void handleSetUserId(MethodCall call, Result result) {
78120
}
79121

80122
private void handleSetCurrentScreen(MethodCall call, Result result) {
81-
Activity activity = registrar.activity();
82123
if (activity == null) {
83124
result.error("no_activity", "handleSetCurrentScreen requires a foreground activity", null);
84125
return;

packages/firebase_analytics/example/android/app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ flutter {
5454
}
5555

5656
dependencies {
57+
androidTestImplementation 'androidx.test:runner:1.2.0'
58+
androidTestImplementation 'androidx.test:rules:1.2.0'
59+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
5760
}
5861

5962
apply plugin: 'com.google.gms.google-services'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.firebaseanalyticsexample;
6+
7+
import androidx.test.rule.ActivityTestRule;
8+
import dev.flutter.plugins.e2e.FlutterRunner;
9+
import org.junit.Rule;
10+
import org.junit.runner.RunWith;
11+
12+
@RunWith(FlutterRunner.class)
13+
public class EmbeddingV1ActivityTest {
14+
@Rule
15+
public ActivityTestRule<EmbeddingV1Activity> rule =
16+
new ActivityTestRule<>(EmbeddingV1Activity.class);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.firebaseanalyticsexample;
6+
7+
import androidx.test.rule.ActivityTestRule;
8+
import dev.flutter.plugins.e2e.FlutterRunner;
9+
import org.junit.Rule;
10+
import org.junit.runner.RunWith;
11+
12+
@RunWith(FlutterRunner.class)
13+
public class MainActivityTest {
14+
@Rule public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
15+
}

packages/firebase_analytics/example/android/app/src/main/AndroidManifest.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,16 @@
1515
<category android:name="android.intent.category.LAUNCHER"/>
1616
</intent-filter>
1717
</activity>
18+
<activity android:name=".EmbeddingV1Activity"
19+
android:launchMode="singleTop"
20+
android:theme="@android:style/Theme.Black.NoTitleBar"
21+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
22+
android:hardwareAccelerated="true"
23+
android:windowSoftInputMode="adjustResize">
24+
<intent-filter>
25+
<action android:name="android.intent.action.MAIN"/>
26+
<category android:name="android.intent.category.LAUNCHER"/>
27+
</intent-filter>
28+
</activity>
1829
</application>
1930
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.flutter.plugins.firebaseanalyticsexample;
2+
3+
import android.os.Bundle;
4+
import io.flutter.app.FlutterActivity;
5+
import io.flutter.plugins.GeneratedPluginRegistrant;
6+
7+
public class EmbeddingV1Activity extends FlutterActivity {
8+
@Override
9+
protected void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
GeneratedPluginRegistrant.registerWith(this);
12+
}
13+
}

packages/firebase_analytics/example/android/app/src/main/java/io/flutter/plugins/firebaseanalyticsexample/MainActivity.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
package io.flutter.plugins.firebaseanalyticsexample;
66

7-
import android.os.Bundle;
8-
import io.flutter.app.FlutterActivity;
9-
import io.flutter.plugins.GeneratedPluginRegistrant;
7+
import dev.flutter.plugins.e2e.E2EPlugin;
8+
import io.flutter.embedding.android.FlutterActivity;
9+
import io.flutter.embedding.engine.FlutterEngine;
10+
import io.flutter.plugins.firebaseanalytics.FirebaseAnalyticsPlugin;
1011

1112
public class MainActivity extends FlutterActivity {
1213
@Override
13-
protected void onCreate(Bundle savedInstanceState) {
14-
super.onCreate(savedInstanceState);
15-
GeneratedPluginRegistrant.registerWith(this);
14+
public void configureFlutterEngine(FlutterEngine flutterEngine) {
15+
flutterEngine.getPlugins().add(new FirebaseAnalyticsPlugin());
16+
flutterEngine.getPlugins().add(new E2EPlugin());
1617
}
1718
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
org.gradle.jvmargs=-Xmx1536M
2+
android.enableR8=true

packages/firebase_analytics/example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dependencies:
77
firebase_analytics:
88
path: ../
99
firebase_core: ^0.4.0
10+
e2e: ^0.2.1
1011

1112
dev_dependencies:
1213
flutter_test:

packages/firebase_analytics/pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Google Analytics for Firebase, an app measuremen
33
solution that provides insight on app usage and user engagement on Android and iOS.
44
author: Flutter Team <[email protected]>
55
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_analytics
6-
version: 5.0.2
6+
version: 5.0.3
77

88
flutter:
99
plugin:
@@ -17,6 +17,7 @@ dependencies:
1717
sdk: flutter
1818

1919
dev_dependencies:
20+
e2e: ^0.2.1
2021
mockito: 3.0.0
2122
flutter_test:
2223
sdk: flutter
@@ -26,4 +27,4 @@ dev_dependencies:
2627

2728
environment:
2829
sdk: ">=2.0.0-dev.28.0 <3.0.0"
29-
flutter: ">=1.5.0 <2.0.0"
30+
flutter: ">=1.9.1+hotfix.4 <2.0.0"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:e2e/e2e.dart';
3+
4+
import 'package:firebase_analytics/firebase_analytics.dart';
5+
6+
void main() {
7+
E2EWidgetsFlutterBinding.ensureInitialized();
8+
9+
testWidgets('Can log event', (WidgetTester tester) async {
10+
final Future<void> future = FirebaseAnalytics().logEvent(name: "foo_event");
11+
expect(future, completes);
12+
});
13+
}

0 commit comments

Comments
 (0)