firebase / flutterfire

πŸ”₯ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.68k stars 3.97k forks source link

[database] apply plugin: 'com.google.gms.google-services' not working #1299

Closed CoreyCole closed 5 years ago

CoreyCole commented 5 years ago

Describe the bug I'm unable to connect to my firebase realtime database and I'm unable to make a connection to the crashlytics server. I've followed the instructions for database here and crashlytics here.

Someone has a similar issue from 2 months ago on stack overflow with no posted solution. Another similar issue was solved by flutter clean, but unfortunately this didn't solve for me. Looks like there is also an open issue in this repo that may be related here.

It looks like google-services plugin cannot find my google-services.json file even though I've re-downloaded and placed it in the android/app directory multiple times now between flutter clean builds. I've simplified the issue just to the realtime database connection and removed all the crashlyics code/config for the time being and I'm still getting the same primary error message

java.lang.IllegalStateException: FirebaseApp with name DEFAULT doesn't exist. Available app names: [DEFAULT]

To Reproduce

main.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_crashlytics/flutter_crashlytics.dart';
import 'src/app.dart';

void main() async {
    bool isInDebugMode = true;

    FlutterError.onError = (FlutterErrorDetails details) {
        if (isInDebugMode) {
            // In development mode simply print to console.
            FlutterError.dumpErrorToConsole(details);
        } else {
            // In production mode report to the application zone to report to
            // Crashlytics.
            Zone.current.handleUncaughtError(details.exception, details.stack);
        }
    };

    await FlutterCrashlytics().initialize();

    runZoned<Future<Null>>(() async {
        runApp(App());
    }, onError: (error, stackTrace) async {
        print('FLUTTER CRASH');
        // Whenever an error occurs, call the `reportCrash` function. This will send
        // Dart errors to our dev console or Crashlytics depending on the environment.
        await FlutterCrashlytics().reportCrash(error, stackTrace, forceCrash: false);
    });
}

app.dart

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'bloc/provider.dart';

class App extends StatelessWidget {
    static final FirebaseApp app = FirebaseApp(name: 'DEFAULT');

    Widget build(context) {
        return Provider(
            firebaseApp: app,
            child: MaterialApp(
                title: 'Pitch Conferencing',
                onGenerateRoute: _routes,
                debugShowCheckedModeBanner: false,
            )
        );
    }
// ...

firebase_service.dart

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';

class FirebaseService {
    DatabaseReference db;

    FirebaseService({ FirebaseApp firebaseApp }) {
        db = FirebaseDatabase(
            app: firebaseApp,
            databaseURL: 'https://pitchconferencing.firebaseio.com'
        ).reference();
        print('checking db connection!');
        db.child('rooms').once().then((result) => print('result = $result'));
    }
}

root build.gradle

buildscript {
    ext.kotlin_version = '1.3.10'
    repositories {
        google()
        jcenter()
        // maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // for firebase
        classpath 'com.google.gms:google-services:4.3.2'
        //classpath 'io.fabric.tools:gradle:1.31.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app build.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 28

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "com.kernellabsinc.pitch_conferencing"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    // android x support library
    // Turns out the Support Library is now deprecated and projects are encouraged to use AndroidX instead.
    // https://caiolandau.com/using-androidx-flutter-project
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation "androidx.core:core-ktx:1.1.0"

    // https://developer.android.com/studio/build/multidex
    // solves problem https://github.com/flutter/flutter/issues/27682
    // def multidex_version = "2.0.1"
    // implementation 'androidx.multidex:multidex:$multidex_version'

    // twilio voice
    implementation 'com.twilio:voice-android:4.5.0'
}

// for firebase
apply plugin: 'com.google.gms.google-services'
//apply plugin: 'io.fabric'
//crashlytics {
//    enableNdk true
//    androidNdkOut "../../debugSymbols"
//    androidNdkLibsOut "../../build/app/intermediates/transforms/stripDebugSymbol/release/0/lib"
//}

dependencies

environment:
  sdk: ">=2.2.2 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2

  # firebase
  firebase_core: 0.4.0+3
  firebase_database: 3.0.7
  # flutter_crashlytics: 1.0.0

  # other
  http: 0.12.0+2
  rxdart: 0.22.4
  uuid: 2.0.2
  auto_size_text: 2.1.0
  connectivity: 0.4.4+1

flutter doctor -v

[βœ“] Flutter (Channel stable, v1.9.1+hotfix.5, on Mac OS X 10.14.6 18G95, locale en-US)
    β€’ Flutter version 1.9.1+hotfix.5 at /Users/corey/flutter
    β€’ Framework revision 1aedbb1835 (5 days ago), 2019-10-17 08:37:27 -0700
    β€’ Engine revision b863200c37
    β€’ Dart version 2.5.0

[βœ“] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    β€’ Android SDK at /Users/corey/Library/Android/sdk
    β€’ Android NDK location not configured (optional; useful for native profiling support)
    β€’ Platform android-28, build-tools 28.0.3
    β€’ ANDROID_HOME = /Users/corey/Library/Android/sdk
    β€’ ANDROID_SDK_ROOT = /Users/corey/Library/Android/sdk
    β€’ Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    β€’ Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    β€’ All Android licenses accepted.

[βœ“] Xcode - develop for iOS and macOS (Xcode 11.1)
    β€’ Xcode at /Applications/Xcode.app/Contents/Developer
    β€’ Xcode 11.1, Build version 11A1027
    β€’ CocoaPods version 1.7.5

[βœ“] Android Studio (version 3.5)
    β€’ Android Studio at /Applications/Android Studio.app/Contents
    β€’ Flutter plugin version 40.2.2
    β€’ Dart plugin version 191.8593
    β€’ Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[βœ“] VS Code (version 1.39.2)
    β€’ VS Code at /Applications/Visual Studio Code.app/Contents
    β€’ Flutter extension version 3.5.1

[βœ“] Connected device (2 available)
    β€’ SM J700H  β€’ 52033d7fe8448323   β€’ android-arm   β€’ Android 6.0.1 (API 23)
    β€’ SM G950U1 β€’ 988939444c58415246 β€’ android-arm64 β€’ Android 9 (API 28)

β€’ No issues found!

What I've tried

CoreyCole commented 5 years ago

Since I'm no longer interested in crashlytics until I can get the database working, I'll change the tag in the title.

CoreyCole commented 5 years ago

I've made a minimum reproduction case here: https://github.com/CoreyCole/test_firebase_app

CoreyCole commented 5 years ago

And asked on stack overflow here: https://stackoverflow.com/questions/58528230/flutter-firebase-realtime-database-firebaseapp-with-name-default-doesnt-exist

CoreyCole commented 5 years ago

I solved the issue. It boiled down to a documentation/error message problem:

// bad
final FirebaseApp app = FirebaseApp(name: 'DEFAULT');

// good
final FirebaseApp app = FirebaseApp(name: '[DEFAULT]');

Unfortunately, in the example app for database, we connect to a db named db2. So I couldn't see what the correct default syntax looked like.

I've made a documentation PR to show the correct default syntax: https://github.com/FirebaseExtended/flutterfire/pull/1306