我想添加jitpack。IO作为一个存储库在我的gradle文件。 这是我的gradle根文件:

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
}

因为我没有一个“allrepositories”来把我的依赖放在那里(只在那里工作),我已经创建并在buildscript代码后添加了这段代码:

allprojects {
    repositories {
        maven {url 'https://www.jitpack.io'}
    }
}

但这是我得到的错误

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'

Gradle 6.8引入了存储库的中央声明,定义存储库的新方法。最新的文档(7.4.2)可以在这里找到。

从设置中移除dependencyresoltionmanagement块。Gradle文件让你的项目以旧的方式工作。


您可以添加jitpack。作为settings.gradle中的dependencyresoltionmanagement中的存储库

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

您需要在“设置”中更新设置。gradle和更改repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)为repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

最后,添加maven {url 'https://jitpack。Io '}到存储库块。

完整的设置。Gradle文件看起来是这样的:

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'

在gradle '7.1.0'版本中只需要添加maven {url 'https://jitpack。Io '}设置。gradle

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

解决方案:

您可以添加此url 在设置。gradle(项目设置)文件, 你可以在Gradle脚本中找到,

像这样在dependencyresoltionmanagement中添加你的url

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

#参见下面的图片以获得完整的参考,

现在同步, 它会起作用,

谢谢你! :)


随着Android工作室的更新,你必须控制你的依赖设置。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" 包括:应用程序的

请把这条线放在呼吸器上

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

替换这一行:

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

用这个:

repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

之前

  repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

一些旧的建议让你添加maven {url "https://jitpack。Io "}进入根构建。Gradle,但只要你使用的是最新版本的as,你可以简单地把它放入设置。Gradle而不是build.gradle


进入设置。Gradle并将其放入存储库中

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

在我的例子中,我只是删除了dependencyresoltionmanagement{…}语句,在设置。Gradle新项目默认添加在settings.gradle中


在settings.gradle中添加这一行

点击这里查看更多信息


在我的例子中,我只是在settings.gradle中添加了mavenLocal()


如果您访问由登录名/密码保护的存储库,请尝试此操作

这里的例子基于GitHub回购

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

你可以删除

dependencyResolutionManagement{}

setting.gradle

文件中添加存储库

build.gradle

例如,在项目级别

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

或直接在

dependencyResolutionManagement{}

块。不要忘记通过点击右上角来同步项目

现在同步


将其添加到您的设置中。Gradle内部依赖解析管理如下:

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