0

I created multiple firebase project in one app to separate the development and production environment, so now I have 2 different generated google-services.json. I want to be able to run/build using a specific environment by specifying buildTypes as mentioned in this article on step 3. So my app folder below will follow this article.

NOTE: Why don't I use flavor? Because I already use command arguments (--dart-define).

This is snippet of my buildTypes:

buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        signingConfig signingConfigs.release
    }
}

I delete apply plugin: 'com.google.gms.google-services' as stated in this documentation.

This is my app folder:

Folder structure

I run my project using debug build (flutter run --debug) but I got this error:

Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized. Have you added the "google-services.json" file to the project?

I checked this documentation page on firebase flutter site, it mentioned:

await Firebase.initializeApp(
    name: 'SecondaryApp',
    options: const FirebaseOptions(
        appId: 'my_appId',
        apiKey: 'my_apiKey',
        messagingSenderId: 'my_messagingSenderId',
        projectId: 'my_projectId'
    )
);

is my choice only passing all required data via command arguments using this approach? I'm afraid it has limitations as I use other Firebase services. Any insights would be nice, thanks.

TijaniRF
  • 160
  • 2
  • 13

2 Answers2

0

Your google-services.json file is under the incorrect folder. It should be in android/app. Not android/app/src/debug. You can follow this official document.

Once your Android app has been registered, download the configuration file from the Firebase Console (the file is called google-services.json). Add this file into the android/app directory within your Flutter project.

Akif
  • 7,098
  • 7
  • 27
  • 53
  • I have 2 google-services.json files for different environments, I think I can't put it in same folder. But let's say I edit one google-services.json filename, how can I use specific google-services.json based on my buildType? – TijaniRF Nov 03 '20 at 16:54
  • 1
    Ok, you can follow this instruction: https://stackoverflow.com/a/34364376/10659482 – Akif Nov 03 '20 at 17:21
  • Wow thanks for the nice thread, I'll test several approaches that suit my use case. – TijaniRF Nov 03 '20 at 19:14
0

I found a simple way, this works for me:

  1. Add this code in your android/app build.gradle:

    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            debuggable false
            copy {
                from 'src/main'
                include 'google-services.json'
                into '.'
            }
        }
    
        debug {
            applicationIdSuffix ".debug" //Change this according to your requirement
            debuggable true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            copy {
                from 'src/debug'
                include 'google-services.json'
                into '.'
            }
        }
    }
    
  2. Next, add the google-services.json like this:

Add them inside android/app/src and then respective folders.

  1. Finally, run the app in debug or release mode to automatically update