303

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'
André Nogueira
  • 3,363
  • 3
  • 10
  • 16

15 Answers15

431

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' }
    }
}
rxszt
  • 3
  • 2
YCuicui
  • 4,363
  • 1
  • 8
  • 4
  • thanks, but I also had to remove "testImplementation 'junit:junit:'" from build.gradle – Cor Jan 14 '22 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 – Vivek Thummar Jan 26 '22 at 09:10
  • 5
    This worked well. Just want to know why did they do this ? Any specific importance of dependencyResolutionManagement in settings.gradle? – oyeraghib Mar 17 '22 at 08:06
  • Where u were Bro.... ? i was wondering this solution from last 3 days..... Thanks Brother... u r a life saver... – Aqeel Mughal Apr 04 '22 at 06:19
  • thanks for the tip. but why did that in the first place ? – other Tall guy Apr 11 '22 at 06:22
  • You might face the same issue @VivekThummar and I did after you get rid of the block. That is because with the new way of declaring repositories the `allprojects` block in the project build.gradle file is not included. You will have to add it to the file so the project knows where to find dependencies. – Adetunji Mohammed Jun 26 '22 at 19:36
  • Hi, I'm having the same problem but I don't know how to fix it. – gabrielfalieri Aug 01 '22 at 13:05
  • 8
    Or you can set `repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)` – desertSniper87 Aug 07 '22 at 12:34
  • 8
    Gradle loves changing things like it's a high fashion brand when they're a build tool where inertia is valued – georgiecasey Oct 15 '22 at 00:48
  • 1
    Great answer with both old and new examples. That makes life a lot easier when you have inherited a 5 year old codebase. Searching for android info is frustrating because google will point you to old and outdated info. – bigh_29 May 23 '23 at 15:58
  • It is possible that 'settings.gradle' already has that repositories, so you could just delete old declaration of repositories and it will work – B-GangsteR Aug 23 '23 at 07:57
275

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' }
    }
}
wwilczyn
  • 2,751
  • 1
  • 6
  • 4
106

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'
Seun Matt
  • 1,660
  • 1
  • 9
  • 14
  • PREFER_SETTINGS only makes sense if you want to ignore declarations in project instead of getting the error. If you want to move them FAIL_ON_PROJECT_REPOS is better, I think. – The incredible Jan Jun 12 '23 at 10:56
71

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)
lava
  • 6,020
  • 2
  • 31
  • 28
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' }
    }
}
Ryadh Hanafi
  • 231
  • 2
  • 4
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' }
    }
}
Mori
  • 2,653
  • 18
  • 24
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

normidar
  • 445
  • 5
  • 11
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 
     `  
   
 
Mazhar Iqbal
  • 813
  • 7
  • 7
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
            }
        }
Grzegorz Dev
  • 3,073
  • 1
  • 18
  • 22
2

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

Lucas
  • 458
  • 4
  • 6
1

Add this line in settings.gradle

See more information here

  • 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](/review/late-answers/32273912) – The Dreams Wind Jul 21 '22 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' }
    }
}
musangi
  • 31
  • 4
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

Tenshi
  • 1
  • 1