Search code examples
androidandroid-studiogradle

How to set global repositories for gradle that I can use them in each gradle project in 2024?


I am an Android learner and in order to adapt to the requirements of my network environment, I need to use the local repository settings in Gradle 8.9. As instructed in most tutorials, create an init.gradle file in the init.d directory with the following content:

allprojects{
    repositories {
        maven { url "XXXX" }
    }
}

However, the result of gradle. bat - debug displays the following error message: Build was configured to prefer settings repositories over project repositories.

How can I set the global repository instead of manually modifying the settings.gradle file for each project? Or how can I change the template for settings.gradle file in android studio? (PS: I can't find file named like build.gradle.ftl)


Solution

  • You must have the repository mode set to PREFER_PROJECT. Once you fix that, you can declare repositories in your settings.gradle

    dependencyResolutionManagement {
     repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
     repositories {
       mavenLocal()
       mavenCentral()
       maven { url 'https://example.com/maven' }
     }
    }
    

    More info can be found at https://saucelabs.com/resources/blog/how-to-prefer-settings-gradle-repositories-over-build-gradle