DEV Community

Cover image for How to Create and Publish an Android Library for Beginners
Khush Panchal
Khush Panchal

Posted on • Originally published at Medium

2 1

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 git@github.com: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! ✌️

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay