Publish your first library

Gaurav Rajput
3 min readSep 7, 2024

--

We use several third-party libraries in our app, but have you ever thought about creating your own? No worries if you haven’t — it’s actually quite simple and can be done in just a few minutes. Let’s create a library that includes some Kotlin extension functions. For now, we’ll keep it simple and implement just one function that converts a Long value to a date in string format. To keep things straightforward, we'll skip things like error handling and input sanitization. Our main goal is to focus on creating our own library.

fun Long.changeToDateFormat(format:String = "yyyy-MM-dd HH:mm:ss"):String {
val date = java.util.Date(this)
val dateStr = java.text.SimpleDateFormat(format)
return dateStr.format(date)
}

So we have this function which is changing long format values to actual date format.

Let's do this step by step:

Step 1: Create a simple Android project.

Step 2: Go to project level app module and click New -> Module (see image for ref)

Step 3: Create a Java or Kotlin module. We’re choosing this because we’re building a simple extension function library. Rename the library to whatever you prefer and feel free to change the package and class names as well. Once done, click Finish.

Step 4: Now go to your newly created package and create a file let's call it DateExtensions.kt.

Now simply paste your fun in this file

fun Long.changeToDateFormat(format:String = "yyyy-MM-dd HH:mm:ss"):String {
val date = java.util.Date(this)
val dateStr = java.text.SimpleDateFormat(format)
return dateStr.format(date)
}

Step 5. Alright! Now, let’s explore how to publish this library. There are several ways to publish a library, but we’ll go with the simplest method: publishing to Jitpack

To do this, we first need to add a plugin to your library’s build.gradle.kts file. It should look something like this:

plugins {
id("java-library")
alias(libs.plugins.jetbrains.kotlin.jvm)
id("maven-publish")
}

Step 6: Now paste the below code to your same build.gradle file

afterEvaluate {
publishing {
publications {
create<MavenPublication>("release") {
from(components["java"])
// put github user name in place of YOUR_GITHUB_USERNAME
groupId = "com.github.YOUR_GITHUB_USERNAME"
artifactId = "date-extensions" // your library name
version = "1.0" // version name of your first library
}
}
}
}

Step 7: Now create a file on your root level project name jitpack.yml.

Step 8: Paste below code in your jitpack.yml

jdk:
- openjdk17 // specify which jdk version should be used for your
// library to be built by jitpack
before_install:
- ./scripts/prepareJitpackEnvironment.sh

Step 9: Push your code to a GitHub repository, ensuring that the repository is public. You can do this directly from Android Studio by clicking on “Share Project on GitHub.”

Step 10: Now go to your GitHub repo and click on Create a new release

Step 11: Go to jitpack.io and paste your github repo link in search view. Here if it's successfully created you will find it. Click on Get it.

Step 12: Now, your library is ready to be used in other Android projects. To do this, you need to modify two files. As shown in the image above, first, update your root-level build.gradle file by adding maven { url "https://jitpack.io" }. Then, add the dependency for your library in your project. You can now use changeToDateFormat on a Long value in your project.

Congratulations! You’ve successfully published your first library.

--

--