具有实时 Firebase 商店的应用程序中的 Android Studio 错误

Posted

技术标签:

【中文标题】具有实时 Firebase 商店的应用程序中的 Android Studio 错误【英文标题】:Android Studio Error in app with real-time firebase store 【发布时间】:2021-10-12 11:59:42 【问题描述】:

我正在开发一个项目并将应用程序与 firebase 连接,现在在连接实时数据库时出现此错误 “无法创建任务 ':app:processReleaseGoogleServices'。任务集上的 DefaultTaskContainer#NamedDomainObjectProvider.configure(Action) 无法在当前上下文中执行” 这些是 2 个 Gradle 文件:

plugins 
    id 'com.android.application'



android 
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig 
        applicationId "com.imrankhan.fitnessapp"
        minSdkVersion 22
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    

    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        
    
    compileOptions 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    


dependencies 
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation platform('com.google.firebase:firebase-bom:28.3.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
    implementation 'com.google.firebase:firebase-firestore:23.0.3'
    implementation 'com.google.firebase:firebase-database:20.0.1'
    testImplementation 'junit:junit:4.13.2'
    implementation 'com.google.android.material:material:1.4.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

apply plugin: 'com.google.gms.google-services'

**<File two code>**


// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript 
    repositories 
        google()
        jcenter()
    
    dependencies 
        classpath "com.android.tools.build:gradle:4.1.3"
        classpath 'com.google.gms:google-services:4.3.9'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    


allprojects 
    repositories 
        google()
        jcenter()
    


task clean(type: Delete) 
    delete rootProject.buildDir

【问题讨论】:

【参考方案1】:

在使用 firebase bom 时,您不需要包含单独的 firebase 依赖版本,否则会导致重叠并引发错误。

将依赖部分替换为:

 dependencies 
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation platform('com.google.firebase:firebase-bom:28.3.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    implementation 'com.google.firebase:firebase-auth'
    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
    implementation 'com.google.firebase:firebase-firestore'
    implementation 'com.google.firebase:firebase-database'
    testImplementation 'junit:junit:4.13.2'
    implementation 'com.google.android.material:material:1.4.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    

【讨论】:

dependencies classpath "com.android.tools.build:gradle:4.1.3" classpath 'com.google.gms:google-services:4.3.9' // 注意:不要将你的此处的应用程序依赖项;它们属于 // 在单独的模块 build.gradle 文件中 它通过更改上面的代码对我有用:dependencies classpath "com.android.tools.build:gradle:4.2.1" classpath 'com.google.gms :google-services:4.3.8' 【参考方案2】:

只需将 build gradle 'com.google.gms:google-services:4.3.9' 中的依赖项替换为 'com.google.gms:google-services:4.3.8' 即可。

【讨论】:

是的,这对我有用。非常感谢!

以上是关于具有实时 Firebase 商店的应用程序中的 Android Studio 错误的主要内容,如果未能解决你的问题,请参考以下文章