JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.85k stars 1.15k forks source link

Gradle - Platform-specific dependencies are not visible for new buildTypes #661

Open ghost opened 3 years ago

ghost commented 3 years ago

I'm currently working on a project that is very close to the Jetpack Compose multi-platform template generated by IDEA, everything was working fine until I tried to create a new android build type.

The relevant Gradle configuration looks like this:

android {
    // ...
    buildTypes {
        getByName("debug") {
            // ...
        }
        getByName("release") {
            // ...
        }
        create("staging") {
            initWith(getByName("debug"))
            applicationIdSuffix = ".staging"
            // ...
        }
    }
}

With this config, debug and release build types work fine, but when Gradle is trying to build the staging one, some things started to fail, like this:

> Task :android:compileStagingKotlin FAILED
e: /Users/brunomed/git/.../MainActivity.kt: (35, 37): Unresolved reference: LocalContext

After further investigation, I realised that anything that is platform-specific (in this case, included by the org.jetbrains.compose.ui:ui-android-debug as opposed to just org.jetbrains.compose.ui:ui) can't be found by the compiler. Same error happens for other components like AndroidView, for example.

My guess is that *-android-* dependencies are not being included in the classpath for custom build types.

ghost commented 3 years ago

Workaround

As I really didn't want to go back to vanilla Compose (and thus be forced to go to Android Studio 4.2), I was searching for a workaround, until with some help from my team I found that this fixes the problem:

android {
    // ...
    buildTypes {
        create("staging") {
            initWith(getByName("debug"))
            matchingFallbacks.add("debug")           // <== adding this config makes the staging build type work as expected!
            applicationIdSuffix = ".staging"
            // ...
        }
    }
}
okushnikov commented 1 month ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.