DEV Community

Cover image for How to Publish Android Library on JitPack.io with GitHub?
Vincent Tsen
Vincent Tsen

Posted on • Updated on • Originally published at vtsen.hashnode.dev

How to Publish Android Library on JitPack.io with GitHub?

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

How_to_Publish_Android Library_on_JitPack_01.gif

2. Create a New Module

  • Go to File -> New -> New Module...
  • Select Android Library, update Module Name and Package Name
  • Click Finish

How_to_Publish_Android Library_on_JitPack_02.gif

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

How_to_Publish_Android Library_on_JitPack_03.gif

  • Implement this code as an example
package com.vtsen.sydneysuburbs

object Sydney {
    val suburbs = listOf("Ryde", "Chippendale")
}
Enter fullscreen mode Exit fullscreen mode

4. Use the Local Module

In order to use the module that you just created,

  • Add implementation project(':<Module_Name>') dependency in the build.gradle (app level) file.

Groovy

dependencies {
    ...
    implementation project(':SydneySuburbs')
}
Enter fullscreen mode Exit fullscreen mode

Kotlin

dependencies {
    ...
    implementation(project(":SydneySuburbs"))
}
Enter fullscreen mode Exit fullscreen mode
  • 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])
    }
...
Enter fullscreen mode Exit fullscreen mode
  • Run your app, it should work!

5. Setup and Configure for JitPack.io

  • Add maven-publish plugin in build.gradle file (Android library module level).

Groovy

plugins {
    ...
    id 'maven-publish'
}
Enter fullscreen mode Exit fullscreen mode

Kotlin

plugins {
    ...
    id("maven-publish")
}
Enter fullscreen mode Exit fullscreen mode

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 correct build.gradle file.

  • Add afterEvaluate at the end of the build.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
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Kotin

publishing {
    publications {
        register<MavenPublication>("release") {
            groupId = "demo-simple-android-lib"
            artifactId = "demo-simple-android-lib"
            version = "0.0.3"

            afterEvaluate {
                from(components["release"])
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. groupId = com.github.<Your_GitHub_User_Name>
  2. 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

How_to_Publish_Android Library_on_JitPack_04.gif

The content in jitpack.yml:

jdk:
  - openjdk11
Enter fullscreen mode Exit fullscreen mode

[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
Enter fullscreen mode Exit fullscreen mode

[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()
        }
    }
    ...
}
Enter fullscreen mode Exit fullscreen mode

[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

How_to_Publish_Android Library_on_JitPack_05.png

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

How_to_Publish_Android Library_on_JitPack_06.png

  • Select your repository and click Look Up. You should see the following:

How_to_Publish_Android Library_on_JitPack_07.png

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

How_to_Publish_Android Library_on_JitPack_08.gif

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.

How_to_Publish_Android Library_on_JitPack_09.png

  • When the build is done, you should see something like this:

How_to_Publish_Android Library_on_JitPack_10.png

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.

How_to_Publish_Android Library_on_JitPack_11.png

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, add maven { url 'https://jitpack.io' }

Groovy

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()

        maven { url 'https://jitpack.io' }
    }
}
Enter fullscreen mode Exit fullscreen mode

Kotlin

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()

        maven ("https://jitpack.io")
    }
}
Enter fullscreen mode Exit fullscreen mode

Note: If you add this in build.gradle (project level), it won't work. You must add it in the settings.gradle instead.

  • In build.gradle (app level), replace implementation project(':SydneySuburbs') with implementation 'com.github.<github_user_name>:<repository_name>:<version_name>'

Groovy

dependencies {
    ...
    implementation 'com.github.vinchamp77:demo-simple-android-lib:0.0.0'
}
Enter fullscreen mode Exit fullscreen mode

Kotlin

dependencies {
    ...
    implementation ("com.github.vinchamp77:demo-simple-android-lib:0.0.0")
}
Enter fullscreen mode Exit fullscreen mode
  • Now, your project can import the Android library package and start using it. For example:
import com.vtsen.sydneysuburbs.Sydney
Enter fullscreen mode Exit fullscreen mode

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:


Originally published at https://vtsen.hashnode.dev.

Oldest comments (0)