create android dependency with gradle and upload artifact to maven repository

some weeks ago i would like to share a set of util classes from my android project with a colleague. and whats better than create a jar and upload it into a maven repository? the following code show how to create a jar file and upload it to a maven repository by using gradle

apply plugin: 'android'
apply plugin: 'maven'

def computeVersionName() {
     return "0.1.0-SNAPSHOT"
}

android {
     // defaultConfig, buildTypes and stuff

     task AnimationUtilsLibJar(type: Jar) {
          archiveName = "animation-" + computeVersionName()
          from fileTree(dir: 'build/classes/debug')
     }
     artifacts {
          archives AnimationUtilsLibJar
     }
uploadArchives {
     repositories {
          mavenDeployer {
               pom.groupId = 'de.buildpath.utils.android'
               pom.artifactId = 'animation'
               pom.version = computeVersionName()

               Console console = System.console()
               def username = null;
               def password = null;

               if (console != null) {
                    username = new String(console.readLine("\nUsername: "))
                    password = new String(console.readPassword("Password: "))

                    if (computeVersionName().endsWith("-SNAPSHOT")) {
                         snapshotRepository(url: "SNAPSHOT_REPO_URL"){
                            authentication(userName: username, password: password)
                         }
                    } else {
                         repository(url: "RELEASE_REPO_URL") {
                              authentication(userName: username, password: password)
                         }
                    }

                }
            }
        }
    }
}

to upload the artifact you have to open a terminal, change to the root directory of your project and execute the following command

gradle uploadArchives

now you can integrate the dependency in your android project

dependencies {
    compile 'de.buildpath.utils.android:animation:0.1.0-SNAPSHOT'
}

 

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>