首页 > 解决方案 > 在Android Studio Arctic Fox Canary 8中,app级build.gradle不生成`allprojects`部分,手动添加时会报错

问题描述

在 Android Studio 北极狐金丝雀 8 中创建新项目时,这是应用级别的 build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.0-alpha08"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

在 Android Studio 4.1.2 中创建相同的新项目时,应用级 build.gradle 文件是这样的:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.3.72"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // 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
}

我正在使用的库之一需要allprojects

我手动尝试allprojects在 Canary 8 中添加该部分,收到此错误:

 problem occurred evaluating root project 'My Application'.
> Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'

为什么allprojectsAndroid Studio Canary 8 中的 被删除了,我怎样才能将它添加回来以便我可以使用这些库?

标签: androidandroid-studiogradle

解决方案


您可以在settings.gradle其中添加要添加到项目的存储库

dependencyResolutionManagement {
   repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
   repositories {
      google()
      mavenCentral()
      jcenter()
      maven { url "https://maven.xyz.com" }
    }
}

推荐阅读