当我更新Gradle -wrapper使用Gradle 8.0时,我遇到了这个问题。我有两个错误:
1. Task ':publishMavenPublicationToMavenRepository' uses this output of task ':androidSourcesJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
2. Task ':publishMavenPublicationToMavenRepository' uses this output of task ':bundleReleaseAar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
我的build.gradle
文件是这样的。
buildscript {
...
}
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
task sourceJar(type: Jar) {
from android.sourceSets.main.jniLibs.srcDirs
classifier "sources"
}
version = '8.0.0'
android {
...
}
dependencies {
...
}
android.libraryVariants.all {
def aptOutputDir = new File(buildDir, "generated/source/apt/${it.unitTestVariant.dirName}")
it.unitTestVariant.addJavaSourceFoldersToModel(aptOutputDir)
}
publishing {
publications {
maven(MavenPublication) {
version = version
artifactId = "your_artifact_id"
groupId = "com.yourgroup.id"
artifact(sourceJar)
artifact("$buildDir/outputs/aar/libraryname-release.aar")
}
}
repositories {
maven {
def releasesUrl = "releaseurl_ifavailable"
def snapshotsUrl = "snapshoturl_ifavailable"
url = version.endsWith('SNAPSHOT') ? snapshotsUrl : releasesUrl
credentials {
username = your_username
password = your_password
}
}
}
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives androidSourcesJar
}
当我试图发布我的库时,我收到了这些错误。
我已经有了解决方案,我会把它贴在解决方案部分。
我找到了答案。
现在我的build.gradle
文件看起来像这样:
buildscript {
...
}
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
version = '8.0.0'
android {
...
}
dependencies {
...
}
android.libraryVariants.all {
def aptOutputDir = new File(buildDir, "generated/source/apt/${it.unitTestVariant.dirName}")
it.unitTestVariant.addJavaSourceFoldersToModel(aptOutputDir)
}
task sourceJar(type: Jar) {
from android.sourceSets.main.jniLibs.srcDirs
archiveClassifier = "sources"
}
task androidSourcesJar(type: Jar) {
archiveClassifier.set("sources")
from android.sourceSets.main.java.sourceFiles
}
afterEvaluate {
publishing {
publications {
maven(MavenPublication) {
from components.release
version = version
artifactId = "your_artifactId"
groupId = "com.yourgroup.id"
}
}
repositories {
maven {
def releasesUrl = "releaseurl_ifavailable"
def snapshotsUrl = "snapshoturl_ifavailable"
url = version.endsWith('SNAPSHOT') ? snapshotsUrl : releasesUrl
credentials {
username = your_username
password = your_password
}
}
}
}
}
基本上,我必须为Android库的发布添加一个afterEvaluate
标签。
另外,我删除了aar文件将要发布的路径,因为from components.release
为我做了这件事。
我希望这能帮助像我一样无法找到这个特定问题的答案的人。
Mayank Pandit,你的答案是正确的(在这里),将publishing
块嵌入afterEvaluate
块中,并用from components.release
替换artifact("$buildDir/outputs/aar/libraryname-release.aar")
声明。
如果您比较问题和答案中的build.gradle
文件,您将看到您也删除了artifact(sourceJar)
声明。这意味着你的Android库现在将在没有源代码的情况下发布。
如果你想发布Android库及其源代码并支持Javadoc,你需要对build.gradle
文件做两个修改,如下所述。
首先,在android
块中添加publishing
块,如下所示:
android {
publishing {
singleVariant("release") {
withJavadocJar()
withSourcesJar()
}
}
}
其次,将maven(MavenPublication)
更改为release(MavenPublication)
。
完成这两个更改后,您可以摆脱sourceJar
任务。
参考Android Gradle Plugin>发布你的库文档页面,获取关于如何发布Android库的最新指导。