Gradle and Android : pom configuration with multiple Maven artifacts publication

前端 未结 2 1008
北海茫月
北海茫月 2021-02-08 12:48

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 vers

相关标签:
2条回答
  • 2021-02-08 13:03

    You need(ed) to set the filter name for the groupId and artifactId the same as you have for 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
    

    Im surprised you get away with the version name suffix, as its not semver.

    0 讨论(0)
  • 2021-02-08 13:18

    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)
        }
      }
    
    0 讨论(0)
提交回复
热议问题