Gradle和Android:带有多个Maven工件发布的pomconfiguration

使用Gradle(v 1.7)作为构build工具处理Android库时,我使用了maven插件,并将任务uploadArchivesconfiguration为将发行版和debugging版本的lib发布到本地maven存储库。
下面的代码工作正常:

// [...] apply plugin: 'android-library' // [...] nothing unusual /* * Define name of the apk output file (build/apk/<outputFile>) */ android.libraryVariants.all { variant -> def outputName = "MyModule-${android.defaultConfig.versionName}-${variant.baseName}.aar" variant.outputFile = new File("$buildDir/libs", outputName) } /* * Publish to maven local repo (older style maven plugin) * Used while android plugin is not fixed regarding maven-publish plugin * * type command "gradle uploadArchives" to publish the module into the * local .m2 repository */ apply plugin: 'maven' android.libraryVariants.all { variant -> // add final apk to the 'archives' configuration project.artifacts { archives variant.outputFile } } def localRepoPath = "file://" + new File( System.getProperty("user.home"), ".m2/repository").absolutePath uploadArchives { repositories.mavenDeployer { repository(url: localRepoPath) addFilter('debug') { artifact, file -> artifact.name.contains("debug") } addFilter('release') { artifact, file -> artifact.name.contains("release") } pom('debug').groupId = 'com.company' pom('release').groupId = 'com.company' pom('debug').artifactId = 'id' pom('release').artifactId = 'id' pom('debug').version = android.defaultConfig.versionName + "d" pom('release').version = android.defaultConfig.versionName pom.packaging = 'aar' } } uploadArchives.dependsOn(assemble) 

但是,当试图重构pomconfiguration时:

 uploadArchives { repositories.mavenDeployer { repository(url: localRepoPath) addFilter('debug') { artifact, file -> artifact.name.contains("debug") } addFilter('release') { artifact, file -> artifact.name.contains("release") } pom.groupId = 'com.company' pom.artifactId = 'id' pom('debug').version = android.defaultConfig.versionName + "d" pom('release').version = android.defaultConfig.versionName pom.packaging = 'aar' } } 

artifactId作为输出文件的名称扩展, groupId作为根目录的名称; 从而给在maven回购的坏道路。

我想知道为什么,也许如果有一个更清洁的方式来实现我所需要的。

Related of "Gradle和Android:带有多个Maven工件发布的pomconfiguration"

作为参考,这是我们如何上传多个APK。 由于我们在APK分裂后上传了多个APK,而您尝试从不同的构buildtypes(debugging和发布)上传多个APK,因此可能并不完全符合您的需求。 但从理论上讲,它们应该是一样的。

 //declare some Variables for later use def projectName = "ProjectName" def versionString = "1.0.0" def baseVersionCode = 1 // Values based on https://developer.android.com/ndk/guides/abis.html#sa ext.abiCodes = ['armeabi-v7a': 1, 'arm64-v8a' : 2, 'x86' : 3, 'x86_64' : 4] // collect artifacts, important for the `uploadArchives` to work artifacts { if (new File('app/build/outputs/apk').exists()) { new File('app/build/outputs/apk').eachFile() { file -> if (file.toString().contains("productionRelease")) { archives file: file } } } } uploadArchives { repositories { mavenDeployer { repository(url: "http://...") project.ext.abiCodes.values().each{ abiVersionCode -> def version = "${versionString}.${baseVersionCode + abiVersionCode}" addFilter(version) { artifact, file -> artifact.name.contains(version) } pom(version).artifactId = projectName.toLowerCase() pom(version).groupId = 'com.yourcompanyname' pom(version).version = version } } } } android { defaultPublishConfig "productionRelease" buildTypes { productionRelease { minifyEnabled false zipAlignEnabled true //...more Config like proguard or signing } } applicationVariants.all { variant -> variant.outputs.each { output -> def abiName = output.getFilter(com.android.build.OutputFile.ABI) def abiVersionCode = project.ext.abiCodes.get(abiName) output.versionCodeOverride = variant.versionCode + abiVersionCode output.versionNameOverride = "$versionString (${output.versionCodeOverride})" def apkName = "$projectName-${variant.name}-v${versionString}.${output.versionCodeOverride}.apk" output.outputFile = new File(output.outputFile.parent, apkName) } }