Gradle 8:':publishMavenPublicationToMavenRepository' uses this output of task ':bundleReleaseAar' without declaring an explicit or implicit dependency

huangapple go评论50阅读模式
英文:

Gradle 8:':publishMavenPublicationToMavenRepository' uses this output of task ':bundleReleaseAar' without declaring an explicit or implicit dependency

问题

以下是您要翻译的代码部分:

  1. 当我将Gradle包装器更新为使用Gradle 8.0时,出现了此问题。我遇到了这两个错误:
1. 任务':publishMavenPublicationToMavenRepository' 使用了任务':androidSourcesJar' 的输出,而没有声明显式或隐式依赖关系。这可能导致根据任务执行顺序产生不正确的结果。

2. 任务':publishMavenPublicationToMavenRepository' 使用了任务':bundleReleaseAar' 的输出,而没有声明显式或隐式依赖关系。这可能导致根据任务执行顺序产生不正确的结果。

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

当我尝试发布我的库时,我遇到了这些错误。

我已经有解决方案,并将在解决方案部分发布。

英文:

I had this issue when I updated my gradle-wrapper to use Gradle 8.0. I had this two errors:

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.

My build.gradle file looked like this.

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
}

I was receiving these errors when I was trying to publish my library.

I already have solution and I will post it under the solution section.

答案1

得分: 2

现在我的 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
                }
            }
        }
    }
}

基本上,我必须添加一个 afterEvaluate 标签以发布 Android 库。

此外,我移除了发布 aar 文件的路径,因为 from components.release 会自动处理这个问题。

希望这能帮助像我一样在这个特定问题上找不到答案的人。

英文:

I found the answer.

Now my build.gradle file looks like this:

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

Basically, I had to add an afterEvaluate tag for publication of the Android library.

Also, I removed the path from which the aar file would be published because from components.release does this for me.

I hope this helps people like me who were not able to find an answer to this particular problem.

答案2

得分: 2

Mayank Pandit,你在你的回答中是正确的(这里),将publishing块嵌套在afterEvaluate块内,并将artifact("$buildDir/outputs/aar/libraryname-release.aar")声明替换为from components.release

如果你比较一下你问题和回答中的build.gradle文件,你会注意到你还删除了artifact(sourceJar)的声明。这意味着你的Android库现在将不再包括源代码。

如果你想要发布带有源代码和支持Javadoc的Android库,你需要对你的build.gradle文件进行两项更改,如下所述。

首先,在android块内添加一个publishing块,如下所示:

android {
  publishing {
    singleVariant("release") {
        withJavadocJar()
        withSourcesJar()
    }
  }
}

其次,将maven(MavenPublication)更改为release(MavenPublication)

在进行这两项更改后,你可以摆脱sourceJar任务。

请参考Android Gradle插件 > 发布你的库文档页面,获取有关如何发布你的Android库的最新指南。

英文:

Mayank Pandit, you're right in your answer (here) to embed the publishing block within an afterEvaluate block and to replace the artifact("$buildDir/outputs/aar/libraryname-release.aar") declaration with from components.release.

If you compare the build.gradle files in your question and answer, you'll see that you've also removed the artifact(sourceJar) declaration. This means your Android library will now be published without its sources.

If you want to publish your Android library with its sources and supporting Javadoc, you need to make two changes to your build.gradle file, described below.

Firstly, add a publishing block within the android block as follows:

android {
  publishing {
    singleVariant("release") {
        withJavadocJar()
        withSourcesJar()
    }
  }
}

Secondly, change maven(MavenPublication) to release(MavenPublication).

After making these two changes, you can get rid of the sourceJar task.

Refer to the Android Gradle Plugin > Publish your library documentation page for the latest guidance on how to publish your Android libraries.

huangapple
  • 本文由 发表于 2023年4月6日 23:15:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定