From fafaf8d03659c49cea3f751ac5d71f57ed26ee8b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 9 Feb 2021 23:25:28 +1100 Subject: [PATCH] Updated to embedding v2 Ref: https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects Fixes for sentry reporting Ref: https://github.com/flutter/flutter/issues/48972 --- android/app/src/main/AndroidManifest.xml | 17 ++++- .../inventree/inventree_app/MainActivity.java | 9 +-- android/app/src/main/res/values/styles.xml | 4 + lib/main.dart | 74 +++++++++++++------ pubspec.lock | 9 ++- pubspec.yaml | 2 +- 6 files changed, 78 insertions(+), 37 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index d0832573..dc125db9 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -7,9 +7,11 @@ additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> + + + + + + - diff --git a/android/app/src/main/java/inventree/inventree_app/MainActivity.java b/android/app/src/main/java/inventree/inventree_app/MainActivity.java index 460b0e13..6d88807f 100644 --- a/android/app/src/main/java/inventree/inventree_app/MainActivity.java +++ b/android/app/src/main/java/inventree/inventree_app/MainActivity.java @@ -1,13 +1,6 @@ package inventree.inventree_app; -import android.os.Bundle; -import io.flutter.app.FlutterActivity; -import io.flutter.plugins.GeneratedPluginRegistrant; +import io.flutter.embedding.android.FlutterActivity; public class MainActivity extends FlutterActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - GeneratedPluginRegistrant.registerWith(this); - } } diff --git a/android/app/src/main/res/values/styles.xml b/android/app/src/main/res/values/styles.xml index 00fa4417..f09400a8 100644 --- a/android/app/src/main/res/values/styles.xml +++ b/android/app/src/main/res/values/styles.xml @@ -5,4 +5,8 @@ Flutter draws its first frame --> @drawable/launch_background + + diff --git a/lib/main.dart b/lib/main.dart index f0a14c94..5eaabe32 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:io'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; @@ -9,13 +10,7 @@ import 'package:flutter/material.dart'; import 'dsn.dart'; -import 'package:sentry/sentry.dart'; - -// Use the secret app key -final SentryClient _sentry = SentryClient( - SentryOptions( - dsn: SENTRY_DSN_KEY, - )); +import 'package:sentry_flutter/sentry_flutter.dart'; bool isInDebugMode() { bool inDebugMode = false; @@ -26,33 +21,66 @@ bool isInDebugMode() { } Future _reportError(dynamic error, dynamic stackTrace) async { - // Print the exception to the console. + print('Caught error: $error'); + + // Errors thrown in development mode are unlikely to be interesting. You can + // check if you are running in dev mode using an assertion and omit sending + // the report. if (isInDebugMode()) { - // Print the full stacktrace in debug mode. print(stackTrace); + print('In dev mode. Not sending report to Sentry.io.'); return; - } else { - try { - await _sentry.captureException( - error, - stackTrace: stackTrace - ); - } catch (e) { - print("Sending error report to sentry.io failed: ${e}"); - print("Original error: ${error}"); - } } + + print('Reporting to Sentry.io...'); + + // Extract some platform information + if (Platform.isIOS) { + + } else if (Platform.isAndroid) { + + } + + Sentry.captureException(error, stackTrace: stackTrace).catchError((error) { + print("Error uploading information to Sentry.io:"); + print(error); + }).then((response) { + print("Uploaded information to Sentry.io : ${response.toString()}"); + }); } void main() async { + + await Sentry.init((options) { + options.dsn = SENTRY_DSN_KEY; + }, + //appRunner: () => runApp(InvenTreeApp()) + ); + + await runZonedGuarded>(() async { + WidgetsFlutterBinding.ensureInitialized(); - runZoned>(() async { - runApp(InvenTreeApp()); - }, onError: _reportError - ); + // This captures errors reported by the Flutter framework. + FlutterError.onError = (FlutterErrorDetails details) async { + if (isInDebugMode()) { + // In development mode simply print to console. + FlutterError.dumpErrorToConsole(details); + } else { + // In production mode report to the application zone to report to + // Sentry. + Zone.current.handleUncaughtError(details.exception, details.stack); + } + }; + + runApp(InvenTreeApp()); + + }, (Object error, StackTrace stackTrace) { + _reportError(error, stackTrace); + }); + } class InvenTreeApp extends StatelessWidget { diff --git a/pubspec.lock b/pubspec.lock index 8d39f12c..baeae699 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -421,12 +421,19 @@ packages: source: hosted version: "2.4.9" sentry: - dependency: "direct main" + dependency: transitive description: name: sentry url: "https://pub.dartlang.org" source: hosted version: "4.0.4" + sentry_flutter: + dependency: "direct main" + description: + name: sentry_flutter + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.4" shared_preferences: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 0902d4da..fea2174a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,7 +35,7 @@ dependencies: device_info: ^1.0.0 # Information about the device font_awesome_flutter: ^8.8.1 # FontAwesome icon set flutter_speed_dial: ^1.2.5 # FAB menu elements - sentry: ^4.0.4 # Error reporting + sentry_flutter: ^4.0.4 # Error reporting flutter_typeahead: ^1.8.1 # Auto-complete input field image_picker: ^0.6.6 # Select or take photos url_launcher: ^5.7.10 # Open link in system browser