3

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'.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

My first project has no target defined and only has source sets for common, the only artifact produced is multiplatform-lib-metadata and this is resolved fine. Removing the dep in multiplatform-test gives unresolved dep error, so it's finding the -metadata without issue. My .m2 folder contains multiplatform-lib-metadata.jar as I'd expect. I've added the enableFeaturePreview to my multiplatform-test and multiplatform-lib, but no change. Same result building from command line.
Scratch what I said, didn't see the multiplatform-lib folder. There is a folder generated without a jar, and with a ".module" file for multiplatform-lib. When I change my dep to implementation("github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT"), it produces an error which I'll add to my question description.
Accepting this as answer since it eventually led to my solution. Switching it off metadata showed me first that the lib must implement the targets that the consumer implemented, (js and jvm). Second, my project structure was wrong (didn't notice but my source was actually in, src/commonMain/**main**/kotlin when it should have been in src/commonMain/kotlin. In my actual project, this revealed many errors that weren't showing up previously. Once resolved, I was able to include it in the test project, no problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.