3

I tried to implement Google's recommended changes for "Add Firebase to your app" which states:

The Google services plugin for Gradle loads the google-services.json file you just downloaded. Modify your build.gradle files to use the plugin.
Project-level build.gradle (<project>/build.gradle):

buildscript {
  dependencies {
    // Add this line
    classpath 'com.google.gms:google-services:4.0.1'
  }
}

App-level build.gradle (<project>/<app-module>/build.gradle):

dependencies {
  // Add this line
  implementation 'com.google.firebase:firebase-core:16.0.1'
}
...
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'

But after making these changes (albeit updated for current versions, ie: services is now 4.2.0 and core is now 16.0.9) I get the following error when trying to run the application:

* Error running Gradle:
ProcessException: Process "X:\Projects\Apps\Flutter\ultimate_mtg\android\gradlew.bat" exited abnormally:

FAILURE: Build failed with an exception.

* Where:
Script 'X:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 29

* What went wrong:
A problem occurred evaluating script.
> Could not find method android() for arguments [flutter_btx813u5j2ly3w9khjl576b0k$_run_closure1@6f90a486] on project ':app' of type org.gradle.api.Project.

It appears at this stage to be inside the flutter SDK. Here is my flutter.gradle file (extract):

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
    }
}

android {
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}

Line 29 is this line:

android {

Anyone have any ideas with this one? I have read many similar threads on this and none of the suggestions there are working for me.

For added information, my \build.gradle file:

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

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
}

And my \app\build.gradle file:

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 from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "sjr.ultimatemtg"
        minSdkVersion 16
        targetSdkVersion 27
        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 'com.google.firebase:firebase-core:16.0.9'
    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'
    implementation 'com.google.firebase:firebase-ads:17.2.0'
}


apply plugin: 'com.google.gms.google-services'
Bisclavret
  • 1,327
  • 9
  • 37
  • 65
  • Possible duplicate of [Could not find method android() for arguments](https://stackoverflow.com/questions/37250493/could-not-find-method-android-for-arguments) – Adelina May 14 '19 at 07:42
  • Thank you for the reply, @Nuts, but this is not relevant to me. My android {} block resides in \app\build.gradle as people suggest in all these threads, and not in \build.gradle. This thread didn't help me unfortunately. – Bisclavret May 14 '19 at 07:50

1 Answers1

0

Try moving 'com.android.application' to the top of the order in the plugin configs. This seems needed to go first similarly to what have been mentioned on this answer.

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

Though nowadays, you can user flutterfire_cli to configure your app to use Firebase.

Omatt
  • 8,564
  • 2
  • 42
  • 144