androidgradleartifactoryandroid-build-type

Get BuildType VersionName for ArtifactoryPublication


I am using the following gradle to create a snapshot build type with a filename including the current commit hash and "SNAPSHOT" as suffix:

def libraryGroupId = 'my.group.id'
def libraryVersion = '1.7.2'
def libraryArtifactId = 'core'

android {
    defaultConfig {
        versionName libraryVersion
    }
    buildTypes {
        debug {
            versionNameSuffix '-debug'
       }
        snapshot {
            versionNameSuffix '-SNAPSHOT-' + getCommitHash()
        }
    }
    libraryVariants.all { variant ->
        variant.outputs.all {
            def versionName = variant.variantData.variantConfiguration.versionName
            outputFileName = "${libraryArtifactId}-" + versionName + ".aar"
        }
    }
}

Furthermore I have an artifactory publishing task:

publishing {
    publications {
        aar(MavenPublication) {
            groupId libraryGroupId
            version libraryVersion
            artifactId libraryArtifactId

            def artifactUrl = "$buildDir/outputs/aar/<??outputFileName??>"
            println "#### artifactUrl: " + artifactUrl
            artifact(artifactUrl)

            pom.withXml {
                ...
            }
        }
    }
}

Right now the "outputFileName" is always set to the last defined build type since variant.outputs.all is overriding it for each build type. So my question is:

How do I get the outputFileName of my current build within the artifactory publication closure?

Any other way is welcome. It doesn't have to be via a self-defined variable. But the artifactoryUrl should match with the generated /outputs/aar/<snapshot-filename>.aar.


Solution

  • You can generate publish task for each of your buildVariant.

    publishing {
        publications {
            android.libraryVariants.all { variant ->
            "aar${variant.name.capitalize()}"(MavenPublication) {
              def versionName = variant.variantData.variantConfiguration.versionName
              artifact("$buildDir/outputs/aar/${libraryArtifactId}-${versionName}.aar")
            }
          }
        }    
    }
    

    and then call the right task - it should be something like publishAarSnapshotPublicationToMavenRepository