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

AndroidGradle

Android Problem Overview


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'

Android Solutions


Solution 1 - Android

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

Remove the dependencyResolutionManagement block from the setting.gradle file to have your project work the old way.

Solution 2 - Android

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 3 - Android

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 4 - Android

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 5 - Android

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 6 - Android

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 7 - Android

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 - Android

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 9 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndré NogueiraView Question on Stackoverflow
Solution 1 - AndroidYCuicuiView Answer on Stackoverflow
Solution 2 - AndroidwwilczynView Answer on Stackoverflow
Solution 3 - AndroidSeun MattView Answer on Stackoverflow
Solution 4 - AndroidlavaView Answer on Stackoverflow
Solution 5 - AndroidMoriView Answer on Stackoverflow
Solution 6 - AndroidShridhar ChoukseyView Answer on Stackoverflow
Solution 7 - AndroidMazhar IqbalView Answer on Stackoverflow
Solution 8 - AndroidRyadh HanafiView Answer on Stackoverflow
Solution 9 - Androidxv6amateurView Answer on Stackoverflow