0

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?

jooliaju
  • 1
  • 1

3 Answers3

1

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>
blackkara
  • 4,900
  • 4
  • 28
  • 58
  • What would "FlutterApplication" be, and would I need to modify the android:name that is already din the AndroidManifest.xml file? – jooliaju Jan 20 '21 at 03:22
  • FlutterApplication is prepared by flutter. You just use it to make custom application class as i wrote. And yes you need to tell in the manifest by modifying android:name – blackkara Jan 20 '21 at 04:27
  • For FlutterApplication, I get "Cannot resolve symbol 'FlutterApplication'" – jooliaju Jan 20 '21 at 04:33
  • yes it could happen, but won't be a problem to run the app – blackkara Jan 20 '21 at 04:59
  • : Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class com.ju.test_app_v2.CustomFlutterApplication, unresolved supertypes: FlutterApplication – jooliaju Jan 20 '21 at 21:55
0

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();
  }
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
  • Where are you supposed to write theCustomFlutterApplication class? – jooliaju Jan 20 '21 at 04:11
  • you can see https://github.com/Instabug/Instabug-Flutter/blob/master/example/android/app/src/main/java/com/instabug/instabugflutterexample , this is directory structure – chunhunghan Jan 20 '21 at 04:13
  • Do you know how I could resolve this issue? : Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class com.ju.test_app_v2.CustomFlutterApplication, unresolved supertypes: FlutterApplication – jooliaju Jan 20 '21 at 21:55
  • I suggest you can directly download this repo and directly test it's example. after it work well. you can compare difference. – chunhunghan Jan 21 '21 at 00:47
0

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
kakaly
  • 17
  • 2