Share your Library as a dependency

Rachit Goyal
May 5, 2018 · 10 min read

Maven Central

Setting up your project

Located on the top left of your Project Explorer in Android Studio
// This is app module's build.gradle file
apply plugin: 'com.android.application'
dependencies {
compile project(':segmented')
}
// This is segmented module's build.gradle file
apply plugin: 'com.android.library'
apply from: 'maven-push.gradle' // Add this line to the top
This is the library module - with 2 files added
POM_NAME=SegmentedProgressBar \\ Your Library Name
POM_ARTIFACT_ID=segmentedProgressBar \\ Your Library Name
POM_PACKAGING=aar
/*
* Copyright <YEAR> <YOUR_NAME>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

apply plugin: 'maven'
apply plugin: 'signing'

def isReleaseBuild() {
return VERSION_NAME.contains("SNAPSHOT") == false
}

def getReleaseRepositoryUrl() {
return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
: "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
}

def getSnapshotRepositoryUrl() {
return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
: "https://oss.sonatype.org/content/repositories/snapshots/"
}

def getRepositoryUsername() {
return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}

def getRepositoryPassword() {
return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}

afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

pom.groupId = GROUP
pom.artifactId = POM_ARTIFACT_ID
pom.version = VERSION_NAME

repository(url: getReleaseRepositoryUrl()) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
snapshotRepository(url: getSnapshotRepositoryUrl()) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}

pom.project {
name POM_NAME
packaging POM_PACKAGING
description POM_DESCRIPTION
url POM_URL

scm {
url POM_SCM_URL
connection POM_SCM_CONNECTION
developerConnection POM_SCM_DEV_CONNECTION
}

licenses {
license {
name POM_LICENCE_NAME
url POM_LICENCE_URL
distribution POM_LICENCE_DIST
}
}

developers {
developer {
id POM_DEVELOPER_ID
name POM_DEVELOPER_NAME
}
}
}
}
}
}

signing {
required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
}

//task androidJavadocs(type: Javadoc) {
//source = android.sourceSets.main.allJava
//}

//task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
//classifier = 'javadoc'
//from androidJavadocs.destinationDir
//}

task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}

artifacts {
archives androidSourcesJar
}
}
VERSION_NAME=<YOUR_VERSION_NAME>
VERSION_CODE=<VERSION_CODE>11
GROUP=com.github.<YOUR_GITHUB_USERNAME>

POM_DESCRIPTION=<PROJECT_DESCRIPTION>
POM_URL=https://github.com/<YOUR_GITHUB_USERNAME>/<PROJECT_NAME>
POM_SCM_URL=https://github.com/<YOUR_GITHUB_USERNAME>/<PROJECT_NAME>
POM_SCM_CONNECTION=scm:git@github.com:<YOUR_GITHUB_USERNAME>/<PROJECT_NAME>.git
POM_SCM_DEV_CONNECTION=scm:git@github.com:<YOUR_GITHUB_USERNAME>/<PROJECT_NAME>.git
POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=<YOUR_GITHUB_USERNAME>
POM_DEVELOPER_NAME=<YOUR_FULL_NAME>
VERSION_NAME=0.0.1
VERSION_CODE
=1
GROUP
=com.github.rayzone107

POM_DESCRIPTION
=A library that creates a Custom View
POM_URL
=https://github.com/rayzone107/SegmentedProgressBar
POM_SCM_URL
=https://github.com/rayzone107/SegmentedProgressBar
POM_SCM_CONNECTION
=scm:git@github.rayzone107/SegmentedProgressBar.git
POM_SCM_DEV_CONNECTION
=scm:git@github.rayzone107/SegmentedProgressBar.git
POM_LICENCE_NAME
=The Apache Software License, Version 2.0
POM_LICENCE_URL
=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST
=repo
POM_DEVELOPER_ID
=rayzone107
POM_DEVELOPER_NAME
=Rachit Goyal

Setup GPG (GNU Privacy Guard)

$ gpg --gen-key
pub rsa2048 2018–05–05 [SC] [expires: 2020–05–04]
X4EC727BA47X77E9D95060B96DE3B7EG93EA0576
uid Full Name<email_id@domain.com>
sub rsa2048 2018–05–05 [E] [expires: 2020–05–04]
$ gpg --keyserver hkp://keyserver.ubuntu.com --send-keys <KEY>
$ gpg --keyserver hkp://pgp.mit.edu --send-keys <KEY>
$ gpg --keyserver hkp://pgp.mit.edu --search-keys <EMAIL_ID>

Upload project on Maven Central

The complete issue might look something like this.

Publish Project

NEXUS_USERNAME=<SONATYPE_USERNAME>
NEXUS_PASSWORD
=<SONATYPE_PASSWORD>
signing.keyId
=<GPG_KEY>
signing.password
=<GPG_KEY_PASSWORD>
signing.secretKeyRingFile
=/Users/<SYSTEM_USERNAME>/.gnupg/secring.gpg
org.gradle.daemon
=true

Library Usage

dependencies {
compile 'com.github.lawloretienne:trestle:0.0.3'
}

Updating Library


JitPack

dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.<YOUR_GITHUB_NAME>'
Click the “Releases” Tab
Click on “Draft a new Release”
Set a “Tag version”

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade