Learn how to declare repositories and update your preferred settings to a project repository to avoid getting the error, “Build was configured to prefer settings repositories over project repositories but repository 'XXXX' was added by build file 'build.gradle'.”
Starting with Gradle 7, Android suggests the use of centralized repository declarations in settings.gradle over project- or module-level build.gradle declarations. If you created a new Android project by following the project creation wizard in the latest Android Studio, you will encounter the following error when you try to add a new maven repository in one of your build.gradle scripts:
1Build was configured to prefer settings repositories over2project repositories but repository 'XXXX' was3added by build file 'build.gradle'
When Gradle tries to resolve the host for one of your java dependencies, it walks through a list of repository urls and checks whether your dependency is hosted there. You can declare this list in your module’s (usually called app or library) build.gradle file the following way.
1buildscript {2repositories {3google()4mavenCentral()5jcenter() // Warning: this repository is going to shut down soon67maven { url 'https://maven.testfairy.com' }8maven { url 'https://example.com/maven' }9}10}1112plugins {13id 'com.android.application'14}1516...
Alternatively, you can declare the list in your project level build.gradle to enable it for all modules.
1allprojects {2repositories {3google()4jcenter()5maven { url 'https://maven.testfairy.com' }6maven { url 'https://example.com/maven' }7}8}9task clean(type: Delete) {10delete rootProject.buildDir11}
Unless otherwise specified in settings.gradle, mixing both methods is allowed. However, in cases where the same dependency is hosted by multiple repos in the list, Gradle may attempt to fetch the dependency from the wrong source.
There is a way to enforce a policy for repository declarations. RepositoriesMode api provides the following modes:
FAIL_ON_PROJECT_REPOS: If this mode is set, any repository declared directly in a project, either directly or via a plugin, will trigger a build error.
PREFER_PROJECT: If this mode is set, any repository declared on a project will cause the project to use the repositories declared by the project, ignoring those declared in settings.
PREFER_SETTINGS: If this mode is set, any repository declared directly in a project, either directly or via a plugin, will be ignored.
When a new project is created with Android Studio project creation wizard, FAIL_ON_PROJECT_REPOS will be selected as the default mode. This means all repository declarations must go into settings.gradle file.
1dependencyResolutionManagement {2repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)3repositories {4google()5mavenCentral()6jcenter() // Warning: this repository is going to shut down soon7maven { url 'https://maven.testfairy.com' }8maven { url 'https://example.com/maven' }9}10}11rootProject.name = "ExampleGradle7"12include ':app'
Build your app and check if one of the errors below occurs.
1Build was configured to prefer settings repositories over2project repositories but repository 'XXXX' was added3by build file 'build.gradle'45Could not find any matches for com.testfairy:testfairy-android-sdk:1.+6as no versions of com.testfairy:testfairy-android-sdk are available.
To fix, make sure you remove the repository declaration from all build.gradle files in the project, including the one in the root project.
Then declare your maven repository in settings.gradle.
Add the following configuration to your settings.gradle or replace the current one if it already exists.
1dependencyResolutionManagement {2repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) // or FAIL_ON_PROJECT_REPOS3repositories {4google()5mavenCentral()6jcenter() // Warning: this repository is going to shut down soon7maven { url 'https://maven.testfairy.com' }8maven { url 'https://example.com/maven' }9}10}11rootProject.name = "ExampleGradle7"12include ':app'
Sync your project and rebuild.
To learn more about managing your dependencies, make sure to check out the Gradle docs.
This article was originally published in October 2021 and has been updated in March 2023.