Flutterfire: [firebase_messaging] Integration problem with Kotlin and plugins

Created on 10 Apr 2020  Â·  7Comments  Â·  Source: FirebaseExtended/flutterfire

Hello,
I have troubles for integrating Firebase Messaging in my project, in Kotlin files.
Application doesn't start, and crash on both emulator and phone.

Is it because I'm using local_auth package, which requires FlutterFragmentActivity ??

The only "relevant" error I can get in logcat is this:

AndroidRuntime: FATAL EXCEPTION: main
    Process: com.scalz.myapp, PID: 4832
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.scalz.myapp/com.scalz.myapp.Application}: java.lang.ClassCastException: com.scalz.myapp.Application cannot be cast to android.app.Activity

I think I carefully followed the flutter messaging integration docs, and also tried different solution found here, or on stackoverflow, but nothing is working in my case.
Could you help me on this please? this would be great!

This is my setup:
flutter doctor -v

[√] Flutter (Channel stable, v1.12.13+hotfix.8, on Microsoft Windows [version 10.0.18363.720], locale fr-FR)
    • Flutter version 1.12.13+hotfix.8 at D:\Travail\flutter\flutter
    • Framework revision 0b8abb4724 (8 weeks ago), 2020-02-11 11:44:36 -0800
    • Engine revision e1e6ced81d
    • Dart version 2.7.0


[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\android-sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = C:\android-sdk
    • Java binary at: C:\Program Files\Android\android-studioCanary\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
    • All Android licenses accepted.

[√] Android Studio (version 3.6)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 45.0.1
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)

[√] Connected device (1 available)
    • AOSP on IA Emulator • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)

Pubspec.yaml

local_auth: ^0.6.1+3
flutter_local_notifications: ^1.2.2
firebase_messaging: ^6.0.13
firebase_core: ^0.4.4+2

manifest

...
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="myapp"
        android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".Application"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
...

build.gradle for project

dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.3' //4.3.2
}

build.gradle for app

apply plugin: 'com.google.gms.google-services'
...
implementation "androidx.core:core:1.1.0"
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-messaging:20.1.5'

MainActivity.kt for using local_auth package which requires FlutterFragmentActivity

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.plugins.GeneratedPluginRegistrant
import android.os.Bundle    
class MainActivity: FlutterFragmentActivity() {  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        GeneratedPluginRegistrant.registerWith(flutterEngine)
    }
}

Application.kt for Firebase Messaging

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }
}

Note: I replaced MainActivity in manifest to Application. And also tried commenting configureFlutterEngine in MainActivity.kt

And I get this error:

AndroidRuntime: FATAL EXCEPTION: main
    Process: com.scalz.myapp, PID: 4832
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.scalz.myapp/com.scalz.myapp.Application}: java.lang.ClassCastException: com.scalz.myapp.Application cannot be cast to android.app.Activity

If I understand it right, it seems it can't cast or find my fragmentActivity.. how can I register it, and fix my issue?
This is my last package to include for finishing my project but I'm stuck on this. Or maybe someone has a working Kotlin example for latest local_auth+FCM??

In advance thank you for help.

bug

Most helpful comment

@scalz, where is the correct tag in manifest? I thought it should be there as well and my build is crashing

All 7 comments

woops, my bad!
I found my mistake. I edited the wrong tag in manifest..
It's working great. Awesome!

@scalz, where is the correct tag in manifest? I thought it should be there as well and my build is crashing

same here. Which tag should you edit in manifest then? @scalz

@mnorhamizan @sakina1403

Solution

<activity
    android:name=".MainActivity"
<application
    android:name=".Application"

@aadil058, I already did this but still crashing

Can you share your error logs please?

Was this page helpful?
0 / 5 - 0 ratings