Gradle和Android:带有多个Maven工件发布的pom配置 [英] Gradle and Android : pom configuration with multiple Maven artifacts publication

查看:190
本文介绍了Gradle和Android:带有多个Maven工件发布的pom配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  // [...] 
apply plugin:' android-library'

// [...]没有什么不寻常的

/ *
*定义apk输出文件的名字(build / apk /< outputFile>)
* /
android.libraryVariants.all
{
variant - >
def outputName =MyModule - $ {android.defaultConfig.versionName} - $ {variant.baseName} .aar
variant.outputFile = new File($ buildDir / libs,outputName)


$ b / *
*发布到maven本地回购(旧式maven插件)
*在Android插件未针对maven-publish插件修复时使用
*
* type命令gradle uploadArchives将模块发布到
*本地.m2存储库
* /
apply plugin:'maven'

android.libraryVariants.all
{
variant - >
//将最终apk添加到'archives'配置
project.artifacts
{
archives variant.outputFile
}
}

def localRepoPath =file://+ new File(
System.getProperty(user.home),.m2 / repository)。absolutePath
$ b $ upload uploadchives
{
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(汇编)

但是,当试图重构pom配置:

  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'


$ / code $ / pre
$ b

artifactId 被展开为名称的输出文件, groupId 作为根目录的名称;因此在maven回购中给了不好的道路。



我想知道为什么会这样,也许如果有更清晰的方法来实现我所需要的。 p>

解决方案

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

  //声明一些变量供以后使用
def projectName = ProjectName
def versionString =1.0.0
def baseVersionCode = 1

//基于https://developer.android.com/ndk/guides/abis的值.html#sa
ext.abiCodes = ['armeabi-v7a':1,
'arm64-v8a':2,
'x86':3,
'x86_64' :4]

//收集工件,对于`uploadArchives`工作非常重要
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 {
defaultPublishConfigproductionRelease

buildTypes {
productionRelease {
minifyEnabled false
zipAlignEnabled true
//...more Config like proguard or signing
}
}

applicationVariants.all {variant - >
variant.outputs.each {输出 - >
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)
}
}


Working on an Android library with Gradle (v 1.7) as the building tool, I've used the maven plugin and configured the task uploadArchives to publish both release and debug version of the lib to the local maven repository.
The code below works ok :

// [...]
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)

However, when trying to refactor the pom configuration :

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 is expanded as the name of the output file, and groupId as the name of the root directory ; thus giving bad paths in the maven repo.

I'd like to know why is that, and maybe if there is a cleaner way to achieve what I need.

解决方案

As a reference, this is how we upload multiple APKs. It may not be exactly what you needed since we are uploading multiple APKs after APK splits, while you were trying to upload multiple APKs from different build types (debug & release). But in theory, they should be the same.

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

这篇关于Gradle和Android:带有多个Maven工件发布的pom配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