簡體   English   中英

在 Android Studio Arctic Fox Canary 8 中,應用程序級別 build.gradle 不生成 `allprojects` 部分並在手動添加時導致錯誤

[英]In Android Studio Arctic Fox Canary 8, the app level build.gradle does not generate `allprojects` section and causes error when manually added

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

在 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

我手動嘗試在 Canary 8 中添加allprojects部分,收到此錯誤:

 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'

為什么allprojects Studio Canary 8 中的所有項目都被刪除了,我該如何將其添加回來以便我可以使用這些庫?

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

Go 到設置setting.gradle並更換線路:

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

和:

repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

這是一個 gradle 設置,它允許它工作。

在 Android 的新版本中,提供了一種定義存儲庫的新方法。 在此版本之前,我們使用build.gradle(Project level)文件來定義存儲庫。 現在您應該將 then 寫入settings.gradle文件。 這就是它與 Gradle 插件沖突並生成此錯誤消息的原因:

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

我們可以通過兩種可能的方式來解決它。

解決方案1:

使用以前的方法:不要在settings.gradle文件中對存儲庫使用dependencyResolutionManagement

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

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

保持 build.gradle(Project level) 如下所示;

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
}

更改或添加 build.gradle 頂部的行(模塊級別)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.google.gms.google-services' // If you use
}

解決方案2:

使用新方法:settings.gradle文件中對存儲庫使用dependencyResolutionManagement

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

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

保持 build.gradle(Project level) 如下所示:

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
}

保持 build.gradle(Module level) 文件如前:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.google.gms.google-services'// If you use
}

希望你能克服你的問題。

settings.gradle ,您可以添加要添加到項目中的存儲庫

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

只需將此行保留在 settings.gradle

  1. 包括 ':app' (放在第一行)
  2. 注釋代碼的 rest(如果有)。

Google 在 label Gradle 項目中做了一些改動。

您可以通過將所有項目 label 配置刪除到 setting.gradle 文件來解決此問題。

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

刪除 Gradle 文件中的此代碼以及 setting.gradle 文件中的所有插件代碼。 如下所示。

    pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
    plugins {
        id 'com.android.application' version '7.1.0-alpha03'
        id 'com.android.library' version '7.1.0-alpha03'
        id 'org.jetbrains.kotlin.android' version '1.5.10'
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "MVVMDEMO"
include ':app'

allProjects{}塊已從 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/reposoitory/'
            credentials {
                username = "your username"
                password = "your password"
            }
        }
    }
}
rootProject.name = "My Application"
include ':app'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM