Cannot use Guava library in iOS by Kotlin Multiplatform

83 Views Asked by At

I am trying to use Kotlin Multiplatform that targets both Android and iOS. But when running tests with the task "iosSimulatorArm64", these imports showed the error "unsolved reference":

import com.google.common.graph.NetworkBuilder

I followed this example, and the build.gradle.kts looks like:

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

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }

    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
        }
    }

    sourceSets {
        commonTest {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
                implementation("com.google.guava:guava:33.0.0-jre")
                implementation("com.google.guava:guava:33.0.0-android")
            }
        }
        val androidUnitTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
    }
}

android {
    compileSdk = 34
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdk = 21
    }
    namespace = "com.jetbrains.android"
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}
1

There are 1 best solutions below

2
Kevin Galligan On

guava is jvm/android only. You can't use it from iOS. You can only use guava from jvm-compatible source sets.