Search code examples
androidmavengradleartifactory

How to use artifactoryPublish to publish release and debug artifacts


I have Android Studio projects that build AARs or APKs in both release and debug versions. I'd like to publish these to different to different repositories on my Artifactory server. The JFrog examples don't appear to cover this case.

Does this mean that it's considered best practice to simply build either only the release or only the debug version, and choose what and where to upload based on the type of build?


Solution

  • I configured my android library build.gradle file that compiled aar file could be uploaded in different repos, dependent on build type.
    For example you want to publish debug artifats to 'libs-debug-local' repository and release artifacts to 'libs-release-local' repository.

    //First you should configure all artifacts you want to publish
    publishing {
        publications {
    
            //Iterate all build types to make specific 
            //artifact for every build type
            android.buildTypes.all { variant ->
    
                //it will create different 
                //publications ('debugAar' and 'releaseAar')
                "${variant.name}Aar"(MavenPublication) {
                    def manifestParser = new com.android.builder.core.DefaultManifestParser()
    
                    //Set values from Android manifest file
                    groupId manifestParser.getPackage(android.sourceSets.main.manifest.srcFile)
                    version = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
                    artifactId project.getName()
    
                    // Tell maven to prepare the generated "*.aar" file for publishing
                    artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar")
                }
            }
        }
    }
    
    //After configuring publications you should
    //create tasks to set correct repo key
    android.buildTypes.all { variant ->
    
        //same publication name as we created above
        def publicationName = "${variant.name}Aar"
    
        //new task name
        def taskName = "${variant.name}Publication"
    
        //in execution time setting publications and repo key, dependent on build type
        tasks."$taskName" << {
            artifactoryPublish {
                doFirst {
                    publications(publicationName)
                    clientConfig.publisher.repoKey = "libs-${variant.name}-local"
                }
            }
        }
    
        //make tasks assembleDebug and assembleRelease dependent on our new tasks
        //it helps to set corrent values for every task
        tasks."assemble${variant.name.capitalize()}".dependsOn(tasks."$taskName")
    }
    
    //Inside artifactory block just set url and credential, without setting repo key and publications
    artifactory {
        contextUrl = 'http://artifactory.cooperok.com:8081/artifactory'
        publish {
            repository {
                username = "username"
                password = "password"
            }
            defaults {
                publishArtifacts = true
    
                // Properties to be attached to the published artifacts.
                properties = ['qa.level': 'basic', 'dev.team': 'core']
            }
        }
    }
    

    That's all. Now if you run command

    Win : gradlew assembleRelease artifactoryPublish Mac : ./gradlew assembleRelease artifactoryPublish

    aar file will be uploaded to 'libs-release-local' repository.
    And if you ran

    Win : gradlew assembleDebug artifactoryPublish Mac : ./gradlew assembleDebug artifactoryPublish

    it will be uploaded to 'libs-debug-local' repository

    One minus of this configuration is that you should always run artifactoryPublish task with assembleDebug/Release tasks