How to integrate instabug SDK into flutter app

776 Views Asked by At

I'm trying to implement Instabug (crash analytics) into my android flutter app, and I'm confused as to where I should input this code into my project.

Initialize Instabug in the onCreate() method of your Application subclass:

Image of the step from instabug

Where is the onCreate() method for the Application subclass in a flutter package? And if I need to create one, where would I make it?

3

There are 3 best solutions below

5
blackkara On

In the flutter app root,

Follow android/app/src/main/kotlin/your package/, then you'll see MainActivity.

Just create a class which is inherits from FlutterApplication in same path of MainActivity

class CustomApplication : FlutterApplication {
    override fun onCreate() {
        super.onCreate()
        // Paste here the integration codes of instabug
    }
}

Then go to Manifest, android/app/src/main/kotlin/your package/AndroidManifest.xml, and modify application section

<application
   android:name=".CustomApplication"
   ...
</application>
4
chunhunghan On

You can use package https://pub.dev/packages/instabug_flutter
For Android, please follow step mentioned in Readme
Step 1: Add the following Maven repository to your project level build.gradle

allprojects {
    repositories {
        maven {
            url "https://sdks.instabug.com/nexus/repository/instabug-cp"
        }
    }
}

Step 2: Create a new Java class that extends FlutterApplication and add it to your AndroidManifest.xml.

<application
    android:name=".CustomFlutterApplication"
    ...
</application>

Step 3: In your newly created CustomFlutterApplication class, override onCreate() and add the following code.

code of CustomFlutterApplication from 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 example code snippet 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();
  }
0
kakaly On

After digging into their example on instabug github repo, I was able to resolve the issue.

  1. Create a new java file, CustomFlutterApplication.java under main/kotlin/com/example/app. This is where MainActivity.kt is present.
    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. In your AndroidManifest.xml, replace android:name="io.flutter.app.FlutterApplication" with android:name=".CustomFlutterApplication"

  2. In your project level build.gradle,

    allprojects {
        repositories {
            maven {
                url "https://sdks.instabug.com/nexus/repository/instabug-cp"
            }
        }
    }
  1. Now build your project