Search code examples
flutterfirebasegoogle-cloud-firestorefirebase-realtime-databaseflutter-dependencies

having difficulties setting up firebase with my flutter project


i have tried following a tutorial for setting up firebase for my flutter project but after setting everything up which worked perfectly now when i imported the firebase packages from pub.dev i started getting error when i try to run my code on an emulator. the packages that i imported are:

firebase_core: ^2.14.0
firebase_auth: ^4.6.3
cloud_firestore: ^4.8.1

furthermore the error that i am facing is:

C:\Users\ahmad\Desktop\flutter projects\chat_app\android\app\src\debug\AndroidManifest.xml Error:
    uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:cloud_firestore] C:\Users\ahmad\Desktop\flutter projects\chat_app\build\cloud_firestore\intermediates\merged_manifest\debug\AndroidManifest.xml as the library might be using APIs not available in 16
    Suggestion: use a compatible library with a minSdk of at most 16,
        or increase this project's minSdk version to at least 19,
        or use tools:overrideLibrary="io.flutter.plugins.firebase.firestore" to force usage (may lead to runtime failures)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:cloud_firestore] C:\Users\ahmad\Desktop\flutter projects\chat_app\build\cloud_firestore\intermediates\merged_manifest\debug\AndroidManifest.xml as the library might be using APIs not available in 16
    Suggestion: use a compatible library with a minSdk of at most 16,
        or increase this project's minSdk version to at least 19,
        or use tools:overrideLibrary="io.flutter.plugins.firebase.firestore" to force usage (may lead to runtime failures)

* 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 26s
 Flutter Fix
 The plugin cloud_firestore requires a higher Android SDK version.                             
 Fix this issue by adding the following to the file C:\Users\ahmad\Desktop\flutter             
 projects\chat_app\android\app\build.gradle:                                                   
 android {                                                                                     
   defaultConfig {                                                                             
     minSdkVersion 19                                                                          
   }                                                                                           
 }                                                                                             
                                                                                               
 Note that your app won't be available to users running Android SDKs below 19.                
 Alternatively, try to find a version of this plugin that supports these lower versions of the 
 Android SDK.                                                                                  
 For more information, see:                                                                    
 https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration                
Exception: Gradle task assembleDebug failed with exit code 1

from what i got from the above error i went to the respective build.gradle and changed the code from this :

    minSdkVersion flutter.minSdkVersion
    targetSdkVersion flutter.targetSdkVersion
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName

to this: minSdkVersion 19

i get the following error:

ERROR:D8: Cannot fit requested classes in a single dex file (# methods: 102117 > 65536)
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

* what went wrong
Execution failed for task ':app:mergeExtDexDebug'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.DexMergingTaskDelegate
   > There was a failure while executing work items
      > A failure occurred while executing com.android.build.gradle.internal.tasks.DexMergingWorkAction
         > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
           The number of method references in a .dex file cannot exceed 64K.
           Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

* 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 9s
[!] App requires Multidex support
    Multidex support is required for your android app to build since the number of methods has exceeded 64k. See https://docs.flutter.dev/deployment/android#enabling-multidex-support for more information. You may pass the --no-multidex flag to skip Flutter's multidex support to use a manual solution.
    Flutter tool can add multidex support. The following file will be added by flutter:
        android/app/src/main/java/io/flutter/app/FlutterMultiDexApplication.java
cannot prompt without a terminal ui
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)

Solution

  • Your setup is demanding for minsdk version 19 and your current minsdk version is 16.

    If you are using flutter version less than 2.8 then in your android/app/build.gradle file change minSdkVersion to 19 this

    defaultConfig {
            ...
            minSdkVersion 19 // 
            ...
        }
    

    If you are using version greater than 2.8 then in your android/local.properties file, add

    flutter.minSdkVersion=19
    

    After the above changes, do flutter clean to clean previous caches.
    Also this change-android-minsdkversion-in-flutter blog may help.