How to write a file to my "application data" direc...
# compose-desktop
c
How to write a file to my "application data" directory? My compose desktop app works locally/debugging, but started failing when I try to build and distribute it to a friend. I was still trying to setup proper logging to get more details out of why that's the case... but after adding sentry it seems as though that writing an auth token to a file is failing. Is there a standard function call for this that I'm missing or does this have to be hand rolled?
claude gave me
Copy code
fun getAppDataDirectory(): Path {
        val userHome = System.getProperty("user.home")
        
        val appDataPath = when {
            // Windows
            System.getProperty("os.name").contains("Windows") -> {
                Paths.get(System.getenv("APPDATA"), APP_NAME)
            }
            // macOS
            System.getProperty("os.name").contains("Mac") -> {
                Paths.get(userHome, "Library", "Application Support", APP_NAME)
            }
            // Linux and others
            else -> {
                Paths.get(userHome, ".config", APP_NAME)
            }
        }
        
        // Create directories if they don't exist
        if (!Files.exists(appDataPath)) {
            Files.createDirectories(appDataPath)
        }
        
        return appDataPath
    }
but it didn't seem to resolve the problem so I'm not sure if it's mostly right or if it's completely off. Surely writing a file for my app to use is something fairly straight forward right? I think my biggest issue with this (and maybe this is just desktop development), how can I make sure I find these issue during development time instead of after shipping?
m
I would use a library like this https://github.com/Syer10/Kotlin-Multiplatform-AppDirs for that. Also why do you use Java file IO? I’d prefer to do it via Okio or kotlinx-io in order to be prepared for multiplatform. Otherwise there is nothing special about the desktop platform. Writing files into the correct places should just work.
c
why do you use Java file IO?
Simple answer to that really.
I will use that library though. looks like exactly what i need. ❤️
You ask a valid question with the kotlin io point though @Michael Paus I kinda just figure java nio would be find on a the jvm basically. but i guess i can move over to that. i'm literally just doing one file operation... saving one file, and deleting it (on log out) 😄
s
vibe coding 😄
c
hahha. all of my vibe coding is web dev because webdev makes 0 sense to me (well. at least the layout system in css) desktop dev i sorta at least know what's going on because of compose. But even in the android world... i dont really use the file apis at all so im a bit lost there. ill try to get kotlinx io and the appdirs library above
m
When you say "started failing", what happens exactly?
c
"started failing" in this case was that my app does a reading/writing of a token value on startup. and so it was just dying. but i didn't really have proper logging in place so i wasn't sure if it was an issue between debug builds and distributed builds. but the appDirs library is actually exactly what I want. i guess it was silly of me to assume that i could just write a file via a relative path.
m
I see. We have a ticket filed for that already. It's a common mistake. Current working directory on desktop isn't normally any kind of data directory but rather the (read only) app install directory. You are expected to create your own data directory in the user's home dir
c
yeah. im learning a ton of new things while just trying to ship a "simple" desktop app
image.png
m
@mikehearn Actually you are not “expected to create your own data directory in the user’s home dir”. Although it is common practice to put things in a folder like
~/.MyApp/
the various operating systems actually have defined there specific folders to store such data and they often even make distinctions between different kinds of data. For example there may be different folders for cached and non-cached data. This library provides some documentation about this. When you go to iOS or Android things are getting even more complicated.
👀 1
m
Yes, I know. But all those type-specific directories are in the user's home directory.
m
Mostly but Apple also has application folders in /Library/Application Support/ for example.