4

I have tried to solve this problem but it keeps giving me the same error.

I use the following firebase services:

  • Firebase Auth
  • Firebase Database
  • Firebase Storage
  • Firebase Messaging

I hope to solve this problem, I appreciate any help, thanks.

Screenshot:

enter image description here

Here I show you the console run

Launching lib\main.dart on Android SDK built for x86 in debug mode...
[{"event":"app.progress","params":{"appId":"870bf9fe-6ecb-481e-9990-a8f0123d691f","id":"0","progressId":null,"message":"Initializing gradle..."}}]Initializing gradle...

Resolving dependencies...
Running 'gradlew assembleDebug'...
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
Built build\app\outputs\apk\debug\app-debug.apk.
Installing build\app\outputs\apk\app.apk...
I/flutter (14362): Load es
Syncing files to device Android SDK built for x86...
D/        (14362): HostConnection::get() New Host Connection established 0x9ffa3dc0, tid 14398
D/EGL_emulation(14362): eglMakeCurrent: 0x9fd86c00: ver 3 0 (tinfo 0x9fff1110)
W/zygote  (14362): Unsupported class loader
W/zygote  (14362): Skipping duplicate class check due to unsupported classloader
I/DynamiteModule(14362): Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
I/DynamiteModule(14362): Selected remote version of com.google.android.gms.firebase_database, version >= 6
V/DynamiteModule(14362): Dynamite loader version >= 2, using loadModule2NoCrashUtils
W/zygote  (14362): Unsupported class loader
W/zygote  (14362): Skipping duplicate class check due to unsupported classloader
E/AndroidRuntime(14362): FATAL EXCEPTION: TokenRefresher
E/AndroidRuntime(14362): Process: com.***********, PID: 14362
E/AndroidRuntime(14362): java.lang.NoSuchFieldError: No static field PREFER_HIGHEST_OR_REMOTE_VERSION_NO_FORCE_STAGING of type Lcom/google/android/gms/dynamite/DynamiteModule$VersionPolicy; in class Lcom/google/android/gms/dynamite/DynamiteModule; or its superclasses (declaration of 'com.google.android.gms.dynamite.DynamiteModule' appears in /data/app/com.*********-Bx6_KiELmKKW1pc-YChs7w==/base.apk)
E/AndroidRuntime(14362):    at com.google.android.gms.flags.FlagValueProvider.initialize(Unknown Source:7)
E/AndroidRuntime(14362):    at com.google.android.gms.flags.FlagRegistry.initialize(Unknown Source:4)
E/AndroidRuntime(14362):    at com.google.firebase.auth.internal.zzx.initialize(Unknown Source:3)
E/AndroidRuntime(14362):    at com.google.firebase.auth.internal.zzt.run(Unknown Source:14)
E/AndroidRuntime(14362):    at android.os.Handler.handleCallback(Handler.java:790)
E/AndroidRuntime(14362):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(14362):    at android.os.Looper.loop(Looper.java:164)
E/AndroidRuntime(14362):    at android.os.HandlerThread.run(HandlerThread.java:65)
W/DynamiteModule(14362): Local module descriptor class for com.google.firebase.auth not found.
W/BiChannelGoogleApi(14362): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@d55aa74
W/DynamiteModule(14362): Local module descriptor class for com.google.firebase.auth not found.
I/FirebaseAuth(14362): [FirebaseAuth:] Loading module via FirebaseOptions.
I/FirebaseAuth(14362): [FirebaseAuth:] Preparing to create service connection to gms implementation
D/EGL_emulation(14362): eglMakeCurrent: 0x9fd86c60: ver 3 0 (tinfo 0x89b36690)
D/FirebaseAuth(14362): Notifying id token listeners about user ( oi1ngjUXDpWPIBaen7po8T5vE8R2 ).
D/FirebaseApp(14362): Notifying auth state listeners.
D/FirebaseApp(14362): Notified 1 auth state listeners.
D/NetworkSecurityConfig(14362): No Network Security Config specified, using platform default
I/zygote  (14362): Do partial code cache collection, code=61KB, data=44KB
I/zygote  (14362): After code cache collection, code=61KB, data=44KB
I/zygote  (14362): Increasing code cache capacity to 256KB
D/FlutterNativeView(14362): FlutterView.send called on a detached view, channel=plugins.flutter.io/firebase_database
I/FirebaseAuth(14362): [FirebaseAuth:] Loading module via FirebaseOptions.
I/FirebaseAuth(14362): [FirebaseAuth:] Preparing to create service connection to gms implementation
Lost connection to device.

