Android library to Maven Central overview

preface

Recently, I heard that jCenter was going to close, so I was in a panic. I thought about migrating to another library for a long time, but I couldn’t find a reliable one. Finally, I found Maven Central, which has the same name as jCenter, but the application and configuration of Maven Central are much more difficult than bintray in jForg. How to register and upload libraries to Maven Central became very difficult. I searched online for the introduction of Java libraries. I had no choice but to search for information on Google and read the official documents. This article provides a detailed overview of how to publish Android libraries to Maven Central. This article is published in the big Talk Android small column, prohibit all unauthorized reprint behavior.

Step 1: Register

Just as jCenter is run by jForg and managed at bintray.com, Maven Central is run by Sonatype, so you first need to register a Sonatype account and get permission to use the repository.

To register your account, go to Issues.sonatype.org and click on the red box:

To register, you need to fill in your email address (important, please fill in your real email address), first name, user name (important, for login), password (complex password with symbols, English and numbers), and verification code.

Note: Sonatype.org actually supports Chinese and will prompt you to switch languages after logging in. It’s very friendly.

If you do not have your own website domain name, it is generally recommended to fill in Github. For example, if your Github is github.com/kongzue, it can be written as com.github. Kongzue. If you have your own domain name, you can flip it according to your domain name as your main address. In addition, note that this application actually only needs to be applied once, so it is suggested to directly set the Group Id to the main package name, without specific project. For example, my projects all start with com.kongzue. So don’t specify com.kongzue. Dialogv3 here, because all your projects can be put into maven repository at the beginning of com.kongzue.

Additional it is important to note that if you use your own domain name, need in accordance with the requirements of the state, in the DNS configuration configuration a TXT record your lot address to complete the verification, the more troublesome, specific reference: central.sonatype.org/pages/produ…

Please wait patiently for the official reply after the application. When the issues status becomes resolved, the application will be successful. If there is any failure, the official will provide comments below, you can also reply to the official comment to complete the application.

Step 2 ·Gradle preparation

Next, you need to enter your project and complete a series of configurations. Please first refer to download the gradle file below and place it in the root directory of your project.

Documents: the publish – mavencentral. Gradle

apply plugin: 'maven-publish' apply plugin: 'signing' task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.source } ext["signing.keyId"] = '' ext["signing.password"] = '' ext["signing.secretKeyRingFile"] = '' ext["ossrhUsername"] = '' ext["ossrhPassword"] = '' File secretPropsFile = project.rootProject.file('local.properties') if (secretPropsFile.exists()) { println "Found secret props file, loading props" Properties p = new Properties() p.load(new FileInputStream(secretPropsFile)) p.each { name, value -> ext[name] = value } } else { println "No props file, loading env vars" } publishing { publications { release(MavenPublication) { // The coordinates of the library, being set from variables that // we'll set up in a moment groupId PUBLISH_GROUP_ID artifactId PUBLISH_ARTIFACT_ID version PUBLISH_VERSION // Two artifacts, the `aar` and the sources artifact("$buildDir/outputs/aar/${project.getName()}-release.aar") artifact androidSourcesJar // Self-explanatory metadata for the most part pom {name = PUBLISH_ARTIFACT_ID description = 'your project description' // If your Project has a dedicated site, use its URL here URL = 'Github address 'licenses {license {// Protocol type, generally default Apache License2.0 do not change:  name = 'The Apache License, Version 2.0 developers' url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'}} {developer {id = 'user id' name = // Version control info, if you're using GitHub, Follow the format as seen here SCM {// change to your Git address: = 'scm:git:ssh://github.com/xxx/xxxx.git' = 'scm:git:github.com/xxx/xxxx.git' connection developerConnection / / branch address:  url = 'https://github.com/xxx/xxxx/tree/master' } // A slightly hacky fix so that your POM will include any transitive dependencies // that your library builds upon withXml { def dependenciesNode = asNode().appendNode('dependencies') project.configurations.implementation.allDependencies.each { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) dependencyNode.appendNode('version', it.version) } } } } } repositories { // The repository to publish to, Sonatype/MavenCentral maven { // This is an arbitrary name, Descriptive: You may also use "Mavencentral" or // any other name that's descriptive for you name = "project name" def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/" // You only need this if you want to publish snapshots, otherwise just set the URL // to the release repo directly url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl // The username and password we've fetched earlier credentials { username ossrhUsername password ossrhPassword } } } } signing { sign publishing.publications }Copy the code

Once done, go to your local.properties file and add the following:

Signing. KeyId = XXXXXXXX signing. The password = password signing. SecretKeyRingFile = D \ : / key/GPG signature secret key file. GPG ossrhUsername = sonatype account OssrhPassword = sonatype passwordCopy the code

Step 3: Signature key application

Here you need a GPG key, you need to download the GPG manager client to complete the application:

www.gpg4win.org/get-gpg4win…

This software supports Chinese, please feel free to use it, as shown in the picture below. Click not to donate, click Download to install.

After the installation is complete, click the main interface menu to create a new key

Click on the first option to create a personal key:

Select Protect the generated key with a passphrase and select Protect the generated key with a passphrase. Select Protect the generated key with a passphrase.

Next, set the password, finish, in the finish interface first do not rush to upload or backup, directly click finish.

Double-click the key you created on the home screen to see the following screen

Please remember the 8-digit letters circled in red below and copy them for later use. Click the Generate Revocation button to save your revocation certificate, then go back to the main screen and right click to publish in the server:

Right click Export again, rename the export file suffix, and export the file to GPG format.

Go back to Android Studio, go to the local.properties file and edit the file:

Signing the keyId = 02 c4ac4b signing. Password = your secret key code signing. SecretKeyRingFile = D \ : / key/GPG signature secret key file. GPG OssrhUsername = Sonatype Account ossrhPassword= Sonatype passwordCopy the code

Finish editing.

Step 4 · Upload

Go to build.gradle in your Module and add the following code:

Ext {PUBLISH_GROUP_ID = "com.xxx. XXXX "// project name PUBLISH_ARTIFACT_ID = 'XXXX' // project name PUBLISH_VERSION = 0.0.1 // version number} apply from: "${rootProject.projectDir}/publish-mavencentral.gradle"Copy the code

Compile the release using Gradle and double-click on Module name /Tasks/build/assemble:

Completed, uploaded to the Maven Central, double-click the name of the Module/Tasks/publishing/publicReleasePublicationToXXXRepository

Once done, go to oss.sonatype.org/ and log in using your Sonatype account and password.

Enter the Staging Repositories on the left

Search for your name in the upper right corner and you should be able to find the library files you uploaded. Click on Content below to view the uploaded files in detail:

After confirming everything, click Close, enter introduction and click Confirm to complete all operations.

Next, wait 5-10 minutes until the “Repository Closed” check appears on the Activity TAB, and then click the Release button to publish to MavenCentral. Wait a few hours for the results to be published at search.maven.org/.

The end of the

The above is the full overview of Android library publishing to Maven Central. This article is published in The Big Talk Android small column. All unauthorized reprinting is prohibited. If you have any questions, please add 271127803 to the discussion group.