Search code examples
kotlinkotlin-multiplatform

Cannot use dependencies in commonMain for kotlin multiplatform


I cannot figure out how to get a commonMain dependency to work in a kotlin multiplatform project. I have read and re-read the documentation many times and have looked at many of the examples, but it just isn't working. Here is the smallest example that I believe should work. What am I doing wrong?

multiplatform-lib

plugins {
    kotlin("multiplatform") version "1.3.61"
    `maven-publish`
}

group = "github.fatalcatharsis"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    mavenLocal()
}

kotlin {
    /* Targets configuration omitted. 
    *  To find out how to configure the targets, please follow the link:
    *  https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#setting-up-targets */

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
    }
}

src/commonMain/kotlin/Test.kt

data class Test (
    val test : Int
)

multiplatform-test

plugins {
    kotlin("multiplatform") version "1.3.61"
}

group = "github.fatalcatharsis"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    mavenLocal()
}

kotlin {
    /* Targets configuration omitted. 
    *  To find out how to configure the targets, please follow the link:
    *  https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#setting-up-targets */

    js {
        browser()
    }

    jvm()

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
                implementation("github.fatalcatharsis:multiplatform-lib-metadata:1.0-SNAPSHOT")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
    }
}

src/commonMain/kotlin/Tester.kt

import github.fatalcatharsis.Test

fun test() {
    val meh = Test()
}

intellij says it resolved the dependency just fine on the project menu, but highlights github as Red. No autocomplete available for "Test". Errors with multiplatform-test\src\commonMain\kotlin\Tester.kt: (1, 8): Unresolved reference: github. Just looks like the dependency content isn't available in the commonMain. I feel like I've missed something subtle and obvious. Any ideas?

Edit: Doing the opposite of what the documentation says for common dependencies, if I change the dependency to:

implementation("github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT")

it produces the error:

Could not determine the dependencies of task ':jsPackageJson'.
> Could not resolve all dependencies for configuration ':jsNpm'.
   > Could not resolve github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT.
     Required by:
         project :
      > Unable to find a matching variant of github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT:
          - Variant 'metadata-api':
              - Found org.gradle.status 'integration' but wasn't required.
              - Required org.gradle.usage 'kotlin-runtime' and found incompatible value 'kotlin-api'.
              - Required org.jetbrains.kotlin.platform.type 'js' and found incompatible value 'common'.

Solution

  • Assuming you've published locally, and that was successful, then the first thing to change is the dependency:

    implementation("github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT")
    

    You probably don't want the metadata artifact.

    Next, add the following to your test app's settings.gradle file.

    enableFeaturePreview("GRADLE_METADATA")
    

    After that, try building on command line. Sometimes intellij does see everything.

    If things still aren't working, I'd start looking at your publish config.