94

When creating a new project in Android Studio Arctic Fox Canary 8, this is the app level 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
}

When creating the same new project in the Android Studio 4.1.2, the app-level build.gradle file is this:

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

One of the libraries I am using requires the allprojects

I manually tried to add the allprojects section in Canary 8, received this error:

 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'

Why was the allprojects in Android Studio Canary 8 was removed and how can I add it back so that I can use the libraries?

dsf
  • 1,203
  • 1
  • 6
  • 11

7 Answers7

235

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" }
    }
}
S Haque
  • 6,881
  • 5
  • 29
  • 35
51

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
    }
}
Boken
  • 4,825
  • 10
  • 32
  • 42
shasia858
  • 580
  • 1
  • 4
  • 2
  • 4
    This doesn't work. When I comment out this block, I get: Failed to resolve: androidx.appcompat:appcompat:1.2.0 Failed to resolve: com.google.android.material:material:1.3.0 Failed to resolve: androidx.compose.ui:ui-test-junit4:1.0.0-beta01 Failed to resolve: androidx.test.espresso:espresso-core:3.3.0 Failed to resolve: androidx.compose.ui:ui:1.0.0-beta01 Failed to resolve: androidx.test.ext:junit:1.1.2 (+5 more) Please remove usages of `jcenter()` Maven repository from your build scripts and migrate your build to other Maven repositories. – Lukasz C. Mar 27 '21 at 17:52
43

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.

Vadik Sirekanyan
  • 3,332
  • 1
  • 22
  • 29
Abdullah Fahad
  • 431
  • 4
  • 2
  • 1
    After applying this setting, project sync successful. I am using Android Studio Arctic Fox | 2020.3.1 Patch 1 (For ARM) OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 11.5.2 on a M1 macbook pro. – Mert Gülsoy Aug 28 '21 at 13:45
  • can you please tell us what is the difference between the two? – Mohamed Salah Jul 13 '22 at 17:20
28

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.

Subarata Talukder
  • 5,407
  • 2
  • 34
  • 50
  • I just wanted to know is it a right way of doing it? I wanted to add this dependency 'com.github.PhilJay:MPAndroidChart:v3.1.0' which requires maven { url 'https://jitpack.io' } repositories. Its wokring....I have directly added it in setting.gradle -> dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() jcenter() maven { url 'https://jitpack.io' } } } – Ashish Bharam Oct 26 '21 at 14:24
9

allProjects{} block is removed from the updated version of gradle. So, in settings.gradle you can add the repositories you want to download and add to the project

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'
statosdotcom
  • 3,109
  • 2
  • 17
  • 40
Rahul
  • 3,293
  • 2
  • 31
  • 43
4

just keep this line in settings.gradle

  1. include ':app' (Put in First line)
  2. comment rest of code if any.
Nidhi
  • 51
  • 3
3

Google has made some changes in the project label Gradle.

you can fix this by removing all project label configuration to setting.gradle file.

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

Remove this code in the Gradle file and all your plugins code in setting.gradle file. Like below.

    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'
Rajneesh Shukla
  • 1,048
  • 13
  • 21