louiscad
05/13/2025, 11:24 PMephemient
05/13/2025, 11:38 PMJacob Ras
05/14/2025, 7:01 AMKonstantin Tskhovrebov
05/14/2025, 8:35 AMColton Idle
05/14/2025, 5:59 PMmohamed rejeb
05/15/2025, 12:44 PMKaanixir
05/25/2025, 5:06 AMexpect object AppDirs {
fun init(platformCtx: Any? = null)
/** Absolute, per-user, per-app directory â valid after `init` */
val path: Path
}
Desktop target :
actual object AppDirs {
actual fun init(platformCtx: Any?) {}
actual val path: Path by lazy {
val p = (System.getProperty("user.home") + "/.someName").toPath()
FileSystem.SYSTEM.createDirectories(p)
p
}
}
Android target:
actual object AppDirs {
private lateinit var internal: Path
/** MUST be called from Application.onCreate() */
actual fun init(platformCtx: Any?) {
val ctx = platformCtx as Context
val dir = ctx.getDir("someName", Context.MODE_PRIVATE)
internal = dir.absolutePath.toPath()
}
actual val path: Path
get() = if (::internal.isInitialized) internal
else error("AppDirs.init(ctx) not called yet")
}
...and so on.....louiscad
05/25/2025, 9:38 AMAhmed Riyadh
06/06/2025, 8:13 PMKaanixir
06/07/2025, 3:39 AMdirectories-jvm
contains the perfect platform default logic that I'll reuse in this solution, to get a something more accurateAhmed Riyadh
06/07/2025, 7:01 AMAhmed Riyadh
06/07/2025, 7:03 AMhttps://klibs.io/project/psuzn/multiplatform-pathshttps://github.com/psuzn/multiplatform-paths/blob/main/multiplatform-paths/src/des[âŚ]/kotlin/me/sujanpoudel/utils/paths/directories.desktopCommon.kt I mean this solution works but I still prefer to avoid it, it's not a good practice to hardcode these paths as I mentioned earlier. This is just something to keep in mind, even if you don't plan on using better solutions.
Ahmed Riyadh
06/07/2025, 7:06 AMFor example, in Flutterâspecifically on Windowsâit uses the Win32 API directly.
See also: Linux implementation and macOS. This is maintained by Flutter/Google and I never experienced any issues with it, it works perfectly on all cases, including Flatpak and also WSL.BTW, thanks to KMP, it's possible to port path_provider from the Flutter world to Kotlin in even easier way, this is a great opportunity for whoever want to create their first KMP library that solves common issues.
Ahmed Riyadh
06/07/2025, 7:09 AMlouiscad
06/07/2025, 10:53 AMAhmed Riyadh
06/07/2025, 11:00 AMlouiscad
06/07/2025, 11:03 AMIt doesn't have a platform implementation on Windows or Linux but it's well implemented on macOS.Aren't you saying the opposite of what the code you linked says?
Ahmed Riyadh
06/07/2025, 11:06 AMNSFileManager
, on Linux and Windows and macOS/JVM, it uses environment variables of the OS.
See also: https://github.com/vinceglb/FileKit/blob/main/filekit-core/src/macosMain/kotlin/io/github/vinceglb/filekit/FileKit.macos.kt#L29louiscad
06/07/2025, 11:07 AMAhmed Riyadh
06/07/2025, 11:12 AM