Creating a Private Maven Repository for Android Libraries on GitLab
Creating open-source Android libraries benefits for both library owner and the other developers. A code written by a developer can save other developers from building the same thing, and also the other developers can contribute the same code. Thus, a library that will be useful for many people is can be created with the contribution of a few people.
However, you may not want to open some of your personal or corporate libraries to the outside world. In this case, you can create your own packages using one of GitHub, GitLab, AWS, GCP etc. services.
Reusability and Versioning
When you have multiple projects that use shared components, implementing a single main code for these projects instead of using the same code in all projects will benefit you in many ways. It is important to make bug fix or code change in a single library and create a new version, both in terms of spending less time and ensuring consistent build quality.
Maven Publish Plugin
As described in the official documentation, Android Gradle plugin 3.6.0 and higher include support for the Maven Publish Gradle plugin, which allows you to publish build artifacts to an Apache Maven repository.
First, we add the maven-publish
plugin to the project level build.gradle
file.
In order to publish our plugin, we need to create a Gradle file that can be use in our library configuration. Create a file called gradle-mvn-push.gradle
in the root of the project, which will apply the Gradle-Maven plugin and specify the location of the GitLab package when using the gradle publish
command:
In the publications section, we used our build types as publication name like release, debug. If you have additional flavor types in your project you can create other publications for each flavor name combined with build types.(e.g. paidRelease
, paidDebug
)
In the repositories section, we describe our repository locations to publish. In the example, we used GitLab Package Registry address. You can also use the other services.
The private token used in credentials is our personal GitLab Access Token. You can create it from here with api
, read_registry
and write_registry
scopes.
Finally, we apply the gradle-mvn-push.gradle
file that we created in the libraries gradle file (i.e. mylibrary/build.gradle
):
With these steps, we are ready to create our first package. First, we need to build our library to create AAR (Android Archive) files. Locate to your library module directory and run gradle build
command.
If you look at library/build/outputs/aar
you will see the built aar files for each build variant. Finally, we run gradle publish
command.
Thus, we have published our first package. If we can see the package we published in the GitLab Package Registry, then there is no problem.
Installing Published Libraries
At this step, we don’t need to do anything other than implementing any gradle library into the project. We just specify the gitlab credentials while adding the maven repository to the project level gradle file.
Finally, we need to implement the package into module level gradle file and sync gradle.