kotlinandroid-jetpack-composecompose-multiplatformcompose-desktopandroid-jetpack-datastore

DataStore not working in Compose Desktop app


I'm using datastore in compose desktop kotlin multiplatform app:

class DataStoreUseCase() {
    private val dataStore = createDataStore()

    suspend fun saveString(key: DataStoreKey, value: String) {
        dataStore.edit { preferences ->
            preferences[stringPreferencesKey(key.name)] = value
        }
    }

    suspend fun getString(key: DataStoreKey): String? {
        val preferences = dataStore.data.first()
        return preferences[stringPreferencesKey(key.name)]
    }

    private fun createDataStore(): DataStore<Preferences> =
        PreferenceDataStoreFactory.createWithPath(
            produceFile = { "app.preferences_pb".toPath() }
        )
}

It works perfectly when executing the project under Android Studio.

If I generate the executable and execute it under Windows... it doesn't work. Doesn't store or recover variables.

I see that a app.preferences_pb.tmp file is created on the application AppData folder, where it haves permissions and reading or writting of other non datastore files works... but in this case this file has 0 bytes size.

Note it's a .tmp file, and when I execute it under Android Studio i receive a full working file without .tmp extension.

What is happening?


Solution

  • try adding

    modules("jdk.unsupported") and modules("jdk.unsupported.desktop")

    in build.gradle.kts(:desktop) like that :

    nativeDistributions {
           
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            modules("jdk.unsupported")
            modules("jdk.unsupported.desktop") 
                            
            packageName = "desktop"
            packageVersion = "1.0.0"
        }
    

    take a look at https://issuetracker.google.com/issues/280205600