119

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
  • 1,403
  • 2
  • 8
  • 14

10 Answers10

172

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.

cod3monk3y
  • 8,918
  • 5
  • 38
  • 51
YCuicui
  • 1,752
  • 1
  • 7
  • 2
  • thanks, but I also had to remove "testImplementation 'junit:junit:'" from build.gradle – Cor Jan 14 '22 at 10:20
  • 1
    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
  • 2
    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
120

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
  • 1,201
  • 1
  • 5
  • 3
63

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,114
  • 8
  • 14
20

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
  • 2,903
  • 2
  • 21
  • 19
15

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
  • 941
  • 8
  • 16
14

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

3

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
  • 565
  • 5
  • 7
3

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' }
    }
}
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 delete the dependencyResolutionManagement{...} statement that in the settings.gradle the new project is default added in settings.gradle

normidar
  • 77
  • 4