Publish a multi module Android library to Maven (GitHub Packages)

24 Juil

First thing first, all credits to for his post about Publishing Multi Module Android Libraries, so my post will be based on his work but with more details ( steps ) for newbies like me who struggle to publish their multi-module android library, as you know publishing to maven isn’t something easy you do as clicking a button, also I’ll trait an issue I got when following his tutorial and how I solved it.

We’ll publish to Maven Local and also to Github Packages, so let’s start!

The Setup

Open Android Studio and create a new project, choose an empty activity to keep things easy for us

I named my project PublishLibraryTuto

Now we will create for this tutorial two android libraries and a java library

Go to File -> New -> Now Module

We’ll call our first android library my-android-lib-1

And do the same for the second android library and call it my-android-lib-2.

Now we’ll create our Java library and call it my-java-library

Our project should looks like that

We’ll make our app module depends on the others lib, so go to app’s build.gradle and add the following lines inside your dependencies block and Sync Project with Gradle Files

In your app module, you can see that now you have access to other modules’ class

The Publishing logic

So we want to be able to publish all our libraries at once and without duplicating the publication’s logic inside our modules, we can do that by putting our publish logic inside the project build.gradle

Open your build.gradle file and go to your allprojects block and add set the group and version variables, by doing that we will have the same group and version for all modules.

Now, just below this block code, we add subprojects block with the following code.

Basically what’s happening here, is we’re checking if our module is not an application module, to avoid publishing the app module

For publishing java modules

And this part is for android modules.

If you Gradle sync, you’ll get the following error

 

Even if we’re using afterEvaluate, it seems like it doesn’t recognize the release property.

The workaround I found and I post it on StackOverflow is to add another afterEvaluate after the maven block, so our code we’ll look like this now :

Try to Gradle Sync now, you will have a successful build.

Publish To MavenLocal

In your Android Studio, click on Gradle -> Tasks -> publishing -> publishToMavenLocal

If you can’t find the Gradle Tasks, go to Android Studio Settings -> Experimental and uncheck “Do not build Gradle list during Gradle sync

After publishing, let’s check our local repository, it’s located at $USER/.m2/repository 

And our library will be in $USER/.m2/repository/com/appfiza/android 

Publish to Github Packages

To publish to Github Packages we need to specify the repository URL and our credentials, we update our subprojects code like below :

You can check how to generate your Github access token here

Now rather than selecting publishToMavenLocal, select publish, if everything is ok, you’ll get a successful message from Gradle, but go to your GitHub repository and check if it’s been published

My repository GitHub repo packages

How to retrieve our libraries for another project?

Create another fresh new project, and go to your project’s build.gradle and inside allprojects block in repositories add the following code

And in your app’s build.gradle add this to get your libraries

And you can start using them

 

That’s it, here is the Github repository if you need the code.

Hope my tutorial helped you, if you have any question, just leave a comment! 😃

One Reply to “Publish a multi module Android library to Maven (GitHub Packages)”

  1. Pingback: How to get a react native module in native via Maven - App Fiza

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *