How to publish build info along with the artifacts into Artifactory from a Gradle Android Project?

Amith Kumar Mutakari
2020-06-22 14:19

 

This article explains how to publish the artifacts along with the Build info from a Gradle Android project and the mandatory things we need to include in the build.gradle,gradle-wrapper.properties file.

1. Perform a Git clone in your client machine https://github.com/jfrog/project-examples.  
2. In gradle-android example (https://github.com/jfrog/project-examples/tree/master/gradle-examples/gradle-android-example) make sure the following settings are in place before running the gradle publish command.   
$ cat build.gradle 

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.3'
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.15.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    apply plugin: 'com.jfrog.artifactory'
    apply plugin: 'maven-publish'
   // apply plugin: 'java'
    repositories {
        google()
        jcenter()
    }
}
artifactoryPublish.skip = true
project('app') {
    artifactoryPublish.dependsOn('build')
    publishing {
        publications {
            app(MavenPublication) {
               // from components.java
                groupId = group
                artifactId = project.getName()
                version = currentVersion
                artifact("$buildDir/outputs/apk/release/app-release-unsigned.apk")
            }
        }
    }
    artifactoryPublish {
        publications(publishing.publications.app)
    }
}
project('library') {
    artifactoryPublish.dependsOn('build')
    publishing {
        publications {
            aar(MavenPublication) {
               // from components.java
                groupId = group
                artifactId = project.getName()
                version = currentVersion
                // Tell maven to prepare the generated "*.aar" file for publishing
                artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
            }
        }
    }
    artifactoryPublish {
       publications(publishing.publications.aar)
   }
}
artifactory {
    clientConfig.setIncludeEnvVars(true)
    clientConfig.info.addEnvironmentProperty('test.adding.dynVar',new Date().toString())
    contextUrl = 'http://localhost:8081/artifactory';
    publish {
        repository {
            repoKey = 'libs-snapshot-local' // The Artifactory repository key to publish to
            username = "admin" // The publisher user name
            password = "password1" // The publisher password
            // This is an optional section for configuring Ivy publication (when publishIvy = true).
            ivy {
                ivyLayout = '[organization]/[module]/ivy-[revision].xml'
                artifactLayout = '[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]'
                mavenCompatible = true //Convert any dots in an [organization] layout value to path separators, similar to Maven's groupId-to-path conversion. True if not specified

            }
        }
        defaults {
            // Reference to Gradle publications defined in the build script.
            // This is how we tell the Artifactory Plugin which artifacts should be
            // published to Artifactory.
            publications('app', 'aar')
            publishArtifacts = true

            // Properties to be attached to the published artifacts.

            properties = ['qa.level': 'basic', 'dev.team' : 'core']

            publishPom = true // Publish generated POM files to Artifactory (true by default)
        }
    }
}
System.setProperty('javax.net.ssl.trustStore', 'cacerts')
System.setProperty('javax.net.ssl.trustStorePassword', 'changeit')

In the gradle-wrapper.properties I have changed the gradle version to 6.4 .

$ cat gradle/wrapper/gradle-wrapper.properties 


#Sun Dec 03 11:42:26 IST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-6.4-all.zip

In this example, I have used
Gradle Build tools 3.6.3 version in the build.gradle, JFrog Build info extractor as 4.15.2 in build.Gradle and in gradle-wrapper.properties I have used Gradle version as 6.4

Now run the artifactoryPublish with gradlew (gradlewrapper)
$ ./gradlew clean artifactoryPublish


> Task :app:lint
Ran lint on variant release: 5 issues found
Ran lint on variant debug: 5 issues found
Wrote HTML report to file:///Users/amithkm/project-examples/gradle-examples/gradle-android-example/app/build/reports/lint-results.html
Wrote XML report to file:///Users/amithkm/project-examples/gradle-examples/gradle-android-example/app/build/reports/lint-results.xml
> Task :library:lint
Ran lint on variant debug: 3 issues found
Ran lint on variant release: 3 issues found
Wrote HTML report to file:///Users/amithkm/project-examples/gradle-examples/gradle-android-example/library/build/reports/lint-results.html
Wrote XML report to file:///Users/amithkm/project-examples/gradle-examples/gradle-android-example/library/build/reports/lint-results.xml
[pool-65-thread-2] Deploying artifact: http://localhost:8081/artifactory/libs-snapshot-local/gradle-android-example/app/1.0-SNAPSHOT/app-1.0-SNAPSHOT.apk
[pool-65-thread-3] Deploying artifact: http://localhost:8081/artifactory/libs-snapshot-local/gradle-android-example/library/1.0-SNAPSHOT/library-1.0-SNAPSHOT.aar
[pool-65-thread-3] Deploying artifact: http://localhost:8081/artifactory/libs-snapshot-local/gradle-android-example/library/1.0-SNAPSHOT/library-1.0-SNAPSHOT.pom
[pool-65-thread-2] Deploying artifact: http://localhost:8081/artifactory/libs-snapshot-local/gradle-android-example/app/1.0-SNAPSHOT/app-1.0-SNAPSHOT.pom
> Task :artifactoryDeploy
Deploying build descriptor to: http://localhost:8081/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under http://localhost:8081/artifactory/webapp/builds/gradle-android-example/1589806919408
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.4/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 9s
121 actionable tasks: 113 executed, 8 up-to-date

The above example will publish artifacts and build info to Artifactory.