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' }