Skip to content

Instantly share code, notes, and snippets.

@lopspower
Last active January 20, 2024 09:18
Show Gist options
  • Save lopspower/6f62fe1492726d848d6d to your computer and use it in GitHub Desktop.
Save lopspower/6f62fe1492726d848d6d to your computer and use it in GitHub Desktop.
Publish AAR to jCenter and Maven Central

Publish AAR to jCenter and Maven Central

Twitter

EDIT: You can find this same updated tutorial here -> Medium

Now I'm going to list how to publish an Android libray to jCenter and then syncronize it with Maven Central:

  1. I use "Android Studio" and I have this simple android lib that I would like to be available on maven: CircularImageView

  2. In the library folder(module) I have the lib code abovementioned. And applying in the build.gradle of this folder apply plugin: 'com.android.library' I got as output an .aar in the build/outputs/aar/ directory of the module's directory

Warning: The module name is very important. So I renamed my module "CircularImageView-Library" in "circularimageview". Indeed, this name is your artifactId. (Previous probleme)

  1. Register to Sonatype, I registered with username lopspower

  2. In the Sonatype OSS Repository I registered a project opening a new Issue:
    Create → Create Issue → Community Support - Open Source Project Repository Hosting → New Project → with groupid com.github.lopspower
    Remember that "only one JIRA issue per top-level groupId is necessary. You have all the necessary permissions to deploy any new artifacts to this groupId or any sub-groups".

  3. Register to Bintray with a username, the one used by me is: lopspower

  4. Enable the automatically signing of the uploaded content:
    from Bintray profile url → GPG Signing → copy paste your gpg armored public and private keys.

UNIX/MAC: You can find respectively these two keys in files public_key_sender.asc and private_key_sender.asc if you execute the following code (the -a or --armor option in gpgis used to generate ASCII-armored key pair):

gpg --gen-key    # generates the key pair
gpg --list-keys  # get your PubKeyId (this value is used in the line below)
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys PubKeyId  # publish your Key
gpg -a --export [email protected] > public_key_sender.asc
gpg -a --export-secret-key [email protected] > private_key_sender.asc

WINDOWS: Generated your gpg key with http://www.gpg4win.org/

  1. In the same web page you can configure the auto-signing from:
    Repositories → Maven → Check the "GPG Sign uploaded files automatically" → Update

  2. In the same web page you can find your Bintray API Key (simply copy it for a later use)

  3. In the same web page you can configure your Sonatype OSS User (created at previous step 3) from the Account link

  4. Add these two lines to the build.gradle in the root of your project

classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1'

So that your build.gradle in the root looks like this:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}
  1. Modify the build.gradle located inside the library folder.
    I modified mine from this:
apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 4
        versionName "2.0.1"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

To this one:

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: "com.jfrog.bintray"

// This is the library version used when deploying the artifact
version = "2.0.1"

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 4
        versionName version
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

def siteUrl = 'https://github.com/lopspower/CircularImageView'      // Homepage URL of the library
def gitUrl = 'https://github.com/lopspower/CircularImageView.git'   // Git repository URL
group = "com.mikhaellopez"                                          // Maven Group ID for the artifact

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'

                // Add your description here
                name 'com.mikhaellopez:circularimageview' // TODO 
                description = 'Description of your project HERE' // TODO
                url siteUrl

                // Set your license
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'lopspower' // TODO
                        name 'Mikhael Lopez' // TODO
                        email '[email protected]' // TODO
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}

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

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

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())

// https://github.com/bintray/gradle-bintray-plugin
bintray {
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")

    configurations = ['archives']
    pkg {
        repo = "maven"
        // it is the name that appears in bintray when logged
        name = "com.mikhaellopez:circularimageview" // TODO
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
        version {
          gpg {
              sign = true //Determines whether to GPG sign the files. The default is false
              passphrase = properties.getProperty("bintray.gpg.password") //Optional. The passphrase for GPG signing'
          }
      }        
    }
}
  1. Add to the local.properties in the root of the project the following lines (remember that this file should never be uploaded on your public repository):
bintray.user=<your bintray username>
bintray.apikey=<your bintray api key>
  
bintray.gpg.password=<your gpg signing password>
bintray.oss.user=<your sonatype username>
bintray.oss.password=<your sonatype password>
  1. Open "Android Studio" terminal and execute:
gradlew bintrayUpload

Warning : If you don't have permission to execute gradlew : $ chmod +x gradlew

  1. From Bintray → My Recent Packages → com.mikhaellopez:circularimageview (this is here only after the execution of the previous point 13) → Add to Jcenter → Check the box → Group Id = "com.mikhaellopez".
    This will request a review and public listing of your library on jCenter repository. This request can be quite long. You will receive a confirmation email but you can check jcenter.bintray.com to see if your library is syncronise.

