Consuming a Private KMP dependency from GitHub

Alejandro Moya
TeamSnap Engineering
3 min readAug 7, 2023

Our previous post explained how to create a private KMP library on github without having to share the same repository for the Android and iOS project. This wouldn’t be complete without explaining how to consume the library from each platform. Well, this post will give you the configuration needed for the Android and iOS project to work.

The Android configuration

On the root build.gradle or build.gradle.kts you’ll find the repositories:

allProjects {
repositories {
google()
mavenCentral()
...
}
}

We need to add the private maven repo there:

  • build.gradle.kts :
allprojects {
repositories {
...
maven {
url = URI("https://maven.pkg.github.com/USER/REPO")
credentials {
username = System.getenv("MYUSER")
password = System.getenv("MYPAT")
}
}
}
}
  • build.gradle :
allprojects {
repositories {
...
maven {
url 'https://maven.pkg.github.com/USER/REPO'
credentials {
username = "${System.getenv("MYUSER")}"
password = "${System.getenv("MYPAT")}"
}
}
}
}

As you can see on the scripts again, we see the MYUSER and MYPAT environment variables, each user will add these to their computer with their own github user and private access token with access to the private repo.

This works for mac, windows and linux, as long as the user adds the environment variables to their system environment variables configuration, they will have access to the repo.

After this, they can add the library as a regular dependency using api or implementation , depending on how you set the package name it should look like this:

implementation("com.example.mylibrary:shared-android:0.0.1")

This finishes the Android project configuration.

The iOS project configuration

There are a few options available on mac to achieve the same behaviour, you could use the same approach as on Android by using the environment variables, I’ve used that option in the past, but there’s a catch if you have a project that runs on CI. using environment variables on cocoapods exposes the information on the console output. This is due to cocoapods using git commands to retrieve the dependencies from each repo.

So, which option is better for cocoapods?

The answer is git credentials. And how?

The answer is a terminal command:

git config --global credential.helper store

After running this command, you can add the dependency to your pod file as any other regular dependency, in the case of the KMP library you would use the name and basename parameters under the cocoapods configuration on the gradle script file:

pod 'MySharedLibrary', :git => 'https://github.com/USER/REPO.git', :tag => '0.0.1'

Once you add this to the pod file and save it you can run pod install as usual, you’ll get asked about the credentials, once you complete the credentials those will be saved for git to use them in the future. Remember you can’t use your password on git, you’ll have to create a PAT to be able to use this feature.

Well, that completes the configuration, you can now consume your KMP library from Android and iOS, you can keep working on your projects as usual, and you can now share a codebase that is built as native libraries for each platform. We at TeamSnap use this successfully and each mobile platform team enjoys the benefits of having a single codebase for some of our architecture. As always, leave your comments and we’ll read and answer as we can.

Happy coding!

--

--

Alejandro Moya
TeamSnap Engineering

Mobile developer, Father, geek, this is my first attempt at having a blog, be kind 😄