My Android project and the build.gradle.

Android -> build.grandle

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
          classpath 'com.android.tools.build:gradle:3.2.0'
          classpath 'com.google.gms:google-services:4.1.0'
        //classpath 'com.android.tools.build:gradle:3.1.2'
        //classpath 'com.google.gms:google-services:3.2.1' 
    }
}

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
}

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "26.1.0"
            }
        }
    }
}

Android -> App -> src -> build.grandle

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 from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
    compileSdkVersion 27

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "com.*************"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    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-core:16.0.4'
}
apply plugin: 'com.google.gms.google-services'



I updated the versions on Android -> build.gradle. But now it comes out with another error.

UPDATED:

Launching lib\main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
* Error running Gradle:
Exit code 1 from: C:\Users\*******\AndroidStudioProjects\**********\*********\android\gradlew.bat app:properties:
Starting a Gradle Daemon (subsequent builds will be faster)


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'android'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:3.2.0.
     Required by:
         project :
      > Could not resolve com.android.tools.build:gradle:3.2.0.
         > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.2.0/gradle-3.2.0.pom'.
            > Could not HEAD 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.2.0/gradle-3.2.0.pom'.
               > dl.google.com
   > Could not resolve com.google.gms:google-services:4.1.0.
     Required by:
         project :
      > Could not resolve com.google.gms:google-services:4.1.0.
         > Could not get resource 'https://dl.google.com/dl/android/maven2/com/google/gms/google-services/4.1.0/google-services-4.1.0.pom'.
            > Could not HEAD 'https://dl.google.com/dl/android/maven2/com/google/gms/google-services/4.1.0/google-services-4.1.0.pom'.
               > dl.google.com
      > Could not resolve com.google.gms:google-services:4.1.0.
         > Could not get resource 'https://jcenter.bintray.com/com/google/gms/google-services/4.1.0/google-services-4.1.0.pom'.
            > Could not HEAD 'https://jcenter.bintray.com/com/google/gms/google-services/4.1.0/google-services-4.1.0.pom'.
               > Connect to jcenter.bintray.com:443 [jcenter.bintray.com/159.122.18.156] failed: Connection timed out: connect

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 54s

Finished with error: Please review your Gradle project setup in the android/ folder.
DomingoMG
  • 1,827
  • 2
  • 23
  • 44
  • Are you using the latest versions of each? – TheWanderer Oct 09 '18 at 23:18
  • You mean the java versions? – DomingoMG Oct 09 '18 at 23:19
  • The library versions. They need to be up to date. – TheWanderer Oct 09 '18 at 23:19
  • The Dart and Flutter versions I have it updated. But from java I do not know if I have it updated to the latest version, where could I check it? – DomingoMG Oct 09 '18 at 23:23
  • Open your Android project and check the build.gradle. – TheWanderer Oct 09 '18 at 23:24
  • I have updated the question where I teach the build.gradle – DomingoMG Oct 09 '18 at 23:32
  • There's the issue. Your Gradle version should be 3.2.0 and your google-services version should be 4.2.0. Flutter generates very outdated Gradle configs for some reason. – TheWanderer Oct 09 '18 at 23:37
  • I also have this issue. I can post my build.gradle files down below if helpful @TheWanderer ? I also found this: https://stackoverflow.com/questions/52685651/fluttererror-facing-in-console-app-is-automatically-closed Edit: Could you also post your app level build.gradle op? – oreid Oct 09 '18 at 23:43
  • Update the versions, but now in the boot console it tells me that the version is not correct. – DomingoMG Oct 09 '18 at 23:47
  • @Beardo I've updated the question and added the Android build.grandle -> App -> Src – DomingoMG Oct 09 '18 at 23:57
  • 1
    @DomingoMG I am in no way a Gradle expert (just trying to fix this problem too). I went to `android/gradle/wrapper/gradle-wrapper-properties` and changed line 6 from `distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip` to `distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip` and it seems to be working. Can you try and advise? – oreid Oct 10 '18 at 00:30
  • No problem. I think the ideal is to help each other even if we do not know much. I downloaded the gradle-4.6-all ... and added it to `C: \ Users \ UserNameHere \ .gradle \ wrapper \ dists \ gradle-4.6-all \ Hdgasfdjag6 \ gradle-4.6-all.zip` It seemed that everything was correct until I missed other mistakes. I updated the question and added the run console. – DomingoMG Oct 10 '18 at 01:15

