Blog Infos
Author
Published
Topics
, , , ,
Published
Streamline Your Kotlin Multiplatform Mobile Development with publishToMavenLocal

 

image source: AI

Intro

You’re an Android developer, excited by the promise of Kotlin Multiplatform Mobile (KMM) to share business logic across platforms. You’ve set up your KMM project, your shared module is taking shape, and now comes the moment of truth: how do you seamlessly integrate and iterate on this shared library within your Android application during development?

If you’ve found yourself wrestling with the idea of constantly publishing to a remote repository just to test a small change, you’ve hit a common and frustrating wall.

This article is your guide to breaking down that wall. We’ll explore the power of Gradle’s publishToMavenLocal task and the mavenLocal() repository. This powerful duo allows you to publish your KMM shared module directly to your local Maven repository, making it instantly available for consumption by your Android app (or any other local project) without touching a remote server.

By the end of this guide, you’ll have a streamlined, offline-capable development workflow that significantly speeds up your KMM integration.

Why Local Maven? The Benefits

Before we dive into the “how,” let’s solidify the “why.” Leveraging a local Maven repository for KMM development offers several compelling advantages:

  • Blazing Fast Iteration: Instead of waiting for a CI/CD pipeline to build, test, and push your KMM library to a remote repository, you can publish locally in seconds. This allows for near-instant feedback on changes made in your shared module.
  • Offline Development: Once your KMM library is published locally, your Android app can consume it even without an internet connection.
  • Reduced CI/CD Load: Frequent small changes don’t necessitate triggering your precious (and sometimes costly) CI/CD pipelines.
  • Simplified Testing: Testing changes in your KMM module directly within the context of your Android application becomes straightforward.
  • Isolation of Experimental Changes: Want to try out a new feature or refactor without affecting others? Local publishing allows you to do this safely.
  • Streamlined Debugging: A local dependency often makes debugging flow between your shared module and the Android app more seamless.
Understanding the Local Maven Repository

When we talk about a “local Maven repository,” we’re referring to a directory on your machine where Gradle (and Maven) stores published artifacts.

Default locations:

  • macOS / Linux: ~/.m2/repository
  • Windows: C:\Users\<your-username>\.m2\repository

Inside this directory, published libraries are organized by their Maven coordinates:

groupId → artifactId → version.

Example:

~/.m2/repository/com/yourcompany/kmm/shared-library/1.0.0-SNAPSHOT/

Setting Up Your KMM Project for Local Publishing

We’ll assume a standard KMM project structure, where your Android application (androidApp) depends on your shared KMM module (shared).

The core of local publishing lies in configuring your shared/build.gradle.kts file.

Apply the maven-publish Plugin

This plugin is essential for enabling publishing capabilities in your Gradle module.

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("maven-publish")
}
Define Group and Version

Use a consistent groupId and a -SNAPSHOT suffix for development versions.

group = "com.yourcompany.kmm"
version = "1.0.0-SNAPSHOT"
Configure Android Library Publishing (If Applicable)

If your shared module targets Android, tell Gradle which variant to publish:

android {
    namespace = "com.yourcompany.kmm.shared"
    compileSdk = 34

sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    publishing {
        singleVariant("release")
    }
}
Configure the publishing Block for KMM

Provide metadata for your library:

publishing {
    publications {
        withType<MavenPublication> {
            pom {
                name = "Your KMM Shared Library"
                description = "A shared Kotlin Multiplatform library for Android and iOS."
                url = "https://github.com/your-org/your-kmm-project"
                licenses {
                    license {
                        name = "The Apache Software License, Version 2.0"
                        url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
                    }
                }
                developers {
                    developer {
                        id = "your_dev_id"
                        name = "Your Name"
                        email = "your.email@example.com"
                    }
                }
                scm {
                    connection = "scm:git:github.com/your-org/your-kmm-project.git"
                    developerConnection = "scm:git:ssh://github.com/your-org/your-kmm-project.git"
                    url = "https://github.com/your-org/your-kmm-project"
                }
            }
        }
    }
}
Running the Publish Task

Publishing is as simple as running a Gradle task from your project root:

./gradlew :shared:publishToMavenLocal

If you have multiple targets, you can also use:

./gradlew :shared:publishAllPublicationsToMavenLocal
Consuming the Local KMM Library in Your Android App
Add mavenLocal() to settings.gradle.kts

This ensures that all modules in your project can resolve dependencies from your local Maven repository:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        mavenLocal()
    }
}
Declare the Dependency in androidApp/build.gradle.kts

Now declare your shared module as a dependency:

dependencies {
    implementation("com.yourcompany.kmm:shared:1.0.0-SNAPSHOT")
}
Sync Gradle

After making these changes, sync your Gradle files in Android Studio.

The Development Workflow

With everything configured, your workflow becomes simple:

  1. Make Changes in your shared module.
  2. Publish Locally:

 

./gradlew :shared:publishToMavenLocal

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

Jobs

3. Rebuild/Rerun your Android app in Android Studio.

Hot Tips for Faster Development
  • Use -SNAPSHOT versions for development.
  • If needed, run ./gradlew clean or use Invalidate Caches / Restart in Android Studio.
Common Pitfalls and Troubleshooting
  • mavenLocal() must be in settings.gradle.kts.
  • Make sure groupIdartifactId, and version match exactly.
  • Don’t forget singleVariant("release") in your android block.
  • If caching issues occur, invalidate caches.
When to Use Remote Repositories

publishToMavenLocal is great for local development, but for stable releases you should publish to:

  • Maven Central
  • Private Artifactory / Nexus
  • GitHub Packages
Conclusion

Integrating Kotlin Multiplatform Mobile into your Android development workflow shouldn’t be a source of constant friction. By mastering the publishToMavenLocal task and configuring mavenLocal(), you unlock a much more efficient and enjoyable development experience.

Go ahead — give it a try in your next KMM project and experience the boost in productivity for yourself!

Dobri Kostadinov
Android Consultant | Trainer
Email me | Follow me on LinkedIn | Follow me on Medium | Buy me a coffee

This article was previously published on proandroiddev.com.

Menu