How to Publish Android Library on JitPack.io with GitHub?
Tags: android, beginners, programming, kotlin
Published: 2022-03-25 21:32:20

**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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641388962988/8Zst6Fzi1.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641428410787/8JgbkVEQ8.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641429055483/mxkiCKkJ-0.gif)
- Implement this code as an example

```kotlin
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 the `build.gradle` (app level) file.

**Groovy**
```kotlin
dependencies {
    ...
    implementation project(':SydneySuburbs')
}
```
**Kotlin**
```kotlin
dependencies {
    ...
    implementation(project(":SydneySuburbs"))
}
```


- Access / use the code that you created in step 3 above. E.g.  `Sydney.suburbs[0]`

```kotlin
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 in `build.gradle` file (Android library module level).

**Groovy**
```kotlin
plugins {
    ...
    id 'maven-publish'
}
```

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

- Add `afterEvaluate` at the end of the `build.gradle` file (Android library module level)

**Groovy**
```kotlin
publishing {
    publications {
        release(MavenPublication) {
            groupId = 'com.github.vinchamp77'
            artifactId = 'demo-simple-android-lib'
            version = '0.0.0'

            afterEvaluate {
                from components.release
            }
        }
    }
}
```

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

            afterEvaluate {
                from(components["release"])
            }
        }
    }
}
```

> 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]( https://developer.android.com/studio/publish-library/upload-library#create-pub)*.

- Switch to project mode, add the `jitpack.yml` in project root folder

![How_to_Publish_Android Library_on_JitPack_04.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1641439047919/Fpld70pHFy.gif)

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.

```kotlin
jdk:
  - openjdk17
```

***\[Updated - Oct 15, 2022\]:*** *If you get the following warnings, you may want to update the* [*publising options*](https://developer.android.com/reference/tools/gradle-api/7.1/com/android/build/api/dsl/PublishingOptions) *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**
```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.*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685181368107/11b986e5-d030-41d5-8422-b9f09fc1e80c.png align="center")

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?](https://vtsen.hashnode.dev/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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641439315923/mq_lprzGS.png)

## 7. Sign Up JitPack
- Go to [jitpack.io](https://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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641439777059/TT9ivMLs3.png)

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

![How_to_Publish_Android Library_on_JitPack_07.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641440019781/KX-cIUENJ.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641440567117/y5UKDPLUe.gif)

## 9. Monitor JitPack Build
- Go back to [jitpack.io](https://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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641441004610/dYg0TQzSL.png)

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

![How_to_Publish_Android Library_on_JitPack_10.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641441081974/AohnjxfO5.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641441213792/TiwyEY_yn.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**
```kotlin
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()

        maven { url 'https://jitpack.io' }
    }
}
```

**Kotlin**
```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 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**
```kotlin
dependencies {
    ...
    implementation 'com.github.vinchamp77:demo-simple-android-lib:0.0.0'
}
```

**Kotlin**
```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:

```kotlin
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](https://github.com/vinchamp77/demo-simple-android-lib) (Groovy example)
- [buildutils](https://github.com/vinchamp77/buildutils) (KTS example)

---

*Originally published at [https://vtsen.hashnode.dev](https://vtsen.hashnode.dev/how-to-publish-android-library-on-jitpackio-with-github)*.