You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio添加Firebase遇Gradle仓库配置冲突报错求助

解决Android Studio添加Firebase时的仓库配置报错

报错信息:

Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'

分步解决方案

1. 清理项目根目录的build.gradle文件

新版Android Studio(AGP 7.0+)要求仓库配置统一在settings.gradle中管理,因此需要删除根目录build.gradle中多余的仓库声明块。修改后的文件内容如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'com.google.gms.google-services' version '4.3.13' apply false
}

说明:

  • 删除了buildscript和allprojects两个包含仓库声明的代码块
  • 将Google服务插件的依赖移到plugins块中声明(apply false表示仅定义插件,不在根项目中应用)

2. 修正模块app/build.gradle文件

移除文件中错误放置的classpath依赖,修改后的内容如下:

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    namespace 'com.example.destination'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.destination"
        minSdk 17
        targetSdk 32
        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.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation platform('com.google.firebase:firebase-bom:31.1.1')
    implementation 'com.google.firebase:firebase-analytics'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

说明:

  • 删除了dependencies块中的classpath 'com.android.tools.build:gradle:7.3.1',该配置属于项目级插件声明,不应放在模块依赖中

3. 确认settings.gradle配置保持不变

你的settings.gradle已经正确配置了仓库管理模式和所需仓库,无需修改:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "Destination"
include ':app'

说明:

  • repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)会强制所有仓库声明必须在settings.gradle中,这也是导致你报错的核心原因,我们通过清理项目级build.gradle的仓库声明来适配这个规则

最后一步:同步项目

完成上述修改后,点击Android Studio右上角的Sync Now按钮,等待Gradle同步完成,报错即可解决。

内容的提问来源于stack exchange,提问作者user20637309

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 15:30:45