create account

Linking jcenter Android Archive (AAR) library to Maven Central Part 2 by kabooom

View this thread on: hive.blogpeakd.comecency.com
· @kabooom · (edited)
$80.01
Linking jcenter Android Archive (AAR) library to Maven Central Part 2
### Repository
[https://github.com/aosp-mirror](https://github.com/aosp-mirror)

### Overview
 
In [Part 1](https://busy.org/@kabooom/creating-android-archive-aar-publishing-to-jcenter-and-using-it-in-android-project) we published our Android Archive (AAR) library to jcenter, in this tutorial we will link jcenter android aar library to maven central. After successful linking our library will be available on both jcenter and maven central.

### Requirements
- Android Studio 3.0 or higher
- Android NDK
- GnuPG

### Difficulty
-  Intermediate

### Tutorial covers
* Android Archive (AAR) Library project
* Requirements for maven central linking
    1. Creating maven jira ticket
    2. Repository must be linked to jcenter
    3. Generating additional artifacts
    4. Adding info in POM file
    5. Sign artifacts using GnuPG
    5.1.  Creating GPG Key Pair
    5.2. Exporting public and private key
    5.3. Uploading keys to bintray
    5.4. Automatic signing artifacts
    5.5. Uploading artifacts
* Linking to Maven Central

### Guide

### Android Archive (AAR) Library project

Project in this tutorial is exactly same as we created in [Part 1](https://busy.org/@kabooom/creating-android-archive-aar-publishing-to-jcenter-and-using-it-in-android-project) in this tutorial we will only change **build.gradle** file. In this tutorial we will create new version that will include additional artifacts and files necessary for linking to maven central. 

Delete older version of library on [https://bintray.com](https://bintray.com) open your repository and click on version and then click on Edit

![pic13.png](https://ipfs.busy.org/ipfs/QmZLEegMLU7zLZ8viV6q36FdmArkDk21CvJFuhVocKmQ5n)

Scroll down and click on Delete Version button to delete this version

 ![pic14.png](https://ipfs.busy.org/ipfs/QmYjhmp3krPzjPWsDLGkg4mQWjJV72qHLpcvV2QYjfw7dF)

### Requirements for maven central linking
Here are list of [requirements](https://central.sonatype.org/pages/requirements.html#a-complete-example-pom) for maven central linking.

#### 1. Creating maven jira ticket

In [Part 1](https://busy.org/@kabooom/creating-android-archive-aar-publishing-to-jcenter-and-using-it-in-android-project) we created this artifact and pushed to jcenter

```groovy
aar(MavenPublication) {
    groupId 'com.faob'
    artifactId archiveName
    version libVersion
    artifact("$buildDir/outputs/aar/$archiveName-release.aar")
}
```
groupId we used in this artifact is **com.faob** and groupId is usually your domain in reverse form. On of the disadvantages of maven central is that we can't use custom groupId you must own the domain  but in jcenter we can use any groupId. In my case i don't own this domain **faob.com** so i have to change this to my project hosting domain which is github which works with maven central.

In this section we make a request to **sonatype.org** to assign groupId to us when we get our groupId then we can link our jcenter repo with maven central. First create account here [https://issues.sonatype.org](https://issues.sonatype.org) then login to your account and open dashboard.  Click on create button to create issue then enter Summary, Description, Grout Id, Project URL, SCM URL and leave others as defaults then click on Create button to create issue

```
Summary = Publish android library
Description = I want to publish my android library to maven central
Group Id = io.github or com.github
Project URL = https://github.com/faob-dev/Tutorials
SCM URL = git@github.com:faob-dev/Tutorials.git
```
After few hours you will get reply something like that 
```
io.github.faob-dev has been prepared.....
```
They assigned me this groupId **io.github.faob-dev** my github username has been appended to create unique groupId. Change groupId in **build.gradle** file

```groovy
aar(MavenPublication) {
    groupId 'io.github.faob-dev'
    artifactId archiveName
    version libVersion
    .........
    .........
}
```

#### 2. Repository must be linked to jcenter

In order to link to maven central, repository in [https://bintray.com](https://bintray.com) must be linked to jcenter as explained in [Part 1](https://busy.org/@kabooom/creating-android-archive-aar-publishing-to-jcenter-and-using-it-in-android-project) it should look like this

![p20.png](https://steemitimages.com/DQmNPVNx4nz5QUoMp2RNBYLwF7TK5jkJCpLBosQT9NL15Zg/p20.png)

#### 3. Generating additional artifacts

Our package should contain jar file for sources and optional jar file for docs. To create additional jars open  **build.gradle** file and add these line to create sources and docs jars

```groovy
task sourcesJar(type: Jar) {
    classifier "sources"
    from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
}

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

artifacts {
    archives javadocJar
    archives sourcesJar
}
```
Next step is to add those artifacts to publishing task so that those artifacts can be uploaded to [https://bintray.com](https://bintray.com)

```groovy
publishing {
    publications {
        aar(MavenPublication) {
            groupId 'io.github.faob-dev'
            artifactId archiveName
            version libVersion

            artifact(sourcesJar)
            artifact(javadocJar)
            artifact("$buildDir/outputs/aar/$archiveName-release.aar")
        }
    }
}
```

#### 4. Adding info in POM file

Maven central requires some additional information to be added in POM file which includes name, description, url, license, scm and developer. For more information please read POM example at the end in maven central  [Requirements](https://central.sonatype.org/pages/requirements.html#a-complete-example-pom). Add additional POM information in publishing  task

```groovy
publishing {
    publications {
        aar(MavenPublication) {
            .........
            .........
            .........
            pom.withXml {
                def root = asNode()
                root.children().last() + {
                    resolveStrategy = Closure.DELEGATE_FIRST
                    name 'AAR'
                    description 'Android aar library'
                    url 'https://github.com/faob-dev/Tutorials'
                    licenses {
                        license {
                            name 'The MIT License (MIT)'
                            url 'https://opensource.org/licenses/MIT'
                        }
                    }
                    scm {
                        url 'https://github.com/faob-dev/Tutorials'
                    }
                    developers {
                        developer {
                            name 'FaoB'
                        }
                    }
                }
            }
        }
    }
}
```

#### 5. Sign artifacts using GnuPG
One of the advantages of [https://bintray.com](https://bintray.com) is that when we pushed our artifacts it does signing automatically. In section we will create GPG key pair and upload it to [https://bintray.com](https://bintray.com) and then we will enable automatic signing option to sign our artifacts automatically when we pushed our artifacts.

##### 5.1. Creating GPG Key Pair
In this section we will create GPG key pair to sign our artifacts. Download and install [GnuPG](https://gnupg.org/download/index.html) then open terminal and type this command to create new key pair

`gpg --gen-key`

Enter Name, Email and add some Passphrase and click on Ok to create key pair

![pic2.png](https://ipfs.busy.org/ipfs/QmaHwHbmGGk4qhwZC8mCZRbMtNdmGShEpbV8AhM95T1CMv)

After successful process key pair will be generated

![pic3.png](https://ipfs.busy.org/ipfs/QmfSVok6FhoR6GypGTRsZVxfAF13SqafF3J1tKndXqGYTX)

##### 5.2. Exporting public and private key
To export public and private key first we need KeyId to get keyId type this command

`gpg --list-keys`

Last eight characters of Fingerprint is KeyId see highlighted characters in screenshot my KeyId is **50C8FBFB**

![4.png](https://ipfs.busy.org/ipfs/QmRAcgUNJU8VhJYu8D6YbkKTpWQQFWoMxNwxeyZvSA7nX3)

To export public key is ASCII format type this command value after **--export** is KeyId and last parameter is file name where you want to save your key

`gpg --armor --export 50C8FBFB > pubkey.gpg`

To export private key is ASCII format type this command value after **--export-secret-keys** is KeyId and last parameter is file name where you want to save your key

`gpg --armor --export-secret-keys 50C8FBFB > seckey.gpg`

##### 5.3. Uploading keys to bintray
Login to [https://bintray.com](https://bintray.com) and click on option menu and click Edit Profile

![pic5.png](https://ipfs.busy.org/ipfs/QmVkUtQPPsqZpF69NNopZL79X3Tvb1DZBVE7V4LZNGJGiX)

Click on GPG Signing on left side menu

![pic6.png](https://ipfs.busy.org/ipfs/QmPUDjeZGMsE9XuoLTLxmhFoTKPedPds4ToCE3Xus7GMtw)

Open keys(generated in **Exporting public and private key** section) **pubkey.gpg** and **seckey.gpg** in your favorite text editor copy and paste pubkey in Public Key and seckey in Private Key in GPG Signing page and click Update to upload keys

![pic7.png](https://ipfs.busy.org/ipfs/QmR7qX2MnTtzUQomdMZMkFUhHuYTWD2hL8Xc38W8ZjD9sn)

##### 5.4 Automatic signing artifacts
To enable automatic signing option open your repository where you want to push your artifacts and click on Edit

![pic8.png](https://ipfs.busy.org/ipfs/QmNVDdJN63nG7qmjL7dPjofH9bnMjZK5WGjqpEReLQKZrQ)

Scroll down and enable option **GPG sign uploaded files using Bintray's public/private key pair** and click on Update button to enable that option

![pic9.png](https://ipfs.busy.org/ipfs/QmXdP6h3AbgBL5u1vqBrP8St8uPH6Eupycc9vADmFxa6LH)

##### 5.5. Uploading artifacts
Open Gradle tool window in android studio and double click on 

**app > Tasks > build > build** to create all artifacts
**app > Tasks > publishing > generatePomFileForAarPublication** to create POM file
**app > Tasks > publishing > bintrayUpload** to push all artifacts to bintray

![pic15.png](https://ipfs.busy.org/ipfs/QmXK4cG54C12zXzuxwLE7p81rc6cAFpnL7L5spXA9NbV6g)

After successful upload you can view files in bintray

![pic16.png](https://ipfs.busy.org/ipfs/QmNhkP1Et1y9pepBZ7SBHyA7V3R1QPc7jhptFciAx1maTE)

As you can see all artifacts has been uploaded and all those artifacts are automatically signed with **.asc** extension.

### Linking to Maven Central
To link our jcenter library to maven central we need Sonatype OSS credentails(token key, token pass) to get these credentials login with your sonatype account that we created in **Creating maven jira ticket** section [https://oss.sonatype.org](https://oss.sonatype.org). Click on option menu in top right corner and click Profile

![pic10.png](https://ipfs.busy.org/ipfs/QmQQeE6iNLdgw2ij27vtzmNPDPfYNDupZARmfPxm1G6qed)

Select User Token from choice box

![pic11.png](https://ipfs.busy.org/ipfs/QmYkq1eeo3jA583qDduV9qr3GAfviSm5BFqiVr4D7VGUHg)

Click on Access User Token button and copy token key and token password

![pic12.png](https://ipfs.busy.org/ipfs/QmWBH3yXkkerqZimVM7mVFcc6LKDVnVyYmK5CxRfZgfeCq)

To link our jcenter library to maven central, open your repository on [https://bintray.com](https://bintray.com) and click on Maven Central tab add tokey key and token password and click on Sync button to pushed library to maven central. After successful operation you will see Sync success message on right side

![pic17.png](https://ipfs.busy.org/ipfs/QmVYyTmQPSVntRBQUqPUhY1BVDNDqv8x6f9MBFfxPryenp)

After successful maven central sync we can see our library in this repository

![pic18.png](https://ipfs.busy.org/ipfs/QmecbwCg7GGwwZaexTwXqJZJXp2TgePMYhZ4WwWBPYRCbG)

### Importing library from Maven Central

importing library from maven central is simple just add this one line to dependencies in app **build.gradle** file. Note dependency ends with @aar since it is aar library

```groovy
dependencies {
    implementation 'io.github.faob-dev:aar:1.0.0@aar'
    ...
    ...
}
```

### Github

Complete project available on [github](https://github.com/faob-dev/Tutorials). Clone repo and open project name **AAR** just change **build.gradle** file here is  complete file

```groovy
plugins {
    id "com.jfrog.bintray" version "1.7.3"
}

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

def archiveName = 'aar'
def libVersion = '1.0.0'

android {
    compileSdkVersion 27
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 27
        archivesBaseName = archiveName
        versionCode 1
        versionName = libVersion
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

task sourcesJar(type: Jar) {
    classifier "sources"
    from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
}

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

artifacts {
    archives javadocJar
    archives sourcesJar
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")
    pkg {
        repo = 'foo'
        name = 'libs'
        licenses = ['MIT']
        websiteUrl = 'https://github.com/faob-dev/Tutorials'
        issueTrackerUrl = 'https://github.com/faob-dev/Tutorials/issues'
        vcsUrl = 'https://github.com/faob-dev/Tutorials.git'
        version {
            name = libVersion
            vcsTag = libVersion
        }
    }
    publications = ['aar']
}

publishing {
    publications {
        aar(MavenPublication) {
            groupId 'io.github.faob-dev'
            artifactId archiveName
            version libVersion

            artifact(sourcesJar)
            artifact(javadocJar)
            artifact("$buildDir/outputs/aar/$archiveName-release.aar")

            pom.withXml {
                def root = asNode()
                root.children().last() + {
                    resolveStrategy = Closure.DELEGATE_FIRST
                    name 'AAR'
                    description 'Android aar library'
                    url 'https://github.com/faob-dev/Tutorials'
                    licenses {
                        license {
                            name 'The MIT License (MIT)'
                            url 'https://opensource.org/licenses/MIT'
                        }
                    }
                    scm {
                        url 'https://github.com/faob-dev/Tutorials'
                    }
                    developers {
                        developer {
                            name 'FaoB'
                        }
                    }
                }
            }
        }
    }
}
```
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 152 others
properties (23)
authorkabooom
permlinklinking-jcenter-android-archive-aar-to-maven-central-part-2
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.0","format":"markdown","tags":["utopian-io","tutorials","steemdev","android","java"],"users":["kabooom","github.com","aar"],"links":["https://github.com/aosp-mirror","https://busy.org/@kabooom/creating-android-archive-aar-publishing-to-jcenter-and-using-it-in-android-project","https://busy.org/@kabooom/creating-android-archive-aar-publishing-to-jcenter-and-using-it-in-android-project","https://bintray.com","https://central.sonatype.org/pages/requirements.html#a-complete-example-pom","https://busy.org/@kabooom/creating-android-archive-aar-publishing-to-jcenter-and-using-it-in-android-project","https://issues.sonatype.org","https://bintray.com","https://busy.org/@kabooom/creating-android-archive-aar-publishing-to-jcenter-and-using-it-in-android-project","https://bintray.com"],"image":["https://ipfs.busy.org/ipfs/QmZLEegMLU7zLZ8viV6q36FdmArkDk21CvJFuhVocKmQ5n","https://ipfs.busy.org/ipfs/QmYjhmp3krPzjPWsDLGkg4mQWjJV72qHLpcvV2QYjfw7dF","https://steemitimages.com/DQmNPVNx4nz5QUoMp2RNBYLwF7TK5jkJCpLBosQT9NL15Zg/p20.png","https://ipfs.busy.org/ipfs/QmaHwHbmGGk4qhwZC8mCZRbMtNdmGShEpbV8AhM95T1CMv","https://ipfs.busy.org/ipfs/QmfSVok6FhoR6GypGTRsZVxfAF13SqafF3J1tKndXqGYTX","https://ipfs.busy.org/ipfs/QmRAcgUNJU8VhJYu8D6YbkKTpWQQFWoMxNwxeyZvSA7nX3","https://ipfs.busy.org/ipfs/QmVkUtQPPsqZpF69NNopZL79X3Tvb1DZBVE7V4LZNGJGiX","https://ipfs.busy.org/ipfs/QmPUDjeZGMsE9XuoLTLxmhFoTKPedPds4ToCE3Xus7GMtw","https://ipfs.busy.org/ipfs/QmR7qX2MnTtzUQomdMZMkFUhHuYTWD2hL8Xc38W8ZjD9sn","https://ipfs.busy.org/ipfs/QmNVDdJN63nG7qmjL7dPjofH9bnMjZK5WGjqpEReLQKZrQ","https://ipfs.busy.org/ipfs/QmXdP6h3AbgBL5u1vqBrP8St8uPH6Eupycc9vADmFxa6LH","https://ipfs.busy.org/ipfs/QmXK4cG54C12zXzuxwLE7p81rc6cAFpnL7L5spXA9NbV6g","https://ipfs.busy.org/ipfs/QmNhkP1Et1y9pepBZ7SBHyA7V3R1QPc7jhptFciAx1maTE","https://ipfs.busy.org/ipfs/QmQQeE6iNLdgw2ij27vtzmNPDPfYNDupZARmfPxm1G6qed","https://ipfs.busy.org/ipfs/QmYkq1eeo3jA583qDduV9qr3GAfviSm5BFqiVr4D7VGUHg","https://ipfs.busy.org/ipfs/QmWBH3yXkkerqZimVM7mVFcc6LKDVnVyYmK5CxRfZgfeCq","https://ipfs.busy.org/ipfs/QmVYyTmQPSVntRBQUqPUhY1BVDNDqv8x6f9MBFfxPryenp","https://ipfs.busy.org/ipfs/QmecbwCg7GGwwZaexTwXqJZJXp2TgePMYhZ4WwWBPYRCbG"]}
created2018-06-28 19:48:30
last_update2018-07-04 08:32:54
depth0
children3
last_payout2018-07-05 19:48:30
cashout_time1969-12-31 23:59:59
total_payout_value60.701 HBD
curator_payout_value19.311 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length15,589
author_reputation3,986,929,587,227
root_title"Linking jcenter Android Archive (AAR) library to Maven Central Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id62,656,111
net_rshares32,435,057,020,079
author_curate_reward""
vote details (216)
@codingdefined ·
Thank you for your contribution. Its good to see tutorial like this, but you should also explain what is Maven Central and how it will benefit in the long run if you use it. 

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/21211313).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
properties (22)
authorcodingdefined
permlinkre-kabooom-linking-jcenter-android-archive-aar-to-maven-central-part-2-20180629t120652515z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/21211313","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-06-29 12:06:54
last_update2018-06-29 12:06:54
depth1
children1
last_payout2018-07-06 12:06:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length666
author_reputation532,125,750,373,424
root_title"Linking jcenter Android Archive (AAR) library to Maven Central Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id62,734,535
net_rshares0
@kabooom ·
I am currently working on third tutorial on jitpack repository and will compare jcenter vs maven central vs jitpack. Can you tell me is this post approved or not? or i should explain pros and cons of maven central in this tutorial?
properties (22)
authorkabooom
permlinkre-codingdefined-re-kabooom-linking-jcenter-android-archive-aar-to-maven-central-part-2-20180629t140747470z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"busy","app":"busy/2.4.0"}
created2018-06-29 14:07:54
last_update2018-06-29 14:07:54
depth2
children0
last_payout2018-07-06 14:07:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length231
author_reputation3,986,929,587,227
root_title"Linking jcenter Android Archive (AAR) library to Maven Central Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id62,747,203
net_rshares0
@utopian-io ·
Hey @kabooom
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-linking-jcenter-android-archive-aar-to-maven-central-part-2-20180630t030508z
categoryutopian-io
json_metadata"{"app": "beem/0.19.29"}"
created2018-06-30 03:05:09
last_update2018-06-30 03:05:09
depth1
children0
last_payout2018-07-07 03:05:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length299
author_reputation152,955,367,999,756
root_title"Linking jcenter Android Archive (AAR) library to Maven Central Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id62,814,975
net_rshares0