Efficiently managing dependencies and build configurations is critical for Kotlin Multiplatform (KMP) projects targeting Android, iOS, desktop, and web. This guide covers Gradle optimization, platform-specific builds, and CI/CD pipelines to streamline your workflow.
Poorly managed dependencies lead to:
KMP Advantages:
Step 1: Declare versions in gradle.properties
:
# gradle.properties
kotlinVersion = 1.9.20
ktorVersion = 2.3.6
Step 2: Reference in build.gradle.kts
:
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
}
Benefits:
Module | Dependency | Purpose |
---|---|---|
commonMain | kotlinx-datetime | Cross-platform date handling |
androidMain | androidx.lifecycle:lifecycle-viewmodel | Android-specific UI logic |
iosMain | ktor-client-darwin | iOS networking |
Implementation:
// build.gradle.kts
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")
}
}
val androidMain by getting {
dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
}
}
}
}
kotlin {
androidTarget() // Android
iosX64() // Intel Mac iOS Simulator
iosArm64() // M1/M2 iOS Devices
jvm("desktop") { // Desktop (Windows/macOS/Linux)
compilations.all {
kotlinOptions.jvmTarget = "17"
}
}
js(IR) { // Web
browser()
}
}
expect
/actual
Shared Code (commonMain
)
expect fun getDeviceId(): String
Android Implementation
actual fun getDeviceId(): String =
Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
iOS Implementation
actual fun getDeviceId(): String =
UIDevice.current.identifierForVendor?.uuidString ?: ""
Define in build.gradle.kts
:
android {
flavorDimensions += "environment"
productFlavors {
create("dev") {
dimension = "environment"
buildConfigField("String", "API_URL", "\"https://dev.api.com\"")
}
create("prod") {
dimension = "environment"
buildConfigField("String", "API_URL", "\"https://api.com\"")
}
}
}
Access in Code:
val apiUrl = BuildConfig.API_URL
Dev
/Prod
schemes in Xcode.API_URL
to Info.plist
:<key>API_URL</key>
<string>$(API_BASE_URL)</string>
gradle.properties
:
# Parallel execution & caching
org.gradle.parallel=true
org.gradle.caching=true
kotlin.incremental=true
Enable Build Cache:
./gradlew build --build-cache
Repository Setup:
dependencyResolutionManagement {
repositories {
mavenCentral()
google()
}
}
Estimated Impact:
Optimization | Build Time Reduction |
---|---|
Parallel Execution | 30-40% |
Incremental Compilation | 50-60% |
name: KMP Build Pipeline
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
java-version: 17
- run: ./gradlew build
ios:
runs-on: macos-latest
needs: build
steps:
- uses: actions/checkout@v4
- run: ./gradlew :shared:linkDebugFrameworkIosArm64
Force a Specific Version:
dependencies {
implementation("io.ktor:ktor-client-core") {
version { strictly("2.3.6") }
}
}
Analyze Dependencies:
./gradlew :shared:dependencies > deps.txt
Clean Gradle Cache:
./gradlew cleanBuildCache
rm -rf ~/.gradle/caches
gradle.properties
for consistency.commonMain
, androidMain
, iosMain
.strictly()
and dependency reports.Internal Links
External Links
Introduction: Transform Your Cross-Platform Development with Material Design 3 Are you ready to revolutionize your… Read More
Jetpack Compose 1.8 rolls out handy features like Autofill integration, slick Text enhancements including auto-sizing… Read More
Reified Keyword in Kotlin: Simplify Your Generic Functions Kotlin's reified keyword lets your generic functions know the… Read More
Android Studio Cloud: Ditch the Setup, Code Anywhere (Seriously!) Alright, fellow Android devs, gather 'round… Read More