Skip to content

Commit

Permalink
[crashlytics] Migrate to new Crashlytics SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
axel-op committed Apr 2, 2020
1 parent bdb95fc commit 14966df
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 118 deletions.
9 changes: 9 additions & 0 deletions packages/firebase_crashlytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.2.0

* **Breaking**: Migration to the new Firebase Crashlytics SDK. Follow the [migration guide](https://firebase.google.com/docs/crashlytics/upgrade-sdk?platform=android) to update your app.
* **Breaking**: the following methods have been removed:
* `setUserEmail`
* `setUserEmail`
* `getVersion`
* `isDebuggable`

## 0.1.3+3

* Fix for missing UserAgent.h compilation failures.
Expand Down
5 changes: 1 addition & 4 deletions packages/firebase_crashlytics/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ rootProject.allprojects {
repositories {
google()
jcenter()
maven {
url 'https://maven.fabric.io/public'
}
}
}

Expand All @@ -37,7 +34,7 @@ android {
}

dependencies {
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
implementation 'com.google.firebase:firebase-crashlytics:17.0.0-beta03'
implementation 'com.google.firebase:firebase-common:16.1.0'
implementation 'androidx.annotation:annotation:1.1.0'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import android.content.Context;
import android.util.Log;
import com.crashlytics.android.Crashlytics;
import io.fabric.sdk.android.Fabric;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
Expand Down Expand Up @@ -42,11 +41,6 @@ private static MethodChannel setup(BinaryMessenger binaryMessenger, Context cont
final MethodChannel channel =
new MethodChannel(binaryMessenger, "plugins.flutter.io/firebase_crashlytics");
channel.setMethodCallHandler(new FirebaseCrashlyticsPlugin());

if (!Fabric.isInitialized()) {
Fabric.with(context, new Crashlytics());
}

return channel;
}

Expand All @@ -57,28 +51,29 @@ public static void registerWith(Registrar registrar) {

@Override
public void onMethodCall(MethodCall call, Result result) {
final FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
if (call.method.equals("Crashlytics#onError")) {
// Add logs.
List<String> logs = call.argument("logs");
for (String log : logs) {
Crashlytics.log(log);
crashlytics.log(log);
}

// Set keys.
List<Map<String, Object>> keys = call.argument("keys");
for (Map<String, Object> key : keys) {
switch ((String) key.get("type")) {
case "int":
Crashlytics.setInt((String) key.get("key"), (int) key.get("value"));
crashlytics.setCustomKey((String) key.get("key"), (int) key.get("value"));
break;
case "double":
Crashlytics.setDouble((String) key.get("key"), (double) key.get("value"));
crashlytics.setCustomKey((String) key.get("key"), (double) key.get("value"));
break;
case "string":
Crashlytics.setString((String) key.get("key"), (String) key.get("value"));
crashlytics.setCustomKey((String) key.get("key"), (String) key.get("value"));
break;
case "boolean":
Crashlytics.setBool((String) key.get("key"), (boolean) key.get("value"));
crashlytics.setCustomKey((String) key.get("key"), (boolean) key.get("value"));
break;
}
}
Expand All @@ -96,30 +91,20 @@ public void onMethodCall(MethodCall call, Result result) {
}
exception.setStackTrace(elements.toArray(new StackTraceElement[elements.size()]));

Crashlytics.setString("exception", (String) call.argument("exception"));
crashlytics.setCustomKey("exception", (String) call.argument("exception"));

// Set a "reason" (to match iOS) to show where the exception was thrown.
final String context = call.argument("context");
if (context != null) Crashlytics.setString("reason", "thrown " + context);
if (context != null) crashlytics.setCustomKey("reason", "thrown " + context);

// Log information.
final String information = call.argument("information");
if (information != null && !information.isEmpty()) Crashlytics.log(information);
if (information != null && !information.isEmpty()) crashlytics.log(information);

Crashlytics.logException(exception);
crashlytics.recordException(exception);
result.success("Error reported to Crashlytics.");
} else if (call.method.equals("Crashlytics#isDebuggable")) {
result.success(Fabric.isDebuggable());
} else if (call.method.equals("Crashlytics#getVersion")) {
result.success(Crashlytics.getInstance().getVersion());
} else if (call.method.equals("Crashlytics#setUserEmail")) {
Crashlytics.setUserEmail((String) call.argument("email"));
result.success(null);
} else if (call.method.equals("Crashlytics#setUserIdentifier")) {
Crashlytics.setUserIdentifier((String) call.argument("identifier"));
result.success(null);
} else if (call.method.equals("Crashlytics#setUserName")) {
Crashlytics.setUserName((String) call.argument("name"));
crashlytics.setUserId((String) call.argument("identifier"));
result.success(null);
} else {
result.notImplemented();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ dependencies {
implementation "android.arch.lifecycle:extensions:1.1.1"
}

apply plugin: 'io.fabric'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package io.flutter.plugins.firebase.crashlytics.firebasecrashlytics;

import androidx.test.rule.ActivityTestRule;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package io.flutter.plugins.firebase.crashlytics.firebasecrashlyticsexample;

import android.os.Bundle;
Expand Down
5 changes: 1 addition & 4 deletions packages/firebase_crashlytics/example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ buildscript {
repositories {
google()
jcenter()
maven {
url 'https://maven.fabric.io/public'
}
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.3.0'
classpath 'io.fabric.tools:gradle:1.26.1'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.0.0-beta03'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ void main() {
test('recordFlutterError', () async {
// This is currently only testing that we can log errors without crashing.
final Crashlytics crashlytics = Crashlytics.instance;
await crashlytics.setUserName('testing');
await crashlytics.setUserIdentifier('hello');
crashlytics.setBool('testBool', true);
crashlytics.setInt('testInt', 42);
Expand Down
29 changes: 0 additions & 29 deletions packages/firebase_crashlytics/lib/src/firebase_crashlytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,6 @@ class Crashlytics {
throw StateError('Error thrown by Crashlytics plugin');
}

/// Reports the global value for debug mode.
/// TODO(kroikie): Clarify what this means in context of both Android and iOS.
Future<bool> isDebuggable() async {
final bool result =
await channel.invokeMethod<bool>('Crashlytics#isDebuggable');
return result;
}

/// Returns Crashlytics SDK version.
Future<String> getVersion() async {
final String result =
await channel.invokeMethod<String>('Crashlytics#getVersion');
return result;
}

/// Add text logging that will be sent with your next report. `msg` will be
/// printed to the console when in debug mode. Each report has a rolling max
/// of 64k of logs, older logs are removed to allow newer logs to fit within
Expand Down Expand Up @@ -110,27 +95,13 @@ class Crashlytics {
_setValue(key, value);
}

/// Optionally set a end-user's name or username for display within the
/// Crashlytics UI. Please be mindful of end-user's privacy.
Future<void> setUserEmail(String email) async {
await channel.invokeMethod<void>(
'Crashlytics#setUserEmail', <String, dynamic>{'email': email});
}

/// Specify a user identifier which will be visible in the Crashlytics UI.
/// Please be mindful of end-user's privacy.
Future<void> setUserIdentifier(String identifier) async {
await channel.invokeMethod<void>('Crashlytics#setUserIdentifier',
<String, dynamic>{'identifier': identifier});
}

/// Specify a user name which will be visible in the Crashlytics UI. Please
/// be mindful of end-user's privacy.
Future<void> setUserName(String name) async {
await channel.invokeMethod<void>(
'Crashlytics#setUserName', <String, dynamic>{'name': name});
}

List<Map<String, dynamic>> _prepareKeys() {
final List<Map<String, dynamic>> crashlyticsKeys = <Map<String, dynamic>>[];
for (String key in _keys.keys) {
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_crashlytics/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: firebase_crashlytics
description:
Flutter plugin for Firebase Crashlytics. It reports uncaught errors to the
Firebase console.
version: 0.1.3+3
version: 0.2.0
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_crashlytics

environment:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:e2e/e2e.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';

void main() {
E2EWidgetsFlutterBinding.ensureInitialized();

testWidgets('get version', (WidgetTester tester) async {
final String version = await Crashlytics.instance.getVersion();
expect(version, isNotNull);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('$Crashlytics', () {
group('Crashlytics', () {
final List<MethodCall> log = <MethodCall>[];

final Crashlytics crashlytics = Crashlytics.instance;
Expand All @@ -24,14 +24,8 @@ void main() {
switch (methodCall.method) {
case 'Crashlytics#onError':
return 'Error reported to Crashlytics.';
case 'Crashlytics#isDebuggable':
return true;
case 'Crashlytics#setUserEmail':
return true;
case 'Crashlytics#setUserIdentifier':
return true;
case 'Crashlytics#setUserName':
return true;
case 'Crashlytics#getVersion':
return '0.0.0+1';
default:
Expand Down Expand Up @@ -103,37 +97,10 @@ void main() {
expect(log[0].arguments['keys'][3]['type'], 'string');
});

test('isDebuggable', () async {
expect(await crashlytics.isDebuggable(), true);
expect(
log,
<Matcher>[
isMethodCall(
'Crashlytics#isDebuggable',
arguments: null,
)
],
);
});

test('crash', () {
expect(() => crashlytics.crash(), throwsStateError);
});

test('getVersion', () async {
await crashlytics.getVersion();
expect(log,
<Matcher>[isMethodCall('Crashlytics#getVersion', arguments: null)]);
});

test('setUserEmail', () async {
await crashlytics.setUserEmail('foo');
expect(log, <Matcher>[
isMethodCall('Crashlytics#setUserEmail',
arguments: <String, dynamic>{'email': 'foo'})
]);
});

test('setUserIdentifier', () async {
await crashlytics.setUserIdentifier('foo');
expect(log, <Matcher>[
Expand All @@ -142,14 +109,6 @@ void main() {
]);
});

test('setUserName', () async {
await crashlytics.setUserName('foo');
expect(log, <Matcher>[
isMethodCall('Crashlytics#setUserName',
arguments: <String, dynamic>{'name': 'foo'})
]);
});

test('getStackTraceElements with character index', () async {
final List<String> lines = <String>[
'package:flutter/src/widgets/framework.dart 3825:27 StatefulElement.build'
Expand Down

0 comments on commit 14966df

Please sign in to comment.