'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 1:[1]

You can add jitpack.io as a repository inside dependencyResolutionManagement in settings.gradle

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

Solution 2:[2]

You need to update the settings in settings.gradle and change repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) to repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

and finally, add maven { url 'https://jitpack.io' } to the repositories block.

The complete settings.gradle file will look like this:

import org.gradle.api.initialization.resolve.RepositoriesMode

dependencyResolutionManagement { 
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories { 
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
        maven { url 'https://jitpack.io' }
    }
}
rootProject.name = "appname"
include ':app'

Solution 3:[3]

Replace this line:

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

use this:

repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

Before

enter image description here

After

enter image description here

  repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

Solution 4:[4]

In gradle version '7.1.0' just need to add maven { url 'https://jitpack.io' } in setting.gradle

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

Solution 5:[5]

Solution:

You can add this url in settings.gradle(Project Settings) file, which you will find in Gradle Scripts,

Add your url inside dependencyResolutionManagement like this

dependencyResolutionManagement{
    maven {
        url 'https://jitpack.io'
    }    
}

#See below pic for complete reference, enter image description here

Now sync it, it will work,

Thank you! :)

Solution 6:[6]

Go To Settings.gradle and put it inside the repositories

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

Solution 7:[7]

As the Android studio is upadated so you have to control your dependency form your setting.app

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
    google()
    mavenCentral()
    jcenter() // Warning: this repository is going to shut down soon
    maven { url 'https://jitpack.io' }
  }

} rootProject.name = "EmfDetector" include ':app'

Kindly place this line the respiratory

      maven { url 'https://jitpack.io' } //as i have done above 
     `  
   
 

Solution 8:[8]

some old advices let you add maven { url "https://jitpack.io" }into root build.gradle, but as long as you are using latest version of as, you can simply put it into settings.gradle instead of build.gradle

Solution 9:[9]

In my case, I just delete the dependencyResolutionManagement{...} statement that in the settings.gradle the new project is default added in settings.gradle

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wwilczyn
Solution 2 Seun Matt
Solution 3
Solution 4 Mori
Solution 5 Shridhar Chouksey
Solution 6 Ryadh Hanafi
Solution 7
Solution 8 xv6amateur
Solution 9 normidar