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.99k stars 1.16k forks source link

MissingResourceException using readResourceBytes #4656

Closed dragitpro closed 5 months ago

dragitpro commented 5 months ago

Describe the bug

Just migrating the project from 1.6.2 to 1.6.10-beta01 triggered errors Seems like files are not included anymore or am I missing something ?

Affected platforms

Versions

Exception in thread "AWT-EventQueue-0" org.jetbrains.compose.resources.MissingResourceException: Missing resource with path: files/loader_player.json
    at org.jetbrains.compose.resources.ResourceReader_desktopKt$getPlatformResourceReader$1.getResourceAsStream(ResourceReader.desktop.kt:31)
    at org.jetbrains.compose.resources.ResourceReader_desktopKt$getPlatformResourceReader$1.read(ResourceReader.desktop.kt:7)
    at org.jetbrains.compose.resources.ResourceReaderKt.readResourceBytes(ResourceReader.kt:15)

Additional context

val uri= "files/loader_player.json"
val json = readResourceBytes(uri).decodeToString()

also tied with new api but doesnt seems made for that since path seems not correct :

        val uri = Res.getUri("files/loader_player.json")

or with okio FileNotFoundException

  val uri= "files/loader_player.json"
  val json = platformFileSystem().read(uri.toPath()) {
      readUtf8()
  }
terrakok commented 5 months ago

Did you try any dev versions before the update? There may be broken caches. Try to delete all builds dirs in the project and build it again

dragitpro commented 5 months ago

Thanks for your answer. I hadn't tried the dev version before the update. I deleted the build folders in both the root project and the composeApp folder, invalidated the caches (with all three options checked), and I'm still facing the problem. The Android build is also impacted:

org.jetbrains.compose.resources.MissingResourceException: Missing resource with path: files/loader_player.json at org.jetbrains.compose.resources.ResourceReader_androidKt$getPlatformResourceReader$1.getResourceAsStream(ResourceReader.android.kt:42) at org.jetbrains.compose.resources.ResourceReader_androidKt$getPlatformResourceReader$1.read(ResourceReader.android.kt:8) at org.jetbrains.compose.resources.ResourceReaderKt.readResourceBytes(ResourceReader.kt:15)

I also tried adding the following in case it would help: (not sure if need and how to use)

compose.resources {
    publicResClass = true
    generateResClass = auto
}

but it didn’t solve the problem.

terrakok commented 5 months ago

I need a reproducer of the issue. Could you share your project?

dragitpro commented 5 months ago

Unfortunately, I can't share the project. However, I just tried to reproduce the issue on a minimal project and it works fine there. If you have any ideas about what could be causing this error, such as another cache folder to delete, please let me know. Otherwise, I will continue to investigate on my side

terrakok commented 5 months ago

Maybe you can show your build log which is a reason of the app crash?

dragitpro commented 5 months ago

I find how to trigger the error : its when seting compose-plugin to "1.6.10-beta01" on https://kmp.jetbrains.com/ project using this code :

@OptIn(InternalResourceApi::class)
@Composable
fun DisplayJson(){
    var json by remember { mutableStateOf<String?>(null) }
    Text(json ?: "Loading...")
    LaunchedEffect(Unit) {
        val uri = "files/example.json"
         json = readResourceBytes(uri).decodeToString()
    }
}

So i'm not sure if it was me doing wrong using "1.6.10-beta01" as plugin-version , specialy after reading realse notes

Gradle Plugin org.jetbrains.compose, version 1.6.10-beta01. Based on Jetpack Compose libraries:

terrakok commented 5 months ago

The code snippet uses java resources instead of compose multiplatform resources.

terrakok commented 5 months ago

It won't work with a composeResources directory

dragitpro commented 5 months ago

Thanks ... Solution then : if someone find this issueto not waste a day like me val json = Res.readBytes(uri).decodeToString()

okushnikov commented 2 months ago

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