Thomas Cirksena
Thomas Cirksena

Reputation: 947

Kotlin Multiplatform Library with compose UI elements used in androidMain

I'm wondering if it's possible to create a kotlin multiplatform library that contains a composable located at the src of androidMain.

Right now I'm now able to make it work, because android studio is always complaining about something. With the following build.gradle.kts (almost default) I get this shown error:

build.gradle.kts:

    plugins {
        alias(libs.plugins.kotlinMultiplatform)
        alias(libs.plugins.androidKotlinMultiplatformLibrary)
        alias(libs.plugins.composeCompiler)
        alias(libs.plugins.kotlinx.serialization)
    }
    
    kotlin {
    
    // Target declarations - add or remove as needed below. These define
    // which platforms this KMP module supports.
    // See: https://kotlinlang.org/docs/multiplatform-discover-project.html#targets
        androidLibrary {
            namespace = "com.grumpyshoe.poc.feature.feature_dashboard"
            compileSdk = 35
            minSdk = 26
    
            withHostTestBuilder {
            }
    
            withDeviceTestBuilder {
                sourceSetTreeName = "test"
            }.configure {
                instrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
            }
        }
    
    // For iOS targets, this is also where you should
    // configure native binary output. For more information, see:
    // https://kotlinlang.org/docs/multiplatform-build-native-binaries.html#build-xcframeworks
    
    // A step-by-step guide on how to include this library in an XCode
    // project can be found here:
    // https://developer.android.com/kotlin/multiplatform/migrate
    
        listOf(
            iosX64(),
            iosArm64(),
            iosSimulatorArm64()
        ).forEach { iosTarget ->
            iosTarget.binaries.framework {
                baseName = "feature_dashboardKit"
                isStatic = true
            }
        }
    
    // Source set declarations.
    // Declaring a target automatically creates a source set with the same name. By default, the
    // Kotlin Gradle Plugin creates additional source sets that depend on each other, since it is
    // common to share sources between related targets.
    // See: https://kotlinlang.org/docs/multiplatform-hierarchy.html
        sourceSets {
            commonMain {
                dependencies {
                    implementation(libs.kotlin.stdlib)
                    // Add KMP dependencies here
                }
            }
    
            commonTest {
                dependencies {
                    implementation(libs.kotlin.test)
                }
            }
    
            androidMain {
                dependencies {
    
                    implementation(platform(libs.compose.bom))
                    implementation(libs.compose.compiler)
                    implementation(libs.compose.ui)
                    implementation(libs.compose.ui.tooling)
                    implementation(libs.compose.ui.tooling.preview)
                    implementation(libs.compose.material3)
                    implementation(libs.compose.runtime)
                    implementation(libs.compose.navigation)
                    implementation(libs.androidx.lifecycle.runtime.compose)
                    implementation(libs.kotlinx.serialization.json)
                    // Add Android-specific dependencies here. Note that this source set depends on
                    // commonMain by default and will correctly pull the Android artifacts of any KMP
                    // dependencies declared in commonMain.
                }
            }
    
            getByName("androidDeviceTest") {
                dependencies {
                    implementation(libs.compose.compiler)
                    implementation(libs.compose.runtime)
                    implementation(libs.compose.navigation)
                    implementation(libs.androidx.runner)
                    implementation(libs.androidx.core)
                    implementation(libs.androidx.test.junit)
                }
            }
    
            iosMain {
                dependencies {
                    // Add iOS-specific dependencies here. This a source set created by Kotlin Gradle
                    // Plugin (KGP) that each specific iOS target (e.g., iosX64) depends on as
                    // part of KMP’s default source set hierarchy. Note that this source set depends
                    // on common by default and will correctly pull the iOS artifacts of any
                    // KMP dependencies declared in commonMain.
                }
            }
        }
    
    }

The Error:

    Cannot convert the provided notation to an object of type Dependency: 
    map(valueof(DependencyValueSource)).
    The following types/formats are supported:
      - String or CharSequence values, for example 'org.gradle:gradle-core:1.0'.
      - Maps, for example [group: 'org.gradle', name: 'gradle-core', version: '1.0'].
      - FileCollections, for example files('some.jar', 'someOther.jar').
      - Projects, for example project(':some:project:path').
      - ClassPathNotation, for example gradleApi().

I couldn't find anything according to this kind of setup so I thought maybe in this community someone might know more about it.

Upvotes: 0

Views: 29

Answers (0)

Related Questions