0

我正在尝试在我的 android Flutter 应用程序中实现 Instabug(崩溃分析),但我很困惑我应该在哪里将此代码输入到我的项目中。

在 Application 子类的 onCreate() 方法中初始化 Instabug:

来自 instabug 的步骤图像

Flutter 包中 Application 子类的 onCreate() 方法在哪里?如果我需要创建一个,我会在哪里制作它?

4

3 回答 3

1

在颤振应用根目录中,

跟着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>
于 2021-01-19T22:52:53.267 回答
0

您可以使用包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();
  }
于 2021-01-20T01:42:53.380 回答
0

在深入研究他们在 instabug github repo 上的示例后,我能够解决这个问题。

  1. 在 main/kotlin/com/example/app 下创建一个新的 java 文件 CustomFlutterApplication.java。这是 MainActivity.kt 所在的位置。
    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);
      }
    }
  1. 在您的 AndroidManifest.xml 中,替换android:name="io.flutter.app.FlutterApplication"android:name=".CustomFlutterApplication"

  2. 在您的项目级别 build.gradle 中,

    allprojects {
        repositories {
            maven {
                url "https://sdks.instabug.com/nexus/repository/instabug-cp"
            }
        }
    }
  1. 现在构建你的项目
于 2021-02-19T21:07:56.190 回答