Monday, August 21, 2023

Publish Android Library AAR to Maven Central


 

In this post we will review the steps required to publish an Android library as an artifact in maven central. This is required when we create an Android library that we want our customers to use, without the need to manually download files, and also allows the customers to enjoy the maven dependency management.


The procedure below includes 3 steps:

1. Create a local maven artifact

2. Create a new project in maven central

3. Manually upload the artifact to maven central


Create A Local Maven Artifact

Open the project in Android Studio, make sure the gradle version is at least 7.1.

This is visible through the File menu, Project Structure, Project (on the right bar).



Now, open the build.gradle under the library folder, and add maven-publish plugin right below the existing android library plugin:



Next, in the same file, right after the android root element, add the publishing:



The full text of the publishing element is below. Notice that I've used my-company and my-library, but feel free to replace with the text that describes the relevant company/library. Notice that this company name must be under your ownership at github.




After updating the gradle, we need to rebuild the library.



And finally, run the publish: under the Android Studio gradle tab, select the library project, then under tasks, publishing, select publishReleasePublicationToMavenLocal.



The built maven artifact is now available in ~/.m2/repository/io/github/my-company/my-library


Create a New Project in Maven Central

1. Create new github user by the company name. For example:

https://github.com/my-company/my-library


2. Create new user in Sonatype: https://issues.sonatype.org/secure/Signup!default.jspa


3. Create ticket to create new project: https://issues.sonatype.org/browse/OSSRH-94213

You must prove ownership on the project ID, so either own the DNS or use GitHub and prove ownership on the GitHub user. I have chosen github. Once the ticket is open, follow the instructions in the ticket to prove ownership.


4. Create gpg keys:

gpg --full-gen-key

# select 1

# select 4096

# select 0

# enter name and email

# comment can be empty

gpg --list-keys

gpg --keyserver keyserver.ubuntu.com --send-keys THE_KEY_ID_SHOWN_IN_THE_LIST_KEYS

gpg --export-secret-keys THE_KEY_ID_SHOWN_IN_THE_LIST_KEYS| base64



Manually Upload The Artifact To Maven Central

Basically this steps can be automated, see steps here:

However, I would not recommend doing this automatically, as it is very complicated process, and not expected to be run many times in most scenarios.

The manual steps are listed here:
But we provide details below, so keep on reading below.


1. Run gpg for each file:

cd ~/.m2/repository/io/github/my-compant/my-library/1.0.0/
rm -f my-library-1.0.0.aar.asc
rm -f my-library-1.0.0.pom.asc
rm -f my-library-1.0.0.module.asc
gpg -ab my-library-1.0.0.aar
gpg -ab my-library-1.0.0.pom
gpg -ab my-library-1.0.0.module
rm -f bundle.jar
jar -cvf bundle.jar *


3. Select “Staging Upload” on the left bar




4. Select Artifact bundle

5. Select the file ~/.m2/repository/io/github/my-company/my-library/1.0.0/bundle.jar and upload


6. Select “Staging Repositories” on the left bar



7. Select the uploaded repository, and click Close button on the top



8. In case of failures, the reasons appears on the bottom under the activity tab:



9. Finally, release the library using the release button


No comments:

Post a Comment