GitHub Packages发布Android库报错:存在重复aar构件
问题解决:发布Android库到GitHub Packages时的重复Artifact错误
我尝试按照Medium文章《Publishing Android libraries to GitHub Packages》的步骤,将Android库发布到GitHub Packages。执行./gradlew publish命令时,出现错误:Invalid publication 'release': multiple artifacts with the identical extension and classifier ('aar', 'null')。
我的build.gradle配置如下:
import java.util.Properties plugins { id("com.android.library") id("org.jetbrains.kotlin.android") id("maven-publish") } val getVersionName = "1.0.0" val getArtifactId = "dynamic-map" val githubProperties = Properties() githubProperties.load( rootProject.file("github.properties").inputStream() ) // Load GitHub credentials from github.properties file android { ... } dependencies { ... } afterEvaluate { val gitUsername = githubProperties.getProperty("gpr.usr") val gitPassword = githubProperties.getProperty("gpr.key") println("GitHub Username: $gitUsername") println("GitHub Token: $gitPassword") publishing { publications { create<MavenPublication>("release") { from(components["release"]) groupId = "com.gaurav.kumar" artifactId = getArtifactId version = getVersionName artifact("$buildDir/outputs/aar/${getArtifactId}-release.aar") } } repositories { maven { name = "DynamicMapRepo" url = uri("https://maven.pkg.github.com/Cypher103360/DynamicMap") credentials { username = gitUsername password = gitPassword } } } } }
执行命令后的错误日志:
gaurav@gaurav-ThinkPad-E15-Gen-4:~/AndroidStudioProjects/DynamicMap$ ./gradlew publish > Configure project :dynamic-map GitHub Username: Cypher103360 GitHub Token: ***** > Task :dynamic-map:publishReleasePublicationToDynamicMapRepoRepository FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':dynamic-map:publishReleasePublicationToDynamicMapRepoRepository'. > Failed to publish publication 'release' to repository 'DynamicMapRepo' > Invalid publication 'release': multiple artifacts with the identical extension and classifier ('aar', 'null'). * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 1s 25 actionable tasks: 21 executed, 4 up-to-date
错误原因
你在MavenPublication中重复添加了同一个aar artifact:
from(components["release"]):Gradle会自动将release组件生成的aar文件纳入发布内容- 手动调用
artifact("$buildDir/outputs/aar/${getArtifactId}-release.aar"):再次添加了完全相同的aar文件
这导致发布包中出现两个重复的aar,触发了Gradle的重复检测错误。
解决方法
删除手动添加的artifact(...)语句,仅保留from(components["release"])即可。Gradle会自动处理release组件对应的artifact,无需手动指定文件路径。
修改后的publishing配置片段:
publishing { publications { create<MavenPublication>("release") { from(components["release"]) groupId = "com.gaurav.kumar" artifactId = getArtifactId version = getVersionName // 移除重复的artifact声明 // artifact("$buildDir/outputs/aar/${getArtifactId}-release.aar") } } repositories { maven { name = "DynamicMapRepo" url = uri("https://maven.pkg.github.com/Cypher103360/DynamicMap") credentials { username = gitUsername password = gitPassword } } } }
修改完成后重新执行./gradlew publish,即可正常发布到GitHub Packages。
内容的提问来源于stack exchange,提问作者Cypher
相关产品推荐
相关产品推荐