3 Answers3

12

I believe that this is due to the fact that half of Google's FlutterFire plugins are using old versions of their Android counterparts. As such, I've specified which versions to use in my app level build.gradle. This probably isn't a permanent solution, but seems to have stopped the crashes in the mean time.

In the app level build.gradle, add the following to dependencies:

implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-analytics:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.google.firebase:firebase-firestore:17.1.1'
implementation 'com.google.firebase:firebase-functions:16.1.1'
implementation 'com.google.firebase:firebase-messaging:17.3.3'
implementation 'com.google.firebase:firebase-storage:16.0.3'

Let me know if this works.

Edit: Only add the implementation lines for the Firebase plugins that you are using. You can find the latest version of each here.

oreid
  • 1,336
  • 2
  • 13
  • 25
  • It worked, but I guess you have to be aware of the new versions of Firebase, right? – DomingoMG Oct 10 '18 at 10:02
  • 1
    Not necessarily, as the TokenRefresher issue was probably addressed in one of the updated `implementation` lines. Ideally, Google would keep their Flutter plugins up to date. – oreid Oct 10 '18 at 10:28
  • 1
    Great! Note to others. My flutter app was crashing on startup (using Android Studio) prior to attaching the debugger until I surrounded by initial Firebase call in a try-catch. Still could not catch the error, but at least I did see logging of the issue that lead me here: java.lang.NoSuchFieldError: No field PREFER_HIGHEST_OR_REMOTE_VERSION_NO_FORCE_STAGING – Cirec Beback Dec 28 '18 at 17:13
2

The Beardo's Answer Solves the Issue,

Please Look at my gradle files.

App Level: -

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 from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 27

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.adilstore.rakeshproject"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    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 {
    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.facebook.android:facebook-android-sdk:[4,5)'
    implementation 'com.google.firebase:firebase-core:16.0.4'
    implementation 'com.google.firebase:firebase-analytics:16.0.4'
    implementation 'com.google.firebase:firebase-auth:16.0.4'
    implementation 'com.google.firebase:firebase-firestore:17.1.1'
    implementation 'com.google.firebase:firebase-functions:16.1.1'
    implementation 'com.google.firebase:firebase-messaging:17.3.3'
    implementation 'com.google.firebase:firebase-storage:16.0.3'
}
apply plugin: 'com.google.gms.google-services'
//com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true

And Project Level:-

buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral() //add this 
    }

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

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
}
subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "26.1.0"
            }
        }
    }
}
Rakesh Lanjewar
  • 625
  • 3
  • 8
  • 23
0

In my case upgrading everything to the latest versions helped. Here are the steps:

  • Open the Android project as a separate project
  • Upgrade Gradle
  • Upgrade all the Firebase-related dependencies
  • Migrate to Androidx if needed
  • Go back to the parent Flutter project
  • Upgrade all the Firebase-related dependencies there (i.e., search for the latest versions at https://pub.dev/packages, update them and run flutter --no-color packages upgrade or just click Packages upgrade in the main pubspec.yaml
  • Upgrade the Flutter itself by running flutter --no-color upgrade or clicking Flutter upgrade
Alex Kuzmin
  • 725
  • 5
  • 17