【问题标题】:Gradle and Android : pom configuration with multiple Maven artifacts publicationGradle 和 Android:具有多个 Maven 工件发布的 pom 配置
【发布时间】:2013-08-23 10:27:32
【问题描述】:

在使用 Gradle (v 1.7) 作为构建工具的 Android 库上工作时,我使用了 maven 插件并配置了任务 uploadArchives 以将 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)

但是,当尝试重构 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'
    }
}

artifactId扩展为输出文件名,groupId扩展为根目录名;因此在 maven repo 中给出了错误的路径。

我想知道为什么会这样,也许有更清洁的方法来实现我的需要。

【问题讨论】:

  • 在我的项目中,我需要重命名输出.aar。因此,我在build.gradle 中设置了archivesBaseName,用于artifactId。也许这会有所帮助。
  • 你明白了吗?
  • @YuchenZhong 很遗憾没有,因为这项工作是短期实习的一部分。我不知道谁负责这个项目,也不知道后来是如何处理的。

标签: android gradle


【解决方案1】:

作为参考,这是我们上传多个 APK 的方式。这可能不是您所需要的,因为我们在 APK 拆分后上传了多个 APK,而您尝试从不同的构建类型(调试和发布)上传多个 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)
    }
  }

【讨论】:

  • 很棒的 +1。正是我需要在 abis 上拆分的内容。刚刚为其他开发者添加了一些缺失的部分
  • 嘿,@RafaelT 我不认为zipAlignEnabled true 是必需的,因为它默认设置为true 用于生产构建。 stackoverflow.com/a/27945728/1035008
  • 是的,我刚刚添加了 buildType productionRelease,因为它完全丢失了@Yuchen Zhong
【解决方案2】:

您需要(ed)将groupIdartifactId 的过滤器名称设置为与version 的过滤器名称相同

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

我很惊讶你居然没有使用版本名称后缀,因为它不是 semver。

【讨论】:

  • “逍遥法外”是什么意思?如果调试版本是预发布的,我会说它是,那么该格式几乎是 semver 兼容的,只是缺少一个连字符。
猜你喜欢
  • 2015-05-27
  • 2021-02-19
  • 2015-02-13
  • 1970-01-01
  • 2011-05-25
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多