Jeeva
Jeeva

Reputation: 4825

Bundle files with Compose for Desktop (Compose Multiplatform)

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.

Upvotes: 9

Views: 2653

Answers (2)

Mahozad
Mahozad

Reputation: 24542

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()
        }
    }
}

Upvotes: 0

Jeeva
Jeeva

Reputation: 4825

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() }
}

Upvotes: 7

Related Questions