Publishing a Kotlin library on Github

Publishing a Kotlin library on Github

This blog post will go through how I published a library written in Kotlin and hosted on Github using an awesome service called Jitpack.

I created the library Magic8Ballrrr which is used during my streams on Twitch to give viewers "valuable" advice in exchange for channel points.

Creating a library in Kotlin

Before publishing a library written in Kotlin, we need to create one. If you've never created a library before, the easiest way to get started is with the Gradle CLI tool.

Generating a library project

Run the init command and choose all the options it gives you:

gradle init

You'll be asked about which programming language (e.g. Java, Kotlin, Scala, etc.), project type (e.g. library, application), etc. You'll want to choose library in Kotlin.

This will create your library in a lib folder.

Writing the code

This part should be obvious. Write your code. Also make sure the classes you want to export are public (the default visibility modifier in Kotlin) and ones you don't want exposed are internal or private.

Publishing the library

I used the maven-publish Gradle plugin and populated the publishing section of the build.gradle file with the relevant information to help with publishing.

Jitpack is a great service that offers building JARs from Github repositories. You can learn how to publish on Jitpack here. The service is free for public repositories.

Publishing documentation

You'll want to make sure people will know how to use your library, so one good way in a Kotlin project to do this is to use KDoc syntax to document all your methods and generate documentation with Dokka.

The documentation for this library can be found on the Github Pages deployment: https://techygrrrl.github.io/Magic8Ballrrr

You can see the build.gradle file for configuration details.

Usage in another project

I used this library in techydrrroid, my Twitch chatbot written in Kotlin.

The first thing I did was included it in my build.gradle file:

implementation("com.github.techygrrrl:Magic8Ballrrr:0.1.1")

Usage in Kotlin

Then, I hooked into my existing logic for listening for channel point redemptions by ID and implemented the following method:

private suspend fun handleMagic8BallRedemption(eventData: ChannelPointsRedemptionEventData) {
    val username = eventData.userName
    val userInput = (eventData.userInput ?: "").trim()
    if (userInput.isBlank()) {
        sendChatAsBot("@$username input cannot be blank WutFace")
        return
    }

    val answer = Magic8Ballrrr().ask(userInput)
    val response = "@$username the magic 8 ball's response to your question '$userInput' is: ${answer.format()}"

    sendChatAsBot(response)
}

Magic8Ballrrr strongly considers their question (/s) before telling the viewer their future.

It uses the default answers, which are the standard Magic 8 Ball answers that you may already be familiar with.

You can also customize your own by instantiating Magic8Ballrrr with a list of custom answers. You can see a Kotlin example with custom answers here.

Usage in Java

Here's an example in Java:

ArrayList<Answer> customAnswers = new ArrayList<>();
customAnswers.add(new Answer.Positive("Yes", "✅"));
customAnswers.add(new Answer.Negative("No", "⛔️"));
customAnswers.add(new Answer.Neutral("Maybe", "🔶"));

Answer answer = new Magic8Ballrrr(customAnswers).ask(question);

You can see the full Java example here.

What's next?

So that's it for now but there's still room for improvement. If you'd like to help, here are some immediate things:

For more, see the issues backlog.

Stock photo of pool 8 ball from Pixabay.