289

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'

15 Answers 15

388

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' }
    }
}
10
  • thanks, but I also had to remove "testImplementation 'junit:junit:'" from build.gradle
    – Cor
    Jan 14, 2022 at 10:20
  • 2
    great..now i have some new errors - Could not find androidx.databinding:viewbinding:7.0.3. and also in some other code segments like paypal related code and other also Jan 26, 2022 at 9:10
  • 4
    This worked well. Just want to know why did they do this ? Any specific importance of dependencyResolutionManagement in settings.gradle?
    – oyeraghib
    Mar 17, 2022 at 8:06
  • 7
    Or you can set repositoriesMode.set(RepositoriesMode.PREFER_PROJECT) Aug 7, 2022 at 12:34
  • 5
    Gradle loves changing things like it's a high fashion brand when they're a build tool where inertia is valued Oct 15, 2022 at 0:48
270

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' }
    }
}
2
  • 25
    this is better than the accepted answer imo, it uses the new system instead of disabling it. May 6, 2022 at 8:44
  • 2
    this is better than accepted answer. and working ofc Jun 27, 2022 at 6:16
98

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'
0
70

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)
1
  • 1
    Thanks @lava It works just add target & compile SDK version 33. Aug 11, 2022 at 8:16
23

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' }
    }
}
19

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' }
    }
}
19

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! :)

5

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

4

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

if you access repository secured by login/password - try this

here example based on GitHub repo

<settings.gradle file>


groovy

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/OWNER/REPOSITORY")
            credentials {
                username = "${usernameFromYourGradlePropertiesFile}"
                password = "${passwordFromYourGradlePropertiesFile}"
            }
        }
    }
}


kotlin

// ...
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/OWNER/REPOSITORY")

            val userNamePropertyFromGradleProperties: String by settings
            val passwordPropertyFromGradleProperties: String by settings

            credentials {
                username = userNamePropertyFromGradleProperties
                password = passwordPropertyFromGradleProperties
            }
        }
1

Add this line in settings.gradle

See more information here

1
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Jul 21, 2022 at 11:23
1

add it to your settings.gradle inside dependency resolution management like this:

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

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

0

In my case, I just add mavenLocal() in settings.gradle

0
0

You either could delete the

dependencyResolutionManagement{}

block in the

setting.gradle

file and then add the repositories in the

build.gradle

at Project Level as, for example

allprojects {
repositories {
    google()
    mavenCentral()
} }

or write a repository directly in the

dependencyResolutionManagement{}

block. Don't forget to Sync the Project by clicking top-right on

Sync now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.