How to Prefer settings.gradle Repositories Over build.gradle
Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from intent to execution in minutes.
|
x
Blog
How to Prefer settings.gradle Repositories Over build.gradle
Learn how to declare repositories and update your preferred settings to a project repository to avoid let the erroneousness, “ Build was configured to prefer settings depositary over project repositories but deposit & # x27; XXXX & # x27; was added by build file & # x27; build.gradle & # x27;. ”
Starting with Gradle 7, Android suggests the use ofcentralise secretary declaration in settings.gradleover project- or module-levelbuild.gradledeclaration. 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 yourbuild.gradle scripts:
1Build was configure to prefer settings repositories over2project repositories but secretary & # x27; XXXX & # x27; was3append by build file & # x27; build.gradle & # x27;
Declaring Repositories in build.gradle
When Gradle judge to conclude the horde for one of your java dependencies, it walks through a list of repository urls and check whether your dependency is hosted there. You can declare this list in your module ’ s (usually calledapp or library) build.gradlefile the following way.
1buildscript{2repositories{3google()4mavenCentral()5jcenter()// Warning: this repository is going to shut down soon67maven { url & # x27; https: //maven.testfairy.com & # x27;}8maven { url & # x27; https: //example.com/maven & # x27;}9}10}1112plugins {13id & # x27; com.android.application & # x27;14}1516...
Alternatively, you can declare the list in your project stagebuild.gradleto enable it for all faculty.
1allprojects{2repositories{3google()4jcenter()5maven { url & # x27; https: //maven.testfairy.com & # x27;}6maven { url & # x27; https: //example.com/maven & # x27;}7}8}9task clean(type:Delete){10deleterootProject.buildDir11}
Unless differently condition insettings.gradle, mixing both methods is allowed. However, in cases where the same colony is hosted by multiple repos in the list, Gradle may attempt to get the dependency from the wrong origin.
Declaring repositories in settings.gradle
There is a way to enforce a policy for repository declaration.RepositoriesModeapi provides the following modes:
FAIL_ON_PROJECT_REPOS: If this mode is set, any repository declare directly in a undertaking, either forthwith 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, disregard those declared in scene.
Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.
PREFER_SETTINGS: If this mode is set, any secretary announce directly in a labor, either directly or via a plugin, will be ignored.
When a new project is created with Android Studio undertaking creation wizard,FAIL_ON_PROJECT_REPOSwill be selected as the nonpayment fashion. This means all repository declaration 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 & # x27; https: //maven.testfairy.com & # x27;}8maven { url & # x27; https: //example.com/maven & # x27;}9}10}11rootProject.name=& quot; ExampleGradle7 & quot;12include & # x27;: app & # x27;
How to Fix the “ Build was Configured to Prefer Settings Repositories ” Error
Build your app and see if one of the erroneousness below occurs.
1Build was configure to prefer settings repositories over2project repositories but repository & # x27; XXXX & # x27; was added3by chassis file & # x27; build.gradle & # x27;45Could not find any lucifer for com.testfairy: testfairy-android-sdk:1.+6as no versions of com.testfairy: testfairy-android-sdk are uncommitted.
To fix, make sure you take the repository declaration from allbuild.gradlefile in the undertaking, include the one in the root project.
Then declare your maven repository insettings.gradle.
Migrating Existing Projects
Add the following configuration to yoursettings.gradleor replace the current one if it already subsist.
1dependencyResolutionManagement{2repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)// or FAIL_ON_PROJECT_REPOS3repositories{4google()5mavenCentral()6jcenter()// Warning: this repository is going to shut down presently7maven { url & # x27; https: //maven.testfairy.com & # x27;}8maven { url & # x27; https: //example.com/maven & # x27;}9}10}11rootProject.name=& quot; ExampleGradle7 & quot;12include & # x27;: app & # x27;
Sync your project and rebuild.
Resources for Managing Dependencies
To learn more about managing your dependencies, make sure to check out theGradle docs.
This article was originally issue in October 2021 and has been update in March 2023.
Sr. Software Engineer
Share this station
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free