How to Publish a Kotlin Multiplatform Library on GitHub Packages
Written on
Publishing a library within the open-source community can be an exciting venture. GitHub serves as a pivotal platform for code collaboration and sharing, and its feature, GitHub Packages, simplifies the process of managing and hosting software packages. If you have developed an impressive library and wish to make it accessible to others, this guide will lead you through the process of publishing it on GitHub Packages.
We choose GitHub Packages for its ease of setup and no associated costs.
Step 1: Create Your GitHub Repository
Before you can publish your Kotlin Multiplatform (KMP) library, you need to have a dedicated repository on GitHub. If you haven’t created one yet, start by clicking the “+” icon on the GitHub homepage and selecting “New repository.” Assign a descriptive name, add relevant details, and initialize it with a README if necessary.
Step 2: Prepare Your Library for Packaging
Ensure that your library is organized and includes all the files needed for distribution. This means having your source code, documentation, and any other important resources. A clear README file is essential, as it will be the first point of reference for potential users.
Step 3: Create an Access Token
To publish packages on GitHub, you must authenticate your account. This is done by generating a personal access token. Go to your GitHub account settings, navigate to “Developer settings,” then “Personal access tokens.” Create a new token with permissions that allow for reading and writing to packages. Copy this token for use in the upcoming steps.
Path to Access Token Creation: Account Settings > Developer Settings > Personal Access Token > Tokens > Generate New Token
Step 4: Publish Your Library
With your repository established, necessary metadata in place, and authentication sorted, it’s time to publish your library to GitHub Packages.
In your gradle.properties file, include your Maven username and password:
In build.gradle.kts, add the following:
Note: - We utilize BasicAuthentication as our authentication method. - The PasswordCredentials::class uses the mavenUsername and mavenPassword for authentication.
It’s advisable to store these credentials in a separate file and add it to your .gitignore to prevent accidental commits to your repository.
Additionally, include it at the root level of your build.gradle.kts:
Make sure to add this configuration for publishing your library:
For build.gradle.kts in the androidApp module:
And for build.gradle.kts in the shared module:
Finally, open your terminal and execute the following command:
You should see a confirmation of the successful operation:
Step 5: Verification
Once your library is successfully published, check the GitHub Packages page for your repository to confirm that your package appears there. This allows users to find, install, and integrate your library into their projects seamlessly.
Now you can utilize your packages:
In settings.gradle.kts:
In build.gradle.kts for androidApp:
In build.gradle.kts for shared:
You’re now ready to enjoy the capabilities of your published packages!