在Android Studio Arctic Fox Canary 8 中,app 级别的build.gradle 不会生成`allprojects` 部分并在手动添加时导致错误

dsf*_*dsf 24 android gradle android-studio

在 Android Studio Arctic Fox Canary 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
}

Run Code Online (Sandbox Code Playgroud)

在Android Studio 4.1.2中创建相同的新项目时,app级的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
}

Run Code Online (Sandbox Code Playgroud)

我正在使用的库之一需要 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'
Run Code Online (Sandbox Code Playgroud)

为什么allprojects在 Android Studio Canary 8 中被删除了,我如何将它添加回来以便我可以使用这些库?

Cod*_*meo 70

settings.gradle您可以添加库要添加到项目

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

  • 这应该是正确答案。 (2认同)

小智 33

settings.gradle刚刚注释掉整个街区

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {`enter code here`
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是行不通的。当我注释掉这个块时,我得到: 无法解析:androidx.appcompat:appcompat:1.2.0 无法解析:com.google.android.material:material:1.3.0 无法解析:androidx.compose.ui: ui-test-junit4:1.0.0-beta01 无法解析:androidx.test.espresso:espresso-core:3.3.0 无法解析:androidx.compose.ui:ui:1.0.0-beta01 无法解析:androidx .test.ext:junit:1.1.2 (+5 more) 请从构建脚本中删除 `jcenter()` Maven 存储库的使用,并将您的构建迁移到其他 Maven 存储库。 (4认同)

Sub*_*der 28

在新版本的 Android 中,提供了一种定义存储库的新方法。在此版本之前,我们使用build.gradle(项目级别)文件来定义存储库。现在您应该将其写入settings.gradle文件中。这就是为什么它与 Gradle 插件发生冲突并生成此错误消息:

Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
Run Code Online (Sandbox Code Playgroud)

我们可以通过两种可能的方式来解决它。

解决方案一:

使用以前的方式:不对settings.gradle文件中的存储库使用dependencyResolutionManagement

只需保留 settings.gradle 文件,如下所示:

/*dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}*/
rootProject.name = "YourAppName"
include ':app'
Run Code Online (Sandbox Code Playgroud)

保持 build.gradle(项目级别) 如下所示;

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
        classpath 'com.google.gms:google-services:4.3.10' // If you use
    }
}

allprojects {
    repositories {
        // Check that you have the following line (if not, add it):
        google()  // Google's Maven repository
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

更改或添加 build.gradle 顶部的行(模块级别)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.google.gms.google-services' // If you use
}
Run Code Online (Sandbox Code Playgroud)

解决方案2:

使用新方法:settings.gradle文件中的存储库使用dependencyResolutionManagement

只需保留 settings.gradle 文件,如下所示:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "YourAppName"
include ':app'
Run Code Online (Sandbox Code Playgroud)

保持 build.gradle(项目级别) 如下所示:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
        classpath 'com.google.gms:google-services:4.3.10' // If you use
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

保留 build.gradle(模块级别)文件如前:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.google.gms.google-services'// If you use
}
Run Code Online (Sandbox Code Playgroud)

希望你能克服你的问题。


Rah*_*hul 9

allProjects{}block 已从 gradle 的更新版本中删除。因此,在settings.gradle中您可以添加要下载的存储库并添加到项目中

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon

        maven {
            url 'https://your/url/to/download/repository/'
            credentials {
                username = "your username"
                password = "your password"
            }
        }
    }
}
rootProject.name = "My Application"
include ':app'
Run Code Online (Sandbox Code Playgroud)


小智 8

转到setting.gradle并替换该行:

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
Run Code Online (Sandbox Code Playgroud)

和:

repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
Run Code Online (Sandbox Code Playgroud)

这是一个允许它工作的gradle设置。