Unable to add firebase to android (flutter)

396

I had the same problem and could not migrate to Androidx manually. Note that it is necessary for projects to now be in Androidx if you want to use Firebase, or it may cause more problems later.

Step 1: In your project level gradle change:

    classpath 'com.google.gms:google-services:4.3.2'

to:

    classpath 'com.google.gms:google-services:3.2.0'

Usually after this step, your project will run fine, but it is import to migrate to AndroidX right now and not cause further errors while using Firebase.

Step 2: Once this is done, just make a new project with Androidx settings, you can do this by opening the flutter_console.bat and navigating to your desired folder then run:

    flutter create --androidx your_project_name

on your flutter console.

Step 3: Navigate to your new project > android > gradle.properties and check if it has these two lines:

    android.useAndroidX=true
    android.enableJetifier=true

This means that the project has migrated to Androidx.

Step 4: Finally, move all your code from the old project to the new flutter project along with all the dependencies and plugins you may have added in the old project. (Including dependency in Step 1)

Share:
396
Hasnain
Author by

Hasnain

Updated on December 15, 2022

Comments

  • Hasnain
    Hasnain over 1 year

    I have been trying to resolve this issue since two days and I have also tried the solutions already present at Stack Overflow regarding this problem but did not succeed. All the steps that I have followed are from Add Firebase to your Android app guide (The steps by Firebase Official)

    PROJECT-LEVEL Build.gradle:

    buildscript {
        ext.kotlin_version = '1.2.71'
        repositories {
            google()
            jcenter()
        }
    
        dependencies {
    
            classpath 'com.android.tools.build:gradle:3.2.1'
            classpath 'com.google.gms:google-services:4.3.2'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
    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-LEVEL 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"
    
    android {
        compileSdkVersion 28
    
        sourceSets {
            main.java.srcDirs += 'src/main/kotlin'
        }
    
        lintOptions {
            disable 'InvalidPackage'
        }
    
        defaultConfig {
            // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
            applicationId "com.example.loginapp"
            minSdkVersion 16
            targetSdkVersion 28
            versionCode flutterVersionCode.toInteger()
            versionName flutterVersionName
            testInstrumentationRunner "android.support.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"
        implementation 'com.google.firebase:firebase-analytics:17.2.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    apply plugin: 'com.google.gms.google-services'
    

    Pubspec.yaml File:

    version: 1.0.0+1
    
    environment:
      sdk: ">=2.1.0 <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_auth: ^0.14.0+9
    
    dev_dependencies:
      flutter_test:
        sdk: flutter
    

    Gradle-wrapper.properties:

    #Fri Jun 23 08:50:38 CEST 2017
    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
    

    ERROR:

    PS D:\flutterworks\bhund> flutter run
    Using hardware rendering with device Android SDK built for x86. If you get graphics artifacts, consider enabling software rendering with
    "--enable-software-rendering".
    Launching lib\main.dart on Android SDK built for x86 in debug mode...
    Initializing gradle...                                              5.1s
    Resolving dependencies...
    * Error running Gradle:
    ProcessException: Process "D:\flutterworks\bhund\android\gradlew.bat" exited abnormally:
    
      Command: D:\flutterworks\bhund\android\gradlew.bat app:properties
    
    
    Please review your Gradle project setup in the android/ folder.
    PS D:\flutterworks\bhund>
    
    • creativecreatorormaybenot
      creativecreatorormaybenot over 4 years
      Please post your errors and code as text instead of images because that is far more convenient to work with.
    • Gazihan Alankus
      Gazihan Alankus over 4 years
    • griffins
      griffins over 4 years
      you need to migrate your project to androidX
  • Hasnain
    Hasnain over 4 years
    Do I have to also change this line of code distributionUrl=https\://services.gradle.org/distributions/g‌​radle-5.4.1-all.zip in Gradle-wrapper.properties.
  • Hasnain
    Hasnain over 4 years
    This what I got after applying your suggestion : Gradle task assembleDebug failed with exit code 1
  • Jay Mungara
    Jay Mungara over 4 years
    Yes, you need to update the following line also. distributionUrl=https\://services.gradle.org/distributions/g‌​radle-5.4.1-all.zip
  • Hasnain
    Hasnain over 4 years
    I have to convert 5.4.1 into which version ?
  • Jay Mungara
    Jay Mungara over 4 years
    Just go to your android package > gradle package > wrapper package Inside gradle-wrapper.properties replase this line distributionUrl=https\://services.gradle.org/distributions/g‌​radle-5.4.1-all.zip with your current distributionUrl.
  • Hasnain
    Hasnain over 4 years
    Thanks for making it simple !