androidgradle

Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'build.gradle'


I want to add jitpack.io as a repository in my gradle file. This is my gradle root file:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"

        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

Since I DON'T have a "allrepositories" to put my dependency there (only works there), I've created and added this code after buildscript code:

allprojects {
    repositories {
        maven {url 'https://www.jitpack.io'}
    }
}

But this is the error I get

Caused by: org.gradle.api.InvalidUserCodeException: Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'build.gradle'

Solution

  • General Info

    Gradle 6.8 introduced central declaration of repositories, new way to define repositories. Latest documentation (7.4.2) can be found here.

    You have 2 options from here;

    Remove central declaration to work with your current config

    For this you need to remove the dependencyResolutionManagement block from the setting.gradle file (=the new way). And then the repositories config in your build.gradle will work again (=the old way).

    build.gradle (no change from your example)

    allprojects {
        repositories {
            maven {url 'https://www.jitpack.io'}
        }
    }
    

    Update to work the new way

    The way forward is to edit build.gradle removing that repositories declaration. And then edit settings.gradle to include the maven config.

    settings.gradle

    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
            maven { url 'https://jitpack.io' }
        }
    }