Many tutorials are outdated and incorrect. Thus, I create these most up-to-date guides to publish Android library on JitPack.io.
I was searching for how to publish the Android library on MavenCentral()
and it turned out the process seems very complicated and troublesome. So I found another easy method is to publish my Android library on JitPack.io instead.
Although it is simple, I still spent my whole day to figure that out. It is mainly due to the tutorials out there (even the official documentation) are missing information and not beginner-friendly enough.
So, I'm going to share the step-by-step guides on how to do it and hopefully, this can save you a lot of your time.
1. Create a New Project
- Go to File -> New -> New Project...
- Choose either Empty Activity or Empty Compose Activity, click Next
- Update Name and Save Location
- Click Finish
2. Create a New Module
- Go to File -> New -> New Module...
- Select Android Library, update Module Name and Package Name
- Click Finish
3. Add Code Into Your Module
The module should be created in the root project folder.
- Go to the package, right click, select New -> Kotlin Class/File
- Implement this code as an example
package com.vtsen.sydneysuburbs
object Sydney {
val suburbs = listOf("Ryde", "Chippendale")
}
4. Use the Local Module
In order to use the module that you just created,
- Add
implementation project(':<Module_Name>')
dependency in thebuild.gradle
(app level) file.
Groovy
dependencies {
...
implementation project(':SydneySuburbs')
}
Kotlin
dependencies {
...
implementation(project(":SydneySuburbs"))
}
- Access / use the code that you created in step 3 above. E.g.
Sydney.suburbs[0]
import com.vtsen.sydneysuburbs.Sydney
...
// Example of accessing SydneyBurbs module
Surface(color = MaterialTheme.colors.background) {
Greeting(Sydney.suburbs[0])
}
...
- Run your app, it should work!
5. Setup and Configure for JitPack.io
- Add
maven-publish
plugin inbuild.gradle
file (Android library module level).
Groovy
plugins {
...
id 'maven-publish'
}
Kotlin
plugins {
...
id("maven-publish")
}
Note: There are 3
build.gradle
files - project level, app module level and Android library module level that you just created). Please make sure you update the correctbuild.gradle
file.
- Add
afterEvaluate
at the end of thebuild.gradle
file (Android library module level)
Groovy
publishing {
publications {
release(MavenPublication) {
groupId = 'com.github.vinchamp77'
artifactId = 'demo-simple-android-lib'
version = '0.0.0'
afterEvaluate {
from components.release
}
}
}
}
Kotin
publishing {
publications {
register<MavenPublication>("release") {
groupId = "demo-simple-android-lib"
artifactId = "demo-simple-android-lib"
version = "0.0.3"
afterEvaluate {
from(components["release"])
}
}
}
}
- groupId =
com.github.<Your_GitHub_User_Name>
- artifactId =
'<Your_GitHub_Repository_Name>'
Refer to official Android documentation on how to create the publication.
- Switch to project mode, add the
jitpack.yml
in project root folder
The content in jitpack.yml
:
jdk:
- openjdk11
[Updated - May 27, 2023]: If you upgrade your Android AGP to version 8.0, you will get the following error when JitPack.io is trying to build your library
Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
To fix that, change openjdk11
to openjdk17
in your jitpack.yml file.
jdk:
- openjdk17
[Updated - Oct 15, 2022]: If you get the following warnings, you may want to update the publising options in your build.gradle.
Software Components will not be created automatically for Maven publishing from Android Gradle Plugin 8.0. To opt-in to the future behavior, set the Gradle property android.disableAutomaticComponentCreation=true in the
gradle.properties
file or use the new publishing DSL.
Groovy & Kotlin
android {
...
publishing {
singleVariant("release") {
withSourcesJar()
withJavadocJar()
}
}
...
}
[Updated - May 27, 2023]: If you upgrade your Android Gradle Plugin (AGP) to version 8.0 using Upgrade Assistant in Android Studio, you get a similar message that prevents you from upgrading.
The upgrade step is blocked. To bypass this, you can temporarily add this android.disableAutomaticComponentCreation=true
in your gradle.properties
.
I said temporary because it is merely a workaround for this upgrade assistant bug because once you have upgraded Android AGP, you get the following error, and you want to remove android.disableAutomaticComponentCreation=true
.
The option 'android.disableAutomaticComponentCreation' is deprecated. It was removed in version 8.0 of the Android Gradle plugin. Please remove it from
gradle.properties
.
6. Share Project on GitHub
Now, it is ready to upload your projects to the GitHub repository.
You can also clean up unused dependencies before you upload your project to GitHub. This can help save the build time when JitPack.io
builds your project.
- Follow the detailed steps below if you don't know how to do it
How to Upload Android Studio Project to GitHub?
- Please make sure the repository name matches the
artifactId
in step 5 and uncheck the private check box
7. Sign Up JitPack
- Go to jitpack.io, click the Sign In button at the top left
- Authorize JitPack to allow JitPack access to your GitHub account
- Select your repository and click Look Up. You should see the following:
8. Create a New Release to Trigger JitPack Build
- Go to your repository, click Releases at the right (below the About).
- Click Draft a New Release
- Click Chose a tag, and enter the same version that you specify in step 5 above
- Press enter
- Click Publish release
9. Monitor JitPack Build
- Go back to jitpack.io, and click Look Up.
- Wait for a while, you should see the Log icon is build in progress.
- When the build is done, you should see something like this:
Note: If the build failed, you should see the red report. If it passes, you should see the green report above.
- Click on the green report, you should see something like this at the end.
10. Import JitPack Android Library
Once the JitPack has successfully built your Android library, it is ready to import your Android library to your project from JitPack.io
.
Note: I'm using the same project that I use to create this Android library as an example.
- In
settings.gradle
, addmaven { url 'https://jitpack.io' }
Groovy
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Kotlin
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven ("https://jitpack.io")
}
}
Note: If you add this in
build.gradle
(project level), it won't work. You must add it in thesettings.gradle
instead.
- In
build.gradle
(app level), replaceimplementation project(':SydneySuburbs')
withimplementation 'com.github.<github_user_name>:<repository_name>:<version_name>'
Groovy
dependencies {
...
implementation 'com.github.vinchamp77:demo-simple-android-lib:0.0.0'
}
Kotlin
dependencies {
...
implementation ("com.github.vinchamp77:demo-simple-android-lib:0.0.0")
}
- Now, your project can import the Android library package and start using it. For example:
import com.vtsen.sydneysuburbs.Sydney
Summary
Some feedback that I get from Twitter is some people in the community won't even consider your library if it's not on mavenCentral()
. So it is worth considering publish to mavenCentral()
.
Well, I want to, but if you have any easy-to-follow tutorials, please let me know. For learning and beginner purposes, JitPack.io is good enough for me, at least for now. It is easy and simple to set it up.
Source Code
GitHub repository:
- demo-simple-android-lib (Groovy example)
- buildutils (KTS example)
Originally published at https://vtsen.hashnode.dev.
Top comments (0)