Bundle files with Compose for Desktop (Compose Multiplatform)
Asked Answered
C

2

9

I am an Android developer and I have experience with Jetpack Compose. Now, I am trying to build desktop apps, so I found this Compose for Desktop project. I want to store a JSON file with my desktop app so that I can read the file from my desktop app and show it to users. Not only JSON files, but I also want to store some other files like HTML.

In Android, I could use the raw or assets folders, but I don't know how to do that in Compose for Desktop.

If someone can point me in the right direction, I will appreciate it.

Clung answered 27/6, 2022 at 19:2 Comment(2)
Maybe proto dataStore ? – Yearwood
@Yearwood yes, but I have other files as well so I really need a way to store files within the application. (I edited my question) – Clung
C
7

You can put any assets files inside src\jvmMain\resources access them using useResource.

for example to read json file as text:

useResource("data.json") { stream ->
    val textJson = stream.bufferedReader().use { it.readText() }
}
Clung answered 28/6, 2022 at 23:38 Comment(0)
R
0

Another way (similar to Android raw or assets directory) would be this as explained in their docs.

Create a directory structure like this in your project root directory:

πŸ“‚ myProject
  └─── πŸ“‚ myDirectory // Its name does not matter
      β”œβ”€β”€β”€ πŸ“‚ common  // Files shipped for all platforms
      β”œβ”€β”€β”€ πŸ“‚ windows // Files shipped for windows
      β”œβ”€β”€β”€ πŸ“‚ linux   // Files shipped for linux
      └─── πŸ“‚ macos   // Files shipped for macos

Now, if you want your file to be available in all platforms, place it in the common/ directory. If you want it to by only available in Windows, place it in windows/ directory. and so on.

Then in your project build.gradle.kts specify myDirectory like this:

compose.desktop {
    application {
        // ...
        nativeDistributions {
            // ...
            appResourcesRootDir = (rootDir.toPath() / "myDirectory").toFile()
        }
    }
}
Reface answered 2/8, 2023 at 7:56 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.