Now you can import your library with jcenter like that :

build.gradle (Project):

repositories {
  jcenter()
}

build.gradle (Module):

dependencies {
  compile 'com.mikhaellopez:circularimageview:2.0.1'
}

If you use the default of Android Studio, jcenter() you could simply import like this (for me it doesn't work without the exactly version, i.e. with the plus gives me an error). To do that you need to sync with Maven Central (Next Step)

dependencies {
  compile 'com.mikhaellopez:circularimageview:+'
}
  1. Finally Sync all with Maven Central following: Bintray → My Recent Packages → com.mikhaellopez:circularimageview → Maven Central → Sync. You can check repo.maven.apache.org/maven2/ to see if your library is syncronise.

Warning : It isn't clear but the "User token password" actually corresponds to the password of your Sonatype account.

  1. Now your library should be automatically imported both from Maven Central and from Bintray, you can import with something like this:

build.gradle (Project):

repositories {
  mavenCentral()
}

build.gradle (Module):

dependencies {
  compile 'com.mikhaellopez:circularimageview:+'
}

📚 Best Android Gists

You can see other best Android Gists or offer your just here https://github.com/lopspower/BestAndroidGists 👍.

@macropok
Copy link

same problem. gradlew bintrayUpload success and upload all files successfully. But there is no button to click "Add to JCenter".

@SpinyTech
Copy link

If you can not find "Add to JCenter" button, delete the trail account and register the OSS account.@boybeak @macropok
Here is the link https://bintray.com/signup/oss

@Jyotivijapur
Copy link

Hii Sir, step 15.Bintray → My Recent Packages → com.mikhaellopez:circularimageview → Maven Central → Sync i am unable to do that..i have followed all of ur steps but still saying that To be synced to Maven Central your package needs to be included in the JCenter repository. Click here to get it included. how do i resolve it? pls help

@Jyotivijapur
Copy link

can anyone help me please..

finally i linked my library to JCenter.
when i try to synchronise with maven central i am getting following errors.

Last Sync Errors: Failed to promote repository: central_bundles-9438. Server response: * User 'snyxiuslabs' missing 'promote' permission for staging profile: 7edbe315063867 Dropping existing partial staging repository.

how do i resolve this?pls help me

@surajbokankar
Copy link

Execution failed for task ':app:bintrayUpload'.
Could not create package 'surajbokankar/sso/com.sso': HTTP/1.1 404 Not Found
After point 13.
Can you help me out.

@bpappin
Copy link

bpappin commented Nov 18, 2017

This appears to be out of date relative to the android cradle plugin 3.0.0.

@tomasbjerre
Copy link

Would be nice to have point 14 updated. I dont see "Add to Jcenter" anywhere.

@jasonlamkk
Copy link

i am using

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
    classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0"
}

got error
Error:(64, 0) No signature of method: java.lang.String.call() is applicable for argument types: (build_3zv5heifneaqzodu6o8sk8kps$_run_closure2$_closure13$_closure14$_closure15$_closure17$_closure20) values: [build_3zv5heifneaqzodu6o8sk8kps$_run_closure2$_closure13$_closure14$_closure15$_closure17$_closure20@2b840f53]
Possible solutions: wait(), any(), wait(long), take(int), each(groovy.lang.Closure), any(groovy.lang.Closure)

which point to the line

            developers{
                developer{    //<<<line 64

already delete the spacings and auto indent a few time to make sure no special characters;
don't know what cause the error

if ignore the developers.developer section can build successfully

gradlew bintrayUpload
BUILD SUCCESSFUL in 5s
29 actionable tasks: 12 executed, 17 up-to-date

yet no file uploaded to bintray

@jasonlamkk
Copy link

jasonlamkk commented Dec 9, 2017

(continue ... )
the project is available at
https://github.com/jasonlamkk/AndroidColorfulParticleSystem

suggestions or directly contribute and fix via github is very welcomed

Thanks a lot!

@ND1010
Copy link

ND1010 commented Sep 26, 2019

A problem occurred evaluating root project 'epmoneylib'.

Could not find method install() for arguments [build_67s7lwhi33yl8gxelkx9ca5fc$_run_closure5@2681c50d] on root project 'epmoneylib' of type org.gradle.api.Project.

Getting this issue while using install {} attribute

@Shchvova
Copy link

This is outdated. com.github.dcendents:android-maven-gradle-plugin is abandoned

@daniele-pecora
Copy link

that is it

@julian-alarcon
Copy link

Maven Central will stop supporting com.github.* from April 2021, the alternative is to use io.github.*
https://central.sonatype.org/changelog/#2021-04-01-comgithub-is-not-supported-anymore-as-a-valid-coordinate

@spyhunter99
Copy link

this post is very old, the content no longer works with the latest gradle/android stuff (currently both in the v7.x range)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment