Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a new resource dir to sourceSets.commonMain is not affecting Res.drawable #5030

Closed
AgnaldoNP opened this issue Jun 26, 2024 · 4 comments
Assignees
Labels
bug Something isn't working resources

Comments

@AgnaldoNP
Copy link

Scenario

I'm implementing the android flavor strategy to my project. To do that I've added new source and resource dirs. Let's focus in the commomMain:

sourceSets {
    commonMain {
        kotlin.srcDir("src/commonFlavor1/kotlin")
        resources.srcDir("src/commonFlavor1/composeResources")
    }
}

In fact, I added new sources to each flavors (flavor1 and falvor2) and variantes(flavor1Debug, flavor1Release, flavor2Debug, flavor2Release)

sourceSets {
    commonMain {
        kotlin.srcDir("src/common${variant.flavor.flavorName.capitalized()}/kotlin")
        resources.srcDir("src/common${variant.flavor.flavorName.capitalized()}/composeResources")

        kotlin.srcDir("src/common${variant.variantName.capitalized()}/kotlin")
        resources.srcDir("src/common${variant.variantName.capitalized()}/composeResources")
    }
}

The object variant above is gotten according to gradle command been runned.

Problem

All the rouserces added in src/commonFlavor1/composeResources/drawable are not being processed and added to Res.drawable.

Workarrouund

As a way to "solve" this I've create a task to copy the necessary resources to build/generated/compose/resourceGenerator/preparedResources/commonMain/composeResources/.
Unfortunatelly It doesn't work right after a ./gradlew clean I don't know why

fun recursiveCopy(from: File, to: File) {
    if (!from.exists()) return
    from.listFiles()?.forEach { file ->
        if (file.name != ".DS_Store") {
            if (file.isDirectory) {
                val newDir = File(to, file.name)
                newDir.mkdir()
                recursiveCopy(file, newDir)
            } else {
                val destinationFile = File(to, file.name)
                if (destinationFile.exists()) {
                    println("Overwriting $destinationFile")
                    destinationFile.delete()
                }
                if (!destinationFile.parentFile.exists()) {
                    destinationFile.parentFile.mkdirs()
                }
                println("Copying $file to $to")
                file.copyTo(destinationFile)
            }
        }
    }
}

tasks.register("copyKmpResources") {
    doLast {
        println("\n\n--> Copying Flavored KMP Resources")
        val variant = BuildSrcConfig.Variant.getVariant(project = project, gradle = gradle)
        val projectDir = project.projectDir.absolutePath

        val fromFlavor = File(
            projectDir,
            "src/common${variant.flavor.flavorName.capitalized()}/composeResources/"
        )
        val fromVariant = File(
            projectDir,
            "src/common${variant.flavor.flavorName.capitalized()}/composeResources/"
        )

        val destination = "build/generated/compose/resourceGenerator/preparedResources/commonMain/composeResources/"

        println("destination:\n  $destination")
        val destinationFile = File(projectDir, destination)
        println(" - copying \"$fromFlavor\" to \"$destinationFile\"")
        recursiveCopy(fromFlavor, destinationFile)
        println(" - copying \"$fromFlavor\" to \"$destinationFile\"")
        recursiveCopy(fromVariant, destinationFile)
    }
}

tasks.named("prepareComposeResourcesTaskForCommonMain").configure {
    dependsOn("copyKmpResources")
}

Affected platforms

  • Desktop (Windows, Linux, macOS)
  • iOS
  • Web (K/Wasm) - Canvas based API
  • Android

Versions

  • Libraries:
    • org.jetbrains.compose: 1.6.10
  • Kotlin version: 2.0.0
  • OS version: MAC OS 14.5
  • OS architecture: arm64

To Reproduce
Steps to reproduce the behavior:

  1. Add a new composableResource to commomMain
sourceSets {
    commonMain {
        resources.srcDir("src/commonFlavor1/composeResources")
    }
}
  1. Add a new file in the drawable folder in this new source.
  • e.g.: src/commonFlavor1/composeResources/drawable/icon.xml
  1. Rebuild the project
  2. Check Res.drawable availble items, "icon" will not be an option

Expected behavior
As I've added a new source dir to sourceSets -> commomMain -> resources, I expected that the new resources can be used normally.

Additional context
Adding a new source dir to .kt files has worked as expected, only the composeResources doesn't.

I hope I'm not duplicating a bug, I search for similar issues but I wasn't able to find
Tks.

@AgnaldoNP AgnaldoNP added bug Something isn't working submitted labels Jun 26, 2024
@kropp kropp added resources and removed submitted labels Jun 27, 2024
@terrakok
Copy link
Collaborator

I'm implementing the android flavor strategy to my project. To do that I've added new source and resource dirs.

Please don't! The projects source sets are not for the flavoring. The flavoring as you described is the android specific thing. If you need to have different logic/resources then use gradle modules

@terrakok
Copy link
Collaborator

terrakok commented Jun 27, 2024

Anyway, for resource directory customization there was implemented a feature:
#5016

@terrakok
Copy link
Collaborator

terrakok commented Jun 27, 2024

Additional info:

There is no resources in Kotlin Multiplatform!

sourceSets {
    commonMain {
        kotlin.srcDir("...") // <- adds new source directory to the kotlin source set
        resources.srcDir("...") // <- meaningless for the kotlin multiplatform
    }
}

@terrakok
Copy link
Collaborator

The task in the kotlin tracker: https://youtrack.jetbrains.com/issue/KT-33432/MPP-build-variant-feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working resources
Projects
None yet
Development

No branches or pull requests

3 participants