Logo

dev-resources.site

for different kinds of informations.

How to Create and Publish an Android Library for Beginners

Published at
1/11/2025
Categories
kotlin
androiddev
android
jitpack
Author
khushpanchal123
Categories
4 categories in total
kotlin
open
androiddev
open
android
open
jitpack
open
Author
15 person written this
khushpanchal123
open
How to Create and Publish an Android Library for Beginners

In this article, we will explore the simplest way to create and publish an open-source Android library. This guide is divided into three parts:

  1. How to push an Android project to GitHub
  2. How to create an Android Library
  3. How to publish the library

Create and Push the Project to GitHub

For demonstration, weโ€™ll create a basic library to show a toast (short UI message).

  • Open Android Studio and create a new project with an empty activity. Set the project name (e.g., ToastMe) and package name (e.g., com.toastme), then click Finish.

1

  • Go to GitHub and create a new repository (e.g., ToastMe).

2

  • Open the Android Studio terminal and run the following commands:
git init
git remote add origin [email protected]:khushpanchal/ToastMe.git
git add .
git commit -m "Init commit"
git push -u origin main
Enter fullscreen mode Exit fullscreen mode
  • Refresh GitHub to confirm the code has been pushed.

Create an Android Library

Now, letโ€™s create the library module.

  • In Android Studio, go to File > New > New Module.
  • Select Android Library, set a name (e.g., toastlibrary), and package name (e.g., com.toastlibrary), then click Finish.
  • You should see a new folder (toastlibrary) in the project structure.
  • Add a ToastUtil class in the library module with a utility function to show a toast message:

3

  • Commit the changes:
git add .
git commit -m "Add toastlibrary module"
git push
Enter fullscreen mode Exit fullscreen mode
  • To test the library, add the following dependency to the app-level build.gradle:
implementation(project(":toastlibrary"))
Enter fullscreen mode Exit fullscreen mode

Now, ToastUtil can be used anywhere in the app.

Publish the library

Now comes the interesting part, letโ€™s publish it and make it open source for world to use. We will be using JitPack for hosting our library.

  • Add the maven-publish plugin to the project-level build.gradle.kts:
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.android.library) apply false
    `maven-publish` // Add this
}
Enter fullscreen mode Exit fullscreen mode
  • Update the library (toastlibrary) moduleโ€™s build.gradle.kts:
plugins {
    alias(libs.plugins.android.library)
    alias(libs.plugins.kotlin.android)
    `maven-publish` // Add this
}

android {
    ..
    ..
    publishing { // Add this
        singleVariant("release") {
            withSourcesJar()
            withJavadocJar()
        }
    }
}

dependencies {
    ..
    ..
}

// Add this
publishing {
    publications {
        create("release", MavenPublication::class) {
            groupId = "com.github.khushpanchal" // com.github.<yourusername>
            artifactId = "ToastMe" // your repository name
            version = "0.0.1" // version we want to publish (say 0.0.1)

            afterEvaluate {
                from(components["release"])
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Create jitpack.yml file at project root level and add following
jdk:
  - openjdk17
Enter fullscreen mode Exit fullscreen mode

4

  • Go to GitHub and create a new release:

    • Navigate to the Releases tab in the right and click Create a new release.
    • Choose a tag (e.g., 0.0.1), add a title (e.g., v0.0.1), and publish the release.

5

  • Go to JitPack.io, log in with GitHub, and enter the repository URL (e.g., khushpanchal/ToastMe). Click Get It and wait for the build to complete.
  • Once published, the library will be available for use.

6

Test the Published Library

  • Add JitPack to the settings.gradle.kts of any Android project:
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
            url = uri("https://jitpack.io") // this one
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Add the library to the module-level build.gradle.kts:
implementation("com.github.khushpanchal:ToastMe:0.0.1")
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You have successfully created and published your first Android library. To update the library, modify the code, push the changes to GitHub, create a new release tag, and let JitPack handle the rest.

Source Code: GitHub
Contact Me: LinkedIn | Twitter

Happy coding! โœŒ๏ธ

kotlin Article's
30 articles in total
Favicon
Using SVGs on Canvas with Compose Multiplatform
Favicon
Kotlin Generics Simplified
Favicon
Understanding Quick Sort in Kotlin : A Beginner's Guide
Favicon
Understanding Selection Sort in Kotlin: A Beginner's Guide
Favicon
Wednesday Links - Edition 2025-01-08
Favicon
Testing Pi Logic integrations.
Favicon
Understanding (a bit of) the Gradle Kotlin DSL
Favicon
Android TreeView(Hierarchy based) with Kotlin
Favicon
Creando un notebook con Jupyter y Kotlin
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 3/3)
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 2/3)
Favicon
Understanding Room Database in Android: A Beginner's Guide
Favicon
Fixing Rounded Navigation Bar Corner Padding in Jetpack Compose
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 1/3)
Favicon
My conference year
Favicon
Authentication in Android Project with Firebase.
Favicon
Learn Firebase for Android Development from Scratch, a beginner guide.
Favicon
๐Ÿงน Improve Filtering with the Predicate Interface!
Favicon
How to make the best of a slow machine running on limited resources with a Windows environment as a Java Engineer
Favicon
How to implement detekt in Spring Boot + Kotlin + Gradle project
Favicon
How to Create and Publish an Android Library for Beginners
Favicon
Pub-sub Redis in Micronaut
Favicon
ISBN Stacks โ€” A look at a possible Spring Application implementation without annotations
Favicon
Protecting Applications with Kong security plugins and using StatsD to monitor system states โ€” A healthy camera story
Favicon
Configurable Kong API Gateway with Micronaut Services in Kotlin โ€” A very odd Yucca tribute concert
Favicon
Learning AWS with Localstack and Reactive Kotlin โ€” A stamps and coins implementation
Favicon
Coroutines, Distributed Cache, Resilience, and Replication in Kotlin โ€” Making a VMAโ€™s application
Favicon
From Paris to Berlin โ€” Creating Circuit-Breakers in Kotlin
Favicon
Understanding Merge Sort in Kotlin: A Beginner's Guide
Favicon
City Library โ€” An advanced guide to Circuit Breakers in Kotlin

Featured ones: