Next, click on the Generate token button at the bottom of the page to create the token. Make sure to copy this token and store it securely, as it will be used later on.

Publishing To GitHub Packages

Next, include the gradle-maven-publish-plugin plugin, created by Niklas Baudy, in the build.gradle.kts file of the math-lib module and then sync your project:

plugins {
id("com.vanniktech.maven.publish") version "0.28.0"

This plugin sets up a publish task for you, making it a breeze to upload all your Kotlin Multiplatform library pieces to any Maven instance. Afterward, create a file in your project’s root directory if one does not already exist and include the following properties:

  • githubPackagesUsername: Your GitHub username.
  • githubPackagesPassword: Your GitHub Personal Access Token

As responsible developers, ensuring the security of credentials such as the above should be a must. Ideally, these two properties should never be committed to version control and should remain private to each developer’s environment. However, in projects involving version control, it is beneficial to consider automated publishing through CI/CD pipelines, such as GitHub Actions. By setting these properties as environment variables within the CI/CD workflow, they can be securely managed and protected from exposure in version controlled files. We will look at how to implement this with GitHub Actions later on.

The next step to enable publishing to GitHubPackages is to configure your POM by adding the following block to your math-lib/build.gradle.kts, replacing all the placeholders in brackets with your own values:

In the code snippet provided, the PasswordCredentials class from the gradle-maven-publish-plugin plugin is used to authenticate with GitHub Packages. This class automatically retrieves the required username and password values from the file. Specifically, it references the githubPackagesUsername and githubPackagesPassword properties defined in the file, ensuring secure authentication with GitHub Packages.

Before we go ahead and publish, make sure your androidTarget block has a publishLibraryVariants if you’re publishing android artifacts:

kotlin {
androidTarget {
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
publishLibraryVariants("release", "debug")

).forEach {
it.binaries.framework {
baseName = "math-lib"
isStatic = true

sourceSets {
commonMain.dependencies {

commonTest.dependencies {

android {
namespace = "io.github.<GITHUB_USER_NAME>"
compileSdk = 34
defaultConfig {
minSdk = 28
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

Next, let’s publish your Kotlin Multiplatform artifacts to GitHub Packages. To do this, open your terminal in the Android Studio IDE, and enter the following command to initiate the publishing process:

./gradlew publishAllPublicationsToGithubPackagesRepository

This command triggers the Gradle task to publish your pre-built artifacts to GitHub Packages. If the artifacts have not been previously built, Gradle will handle the necessary build steps before publishing them. The process typically takes a couple of minutes to complete. After the Gradle command completes successfully, navigate to the Packages tab on your GitHub profile to confirm that your artifacts have been published.

With regards to CI/CD, platforms like GitHub Actions enable us to automate the entire process outlined above using a workflow file. Let’s briefly look at how to set this up.

First, ensure you have a GitHub repository for your sample project. If you don’t already have one, create it. Then, navigate to the repository settings by clicking on Settings > Secrets and variables > Actions:

Here, we’ll need to set up some environment variables or secrets that will be used in our GitHub actions workflow:

  • GH_PACKAGES_USER_NAME: Your GitHub username.
  • GH_PACKAGES_PASSWORD: Your GitHub Personal Access Token.

These credentials will be used to authenticate and perform actions on your behalf during the CI/CD process.

Next, let’s set up the GitHub Actions workflow. Create a new directory named ./github/workflows in the root of your project. Then, create a file named publish.yml with the following contents:

After setting up the GitHub Actions workflow, commit and push the changes to your GitHub repository. Once you’ve pushed your changes, navigate to the Actions tab in your GitHub repository. Here, you should see the newly created workflow named Publish Artifacts. If the workflow hasn’t started automatically, you can manually trigger it by clicking on Run workflow.

After the GitHub Actions workflow completes, you should find the artifacts in the Packages tab on your GitHub profile.

To include or import the published library in a project, ensure that the project has a file in its root directory containing the githubPackagesUsername and githubPackagesPassword properties. Next, modify your settings.gradle file to include the GitHub Packages repository in both the pluginManagement and dependencyResolutionManagement repositories blocks:

Replace <GITHUB_USER_NAME> with your GitHub username. After updating the settings.gradle file, sync your project. Then include the library like so replacing <YOUR_PROJECT_PACKAGE_NAME> with yours:


Congratulations on successfully publishing your Kotlin Multiplatform libraries using GitHub Packages! I hope this guide has been helpful in navigating the process and that you’re now equipped to manage your libraries with confidence.

Happy coding!

Source link