我正在尝试在我的 android Flutter 应用程序中实现 Instabug(崩溃分析),但我很困惑我应该在哪里将此代码输入到我的项目中。
在 Application 子类的 onCreate() 方法中初始化 Instabug:
Flutter 包中 Application 子类的 onCreate() 方法在哪里?如果我需要创建一个,我会在哪里制作它?
我正在尝试在我的 android Flutter 应用程序中实现 Instabug(崩溃分析),但我很困惑我应该在哪里将此代码输入到我的项目中。
在 Application 子类的 onCreate() 方法中初始化 Instabug:
Flutter 包中 Application 子类的 onCreate() 方法在哪里?如果我需要创建一个,我会在哪里制作它?
在颤振应用根目录中,
跟着android/app/src/main/kotlin/your package/
,你就知道了MainActivity
。
只需创建一个继承自FlutterApplication
相同路径的类MainActivity
class CustomApplication : FlutterApplication {
override fun onCreate() {
super.onCreate()
// Paste here the integration codes of instabug
}
}
然后去Manifest,android/app/src/main/kotlin/your package/AndroidManifest.xml
修改应用部分
<application
android:name=".CustomApplication"
...
</application>
您可以使用包https://pub.dev/packages/instabug_flutter
,
请Android
按照步骤Readme
1 中提到的步骤:将以下 Maven 存储库添加到您的项目级别build.gradle
allprojects {
repositories {
maven {
url "https://sdks.instabug.com/nexus/repository/instabug-cp"
}
}
}
第 2 步:创建一个Java
扩展的新类FlutterApplication
并将其添加到您的AndroidManifest.xml
.
<application
android:name=".CustomFlutterApplication"
...
</application>
第 3 步:在您新创建的CustomFlutterApplication
类中,覆盖onCreate()
并添加以下代码。
CustomFlutterApplication
来自https://github.com/Instabug/Instabug-Flutter/blob/master/example/android/app/src/main/java/com/instabug/instabugflutterexample/CustomFlutterApplication.java的代码
package com.instabug.instabugflutterexample;
import io.flutter.app.FlutterApplication;
import com.instabug.instabugflutter.InstabugFlutterPlugin;
import java.util.ArrayList;
public class CustomFlutterApplication extends FlutterApplication {
@Override
public void onCreate() {
super.onCreate();
ArrayList<String> invocation_events = new ArrayList<>();
invocation_events.add(InstabugFlutterPlugin.INVOCATION_EVENT_FLOATING_BUTTON);
InstabugFlutterPlugin instabug = new InstabugFlutterPlugin();
instabug.start(CustomFlutterApplication.this, "2d355f559ea67051a56fce82603f8e41", invocation_events);
instabug.setWelcomeMessageMode("WelcomeMessageMode.disabled");
}
}
Dart
示例代码片段https://github.com/Instabug/Instabug-Flutter/blob/master/example/lib/main.dart
import 'dart:async';
import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:instabug_flutter/Instabug.dart';
import 'package:instabug_flutter/BugReporting.dart';
import 'package:instabug_flutter/Surveys.dart';
import 'package:instabug_flutter/FeatureRequests.dart';
import 'package:instabug_flutter/CrashReporting.dart';
void main() async {
FlutterError.onError = (FlutterErrorDetails details) {
Zone.current.handleUncaughtError(details.exception, details.stack);
};
runZoned<Future<void>>(() async {
runApp(MyApp());
}, onError: (dynamic error, StackTrace stackTrace) {
CrashReporting.reportCrash(error, stackTrace);
});
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
if (Platform.isIOS) {
Instabug.start('efa41f402620b5654f2af2b86e387029',
<InvocationEvent>[InvocationEvent.floatingButton]);
}
initPlatformState();
}
在深入研究他们在 instabug github repo 上的示例后,我能够解决这个问题。
package com.example.app;
import io.flutter.app.FlutterApplication;
import com.instabug.instabugflutter.InstabugFlutterPlugin;
import java.util.ArrayList;
public class CustomFlutterApplication extends FlutterApplication {
@Override
public void onCreate() {
super.onCreate();
ArrayList<String> invocationEvents = new ArrayList<>();
invocationEvents.add(InstabugFlutterPlugin.INVOCATION_EVENT_SHAKE);
new InstabugFlutterPlugin().start(CustomFlutterApplication.this, "<API KEY>", invocationEvents);
}
}
在您的 AndroidManifest.xml 中,替换android:name="io.flutter.app.FlutterApplication"
为android:name=".CustomFlutterApplication"
在您的项目级别 build.gradle 中,
allprojects {
repositories {
maven {
url "https://sdks.instabug.com/nexus/repository/instabug-cp"
}
}
}