Preeti Tiwari
Preeti Tiwari

Reputation: 347

App crash after app upgrade due to data store migration in Android

After the app upgrade from play store, getting crashes for the data store. Recently I migrated from shared preferences to the data store. This happening only in pixel 5, and not in android 9.

Below is the log for this crash.

Fatal Exception: d1.n.a: Unable to parse preferences proto.
at androidx.datastore.preferences.PreferencesMapCompat$Companion.readFrom(PreferencesMapCompat.java:33)
at androidx.datastore.preferences.PreferencesSerializer.readFrom(PreferencesSerializer.java:39)
at androidx.datastore.preferences.PreferencesSerializer.<clinit>(PreferencesSerializer.java:34)
at androidx.datastore.SingleProcessDataStore.readData(SingleProcessDataStore.java:239)
at androidx.datastore.SingleProcessDataStore.readDataOrHandleCorruption(SingleProcessDataStore.java:218)
at androidx.datastore.SingleProcessDataStore.readAndInitOnce(SingleProcessDataStore.java:181)
at androidx.datastore.SingleProcessDataStore$actor$1.invokeSuspend(SingleProcessDataStore.java:145)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.java:738)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.java:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.java:665)

Upvotes: 4

Views: 2401

Answers (1)

Stefan
Stefan

Reputation: 1074

It is likely that your proto buffer file is corrupt.

One way of addressing this is to add a corruption handler to the creation of your data store.

val context:Context = ... // your context 
val corruptionHandler = ReplaceFileCorruptionHandler<Preferences> {
    mutablePreferencesOf(
       // recover preferences here
    ) 
}
val dataStore = context.createDataStore("data_store", corruptionHandler)

Upvotes: 2

Related Questions