Creating and publishing your own Android library in Kotlin (2023) | Part -1

Prashant Dixit
6 min readJul 3, 2023

--

In this comprehensive beginner’s guide, we will embark on an exciting journey into the realm of Android development. Get ready to create your very own Android library using Kotlin and learn how to publish it so it can be used by other developers/organizations. So, without further ado, let’s dive into the benefits of building a library and start building one of our own.

What is an Android Library & why should I make one as a developer?

In today’s fast-paced world of Android development, it’s important to learn how to create your own Android library. Why? Well, imagine you’re building a mobile app, and you want to add some cool features or functionalities. Instead of starting from scratch every time, you can save time and effort by using pre-built modules called libraries.

These libraries are like ready-made building blocks that you can easily plug into your app. By creating your own library, you not only make your development process faster but also contribute to the larger community of developers. It’s a great way to share your knowledge and help others in their app-building journey.

With our what and why answered, let’s start coding.

Creating a custom Android library in Kotlin

Step 1: Setting up our sample project

Open Android Studio and start a new project with Empty Views Activity.

Click on Finish and a new project will be created

Step 2: Setting up our Library codebase

Once the project is loaded, to create a new library module, click File -> New -> New Module…

This will open a “Create New Module” window. Here you can see many different templates for creating new modules. In our case, we need to select Android Library from the list of templates and give a title to our library. Once you’ve done that, Click on Finish.

If you have followed the steps mentioned above correctly, you will discover a brand new project folder having the same name as you provided while creating your new library.

Step 3: Adding code inside our library

Let's add code to our library. In this tutorial, we will be using Android’s Snackbar component which shows brief messages on the screen. It provides a non-intrusive way to show informative or actionable messages to the user.

In our library, we’ll create a customizable Snackbar which can be integrated into other applications without the need to write long boilerplate code and add customizations manually.

  • For this tutorial, we will import Snackbar from Android’s Material Design library. Add the following dependency, in your Library’s Module level build.gradle file.
dependencies {
//rest of code
...
implementation 'com.google.android.material:material:1.9.0'
}
  • Inside the Java folder of your library, add a new Kotlin Object file. It should look something like this
  • Next, add the following code to the file we created in the last step.
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar


object Snacky {
fun showSnackbar(view: View, message: String) {
val snackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG)
val snackbarView = snackbar.view

// Set text color
val textView = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text) as? TextView
textView?.setTextColor(ContextCompat.getColor(view.context, android.R.color.white))

// Set position
val params = snackbarView.layoutParams as? FrameLayout.LayoutParams
params?.gravity = Gravity.TOP
snackbarView.layoutParams = params

snackbar.show()
}
}

In the above code, the showSnackbar function takes two parameters, view, and message. It creates a Snackbar with the given message and time duration.

Congratulations, pat yourself on the back. You just created your first library, well done!! Now that our library code is ready and before we deploy it, we need to test our library locally to ensure our library runs perfectly.

Protip: Make it a habit to test each new version of your library when it is included in a sample project so that you can see how the changes affect the app and how simple it will be for developers to use your library. This is a no-brainer, but it will help you avoid making mistakes later on.

In the next step, we’ll do just that.

Step 4: Testing our library locally

At the beginning of the tutorial, we created a new Android Studio project. Now, we will utilize this project to test our library.

In the above image you can see we have two folders, first is app folder that contains the project files for the CustomSnackbar project, and second, we have our library named Snacky.

  • Add Snacky library dependency to the project.

Add the following code to the dependency array in (Module: app) build.gradle file

dependencies {
//rest of code
...
implementation project(':Snacky')
}

Since our library is not hosted on any repository hosting services yet, we will add it as a Project level dependency for local testing. By doing this, our CustomSnackbar project now has access to the library’s code.

  • In the MainActivity.kt file of your app, import the Snacky object from our snacky library.
...
import com.prashd.snacky.Snacky

class MainActivity : AppCompatActivity() {
...
}
  • Next, go to the activity_main.xml file, we’ll add a button here, which when clicked will show the Snackbar.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/rootView"
>

<Button
android:id="@+id/btn_snackbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Snackbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • Now, let’s add functionality to the MainActivity.kt file. We’ll handle the button click event and pass the root view (identified by its XML layout ID) to the showSnackbar function. This will ensure that the Snackbar is displayed on the appropriate part of the screen. Here’s the updated code snippet:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.prashd.snacky.Snacky

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val rootView: View = findViewById(R.id.rootView)

val button = findViewById<Button>(R.id.btn_snackbar)

button.setOnClickListener {
Snacky.showSnackbar(rootView, "This is a test message!!")
}

}
}

Step 5: Build and Test our sample app

Now run the sample app and test the code. If you’ve followed the above steps, you should see the Snackbar appear when the button is clicked. Here is a demo response:

Step 6: Conclusion & Future Steps

Enjoy!! 🥳🎊🎉🎉

In this tutorial, we learned how to create a custom library and add it to your local project and test it. In the next part of this series, we will be working on publishing our Android library to Maven.

You can find the source code of this project on my GitHub. Here is the GitHub repository link.

Thanks a lot for reading this article and follow me for upcoming articles related to app development. Please give a clap if found this article useful. Happy learning😊.

--

--