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
14.85k stars 1.08k forks source link

Allow for Resources accessors to be generated for custom directory structure #4718

Open AlexBurdu opened 2 weeks ago

AlexBurdu commented 2 weeks ago

Describe the bug

In the project I'm working on we try to reduce the nesting of the directories and customized the directory structure:

  sourceSets {
    val commonMain by getting {
      kotlin.apply {
        srcDir("commonMain")
        resources.srcDir("commonMain/composeResources")
      }
  // missing code
  }
// missing code

What happens is that the Resources accessors (extensions) are not generated, hence we can't do Res.drawable.cupcake and still need to rely on DrawableResource("drawable/copcake.xml").

It would appear that the new compose resources feature relies on the standard structure and does not generate the accessors when that is customized.

Changing the above to the following fixes the issue:

  sourceSets {
    val commonMain by getting {
      kotlin.apply {
        srcDir("src/commonMain")
        resources.srcDir("src/commonMain/composeResources")
      }
  // missing code
  }
// missing code

Notice the src addition.

Affected platforms

Versions

To Reproduce Steps and/or the code snippet to reproduce the behavior:

  1. Create a new KMP project and customize the directory structure as above
  2. Add a resource in the composeResources directory
  3. Build the project
  4. Observe Unresolved reference: cupcake

Expected behavior Resources should be accessible for custom directory structure, as the path to them is provided in the gradle configuration.

amakeev commented 1 week ago

One of the use-cases for this is Compose Resources support in Amper. The current limitation in the Compose Gradle Plugin doesn't allow configuring it for the Amper-native project layout, which uses the flat folders structure. Example:

project/
  src/
  src@android/
  composeRresources/
  ...
  module.yaml
  settings.gradle.kts

In this example layout, the composeResources folder is located on the module/project level, without additional nesting.

Currently, there is a workaround for Amper to use Gradle-compatible project layout. But this leads to additional setup burden on the user.

In order to support Amper-native project layout, Compose Gradle plugin should allow customizable location of the composeResources folder

See: AMPER-555 Support Compose resources in native Amper layout