How do I add and use Android libraries in a Kotlin Multiplatform Mobile project?

1.4k Views Asked by At

Feel like this is super straight-forward but I'm new to Kotlin and KMM ><

Basically I am trying to use this library in a KMM project I set up with the KMM plugin in Android Studio.

My shared build.gradle.kts file looks like this: `

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
}

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        version = "1.0"
        ios.deploymentTarget = "14.1"
        podfile = project.file("../iosApp/Podfile")
        framework {
            baseName = "shared"
        }
    }
    
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("com.kizitonwose.calendar:compose:2.0.3")
            }
        }
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    namespace = "com.example.pilld"
    compileSdk = 32
    defaultConfig {
        minSdk = 21
        targetSdk = 32
        multiDexEnabled = true
    }

    compileOptions {
        isCoreLibraryDesugaringEnabled = true
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    dependencies {
        implementation("com.kizitonwose.calendar:compose:2.0.3")
    }
}

dependencies {
    coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.2.0")
}

** App build.gradle.kts (I believe this is called top-level?):**

buildscript {
    dependencies {
        classpath("com.android.tools.build:gradle:7.3.0")
    }
}
plugins {
    //trick: for the same plugin versions in all sub-modules
    id("com.android.application").version("7.3.1").apply(false)
    id("com.android.library").version("7.3.1").apply(false)
    kotlin("android").version("1.7.10").apply(false)
    kotlin("multiplatform").version("1.7.10").apply(false)
    id("org.jetbrains.compose").version("1.2.1")
}

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

** And finally the shared/commonMain/Greeting.kt file in which I am trying to use the library:**

package com.example.pilld

//import com.kizitonwose.calendar.*

class Greeting {
    private val platform: Platform = getPlatform()

    fun greeting(): String {
        return "Hello, ${platform.name}!"
    }
}

`

Haven't tried much yet, just doing setup. I've tried importing in the Greeting.kt file but it doesn't recognize the library. Should I be adding it as a dependency in the commonMain sourceSet, too? I've also mostly been reading documentation but it's a little all over the place and I can't get a straight-forward answer. Any advice would be appreciated!

1

There are 1 best solutions below

2
Evgeny K On

Library you mentioned should be in androidApp module. You need to add this dependency in androidApp/build.gradle.kts file. And use it in your Android presentation layer code (which located in your androidApp/ folder)

You can’t use platform-specific libraries in your shared -> commonMain module. You have two options:

  1. if you want to use library in shared code you need to create common api for your platform-specific code (classes, methods, interfaces) in commonMain and mark it with special expect keyword. Then provide platform-specific realization in androidMain and iosMain and mark them with actual keyword. Please take a look at official documentation

  2. use library directly in your androidApp module, not in shared code