https://kotlinlang.org logo
Title
m

Michael Paus

01/24/2022, 11:08 AM
Setting up a multi-platform Compose library project I am just trying to set up a multi-platform Compose library project which I want to install in mavenLocal. I started with a multi-platform Kotlin (Kotlin Gradle DSL) library project template generated by IntelliJ but then I somehow failed to properly add the necessary Compose dependencies. At the moment I am only targeting JVM (desktop) and Android. Can someone show me what is necessary to go from that template to a working one with support for Compose? I am anything else but a Gradle expert.
d

darkmoon_uk

01/24/2022, 11:51 AM
Hi @Michael Paus, I may be able to help.
First, check that these plugins are present e.g:
plugins {
    id("com.android.library")
    kotlin("multiplatform")
    id("org.jetbrains.compose")
}
In the Android config block enable Android toolchain's Compose integrations with:
android {
    ...
    buildFeatures {
        compose = true
    }

    composeOptions {
        kotlinCompilerVersion = Versions.Kotlin // Removing this broke my compilation, despite it being @Deprecated YMMV
        kotlinCompilerExtensionVersion = Versions.Compose.GoogleAndroid
    }
}
Add Compose dependencies to
commonMain
source set:
kotlin {
    ...
    sourceSets {
        val commonMain by getting {
            dependencies {
                ...
                implementation(compose.runtime)
                implementation(compose.material)
                implementation(compose.materialIconsExtended) // Optional
                implementation(compose.foundation)
                implementation(compose.preview)
                implementation(compose.uiTooling)
            }
        }
    }
}
m

Michael Paus

01/24/2022, 1:54 PM
Whatever I do it does not seem to be able to resolve “org.jetbrains.compose”. I only get
Plugin [id: 'org.jetbrains.compose'] was not found in any of the following sources:
And where did you define Versions.Kotlin and Versions.Compose.GoogleAndroid?
g

Guilherme Cordeiro

01/24/2022, 4:34 PM
Have you applied the compose plugin like this:
id("org.jetbrains.compose")
on the build.gradle you imported the compose dependencies, without a specific version? Usually this plugin requires an explicit version parameter, like:
id("org.jetbrains.compose") version "1.0.1"
but you can omit it by declaring a resolution strategy on `settings.gradle.kts`:
pluginManagement {
    repositories {
        google()
        gradlePluginPortal()
        mavenCentral()
        maven("<https://maven.pkg.jetbrains.space/public/p/compose/dev>") // <- is is required in all modules that use compose plugin too
    }

    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "org.jetbrains.compose") {
                useModule("org.jetbrains.compose:compose-gradle-plugin:1.0.1")
            }
        }
    }
}
This way you keep the version on all modules in sync
m

Michael Paus

01/24/2022, 5:03 PM
That was it (so far)! My whole code compiles and the library was successfully published to maven local. Now let’s see whether it actually works when I try to use it? Thanks a lot @darkmoon_uk and @Guilherme Cordeiro . The problem was the missing resolution strategy for
"org.jetbrains.compose"
I also left out the block
composeOptions {
        kotlinCompilerVersion = Versions.Kotlin // Removing this broke my compilation, despite it being @Deprecated YMMV
        kotlinCompilerExtensionVersion = Versions.Compose.GoogleAndroid
    }
and tried with the defaults and I am also wondering why Chris is using the plugin
id("com.android.library")
whereas IntelliJ generated a template with
id("com.android.application")
.