In Android Studio Arctic Fox Canary 8, the app level build.gradle does not generate `allprojects` section and causes error when manually added
In settings.gradle
you can add the repositories you want to add to the project
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter()
maven { url "https://maven.xyz.com" }
}
}
In settings.gradle
just comment out the whole block
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
Go to setting.gradle
and replace the line:
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
with:
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
That's a gradle setting which allows this to work.
In new release of Android, provide a new way to define your repositories. Before this release we used build.gradle(Project level) file to define repositories. Now you should write then into settings.gradle file. That's why It collides with Gradle plugin and generate this error message:
Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
We can solve it in two possible ways.
Solution 1:
Use previous way: Not to use dependencyResolutionManagement for repositories in settings.gradle file
Just keep settings.gradle file like below:
/*dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}*/
rootProject.name = "YourAppName"
include ':app'
Keep build.gradle(Project level) like below;
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
}
Change or add the line at top of the build.gradle(Module level)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services' // If you use
}
Solution 2:
Use new way: Use dependencyResolutionManagement for repositories in settings.gradle file
Just keep settings.gradle file like below:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "YourAppName"
include ':app'
Keep build.gradle(Project level) like below:
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
}
Keep build.gradle(Module level) file as previous:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'// If you use
}
Hopefully you overcome your problem.