skip to Main Content

I have a project in flutter that relies on Firebase for authentication, firestore, and functions. However, I have a receiver class that extends BroadcastReceiver and runs in the background, on the android side of my application. Within this receiver class I utilize Firestore, Analytics, Auth and Cloud_Functions.

Given this unique android setup, I need to instantiate the following dependencies in my app level build.gradle file:

implementation 'com.google.firebase:firebase-firestore:24.3.1'
    implementation 'com.google.firebase:firebase-auth:21.0.8'
    implementation 'com.google.firebase:firebase-firestore-ktx:24.3.1'
    implementation 'com.google.firebase:firebase-messaging:23.0.8'
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation 'com.google.firebase:firebase-functions-ktx:20.1.1'
    implementation 'com.google.firebase:firebase-analytics-ktx:21.1.1'
    implementation platform('com.google.firebase:firebase-bom:30.4.0')
    implementation 'com.google.firebase:firebase-auth-ktx'

I also have the following in my Flutter pubspec.yaml file:

firebase_core: ^1.24.0
  cloud_firestore: ^3.5.1
  firebase_messaging: ^13.1.0
  cloud_functions: ^3.3.9
  firebase_auth: ^3.11.2

as the Flutter project itself handles a lot of other things as well that are dependent on Firebase (shared components between iOS and Android that use these libraries).

I’ve tested my app on just iOS and it builds and runs fine since I don’t use any kind of iOS specific classes in the background, and I’ve also run the android app by itself without flutter and it builds and runs fine. But when I bring those native android classes into my flutter project, I get runtime errors when spinning my app up for android:

E/flutter (15469): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
E/flutter (15469): #0      FirebaseCoreHostApi.initializeCore
package:firebase_core_platform_interface/…/pigeon/messages.pigeon.dart ...

in reference to the following line of code:

WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

I’m still able to run the iOS side of the app fine, and all dependencies are up to date for me, so I’m fairly confident the issue stems from me having the same packages installed within my app.build.gradle file.

Note: If I remove those dependencies in my gradle file, then the app breaks and throws errors immediately as it can’t find those libraries anywhere.

My question is: How can I implement these firebase packages and reference them from within a native Kotlin file found in the android section of my Flutter project?

I suspect I’m just not searching for the right thing, but everything online just says "Update your packages in your pubspec.yaml file, and mine are already updated so those responses aren’t helpful at all.

2

Answers


  1. The Flutter Firebase package depends on the native Firebase package, so it may look as if the same package is imported separately on android native and flutter side but build tools already manage it so it is allowed

    But the flutter and native side need to use the same library version to avoid any conflicts

    Please use the below command to find version inconsistencies and fix it

    ./gradlew dependencies

    More info on the above command can be found in the below-mentioned link

    https://medium.com/mindorks/avoiding-conflicts-in-android-gradle-dependencies-28e4200ca235

    Login or Signup to reply.
  2. It seems you have imported the same dependencies on both the flutter project and the generated kotlin project which leads to conflicts. And it is not necessary to import both Java and kotlin dependencies simultaneously if you are writing the Kotlin App in the app-level build gradle file.

    Make sure you have the required Gradle version with the Android Gradle plugin version and resolve most of the unnecessary conflicts. As per the Gradles, the issue is because of the app-level Gradle dependencies. Hence remove all manually and re-build the flutter project or add dependencies one by one manually on kotlin priority basis. It will do the job

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search