In the previous article, we learned how to create an Android Library. In this article, we will learn how to Publish an Android Library.
Benefits of Publishing an Android Library:
There are several places where you can publish an Android library:
All the above options can host your public and private repositories. Hosting public repositories is free but private repositories are not.
To publish an Android library we will be using GIT and Github. There is already an article and video about the same which you can go through before further reading. This will help you to understand the commands and process better.
Let’s continue where we left off from the previous article.
1. Open the build.gradle.kts of networkmodule and make the following changes:
Add the Maven plugin, to publish build artifacts to the Maven repository.
1 |
id("maven-publish") |
Sync the project and then add the code below to declare your library module publication.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
... ... publishing { publications { register<MavenPublication>("release") { afterEvaluate { from(components["release"]) } } } } ... ... |
Full build.gradle.kts file after the above changes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
plugins { id("com.android.library") id("org.jetbrains.kotlin.android") id("maven-publish") } android { namespace = "com.androhub.networkmodule" compileSdk = 34 defaultConfig { minSdk = 24 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles("consumer-rules.pro") } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } } publishing { publications { register<MavenPublication>("release") { afterEvaluate { from(components["release"]) } } } } dependencies { implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.9.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") // retrofit dependencies implementation("com.squareup.retrofit2:retrofit:2.9.0") implementation ("com.squareup.retrofit2:converter-gson:2.7.2") implementation ("com.squareup.okhttp3:okhttp:3.10.0") } |
2. We will be using Jitpack for publishing our library as it is easy to use. To make the build at Jitpack we have to create a new file named jitpack.yml under the root directory and add the following code:
1 2 3 4 5 |
jdk: - openjdk17 before_install: - sdk install java 17.0.7-open - sdk use java 17.0.7-open |
We required this file as the project is built on Java 17 but Jitpack works on Java 8. So to customize the setup we have to do this extra step. To learn more about this click here.
Note: If your project is configured to run on Java 8 then you can skip the above step.
3. Now commit your changes and push the code to your GitHub repository. Click here to learn how to push code to GitHub.
4. Open Jitpack site and enter your GitHub repository link in the input box and click on the Look up button.
By doing this Jitpack will find the releases created in the repository. By default, it will pick the following releases:
5. To provide the stable release artifact. First, we have to create Git Tags. To do that click on File => Git => New tag and this will open the below window where you have to enter the Tag Name and click on the Create Tag button.
6. After creating the Tag, we have to push the tag. To do that click on the branches dropdown and click on the Push option. This will open the below window where you have to check if the Push tags option is checked or not. If not then check it and push the code.
7. Go to the GitHub repository and refresh the page to see if Tags have been pushed successfully or not.
Repository before pushing Tag.
Repository after pushing Tag. Here you can see that the Tag count has increased to 1.
8. Creating a Tag is not sufficient for Jitpack to Look Up for the releases. We have to create a new release from GitHub.
Click on Create a new release button.
9. Creating a new release will open a new screen where we have to select Tag and Branch from where the release has to be made. Then enter the release info like title and description which will represent what exactly this release contains.
After entering all the information click on the Publish Release button which is at the bottom of the screen when you scroll.
On clicking Publish Release, a new screen will open which shows details about the created release.
10. Let’s open the Jitpack again and click on the Look Up button. This time it will list all your created releases from the Tags.
Now, Jitpack will start building the artifact of the library module. The process goes like this:
Now we will learn how we can use the published library from Jitpack in our projects. Open any of your projects where you want to use your published library.
1. Open settings.gradle.kts file and add the Maven Jitpack URL to fetch the artifact.
1 |
maven { url = uri("https://jitpack.io") } |
Full settings.gradle.kts file after adding Maven:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url = uri("https://jitpack.io") } } } rootProject.name = "LibraryModuleExample" include(":app") |
2. Now open app-level build.gradle.kts file and add the following dependency and sync the project.
1 |
implementation ("com.github.sonusurender:LibraryModuleDemo:1.0.8") |
For your library, the dependency will be like this:
1 |
implementation ("com.github.<user_name>:<repository_name>:<tag_version>") |
3. To upgrade the library version just commit and push the updated code to the repository and follow the steps of creating tags and creating a new release.
Happy Coding.
Subscribe to us and get the latest news.