Search code examples
androidkotlinandroid-jetpackandroid-jetpack-datastore

unable to write preferences in jetpack data store class not found exception


I am using jetpack datastore to store user preferences. I am to retrieve data perfectly but when I try to write data in datastore it give me an error . The error is attached it is a class not found exception I do not know if some thing is wrong with the dependency or it is just me.

this is the class I made to handle data store

enum class UiMode {

    LIGHT,DARK
}

class DarkModeManager (context : Context){

    private val dataStore = context.createDataStore("settings")

    val uiModeFlow: Flow<UiMode> = dataStore.data
        .catch {
            if (it is IOException) {
                it.printStackTrace()
                emit(emptyPreferences())
            } else {
                throw it
            }
        }
        .map { preference ->
            val isDarkMode = preference[IS_DARK_MODE] ?: false

            when (isDarkMode) {
                true -> UiMode.DARK
                false -> UiMode.LIGHT
            }
        }

    suspend fun setUiMode(uiMode: UiMode) {
        dataStore.edit { preferences ->
            preferences[IS_DARK_MODE] = when (uiMode) {
                UiMode.LIGHT -> false
                UiMode.DARK -> true
            }
        }
    }

    companion object {
        val IS_DARK_MODE = preferencesKey<Boolean>("dark_mode")
    }
}

this is the function from where i am trying to store data into data store

   override fun onOptionsItemSelected(item: MenuItem): Boolean {

    lifecycleScope.launch {
        when (isDarkMode) {
            true -> {
               DarkModeManager.setUiMode(UiMode.LIGHT)
                removeDarkMode()

            }
            false -> {
             DarkModeManager.setUiMode(UiMode.DARK)
                applyDarkMode()
            }
        }
    }

    return super.onOptionsItemSelected(item)
}

this is the error i am getting

2020-11-13 16:22:53.398 21567-21567/com.infinity.movieapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.infinity.movieapp, PID: 21567
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/datastore/preferences/PreferencesProto$PreferenceMap;
    at androidx.datastore.preferences.core.PreferencesSerializer.writeTo(PreferencesSerializer.kt:60)
    at androidx.datastore.preferences.core.PreferencesSerializer.writeTo(PreferencesSerializer.kt:36)
    at androidx.datastore.core.SingleProcessDataStore.writeData$datastore_core(SingleProcessDataStore.kt:296)
    at androidx.datastore.core.SingleProcessDataStore.transformAndWrite(SingleProcessDataStore.kt:280)
    at androidx.datastore.core.SingleProcessDataStore$actor$1.invokeSuspend(SingleProcessDataStore.kt:165)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.datastore.preferences.PreferencesProto$PreferenceMap" on path: DexPathList[[zip file "/data/app/com.infinity.movieapp-9Pi09cjXg2oV8vtvkci8Zg==/base.apk"],nativeLibraryDirectories=[/data/app/com.infinity.movieapp-9Pi09cjXg2oV8vtvkci8Zg==/lib/arm64, /system/lib64, /system/vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at androidx.datastore.preferences.core.PreferencesSerializer.writeTo(PreferencesSerializer.kt:60) 
    at androidx.datastore.preferences.core.PreferencesSerializer.writeTo(PreferencesSerializer.kt:36) 
    at androidx.datastore.core.SingleProcessDataStore.writeData$datastore_core(SingleProcessDataStore.kt:296) 
    at androidx.datastore.core.SingleProcessDataStore.transformAndWrite(SingleProcessDataStore.kt:280) 
    at androidx.datastore.core.SingleProcessDataStore$actor$1.invokeSuspend(SingleProcessDataStore.kt:165) 
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) 

Solution

  • I'm facing the same issue. The issue was because version 1.0.0-alpha03 missing datastore-preferences-proto library.

    You can check the issue here.

    Temporary solution for me is to use previous version 1.0.0-alpha02 while waiting until it got fixed.

    UPDATE

    The issue has been fixed in 1.0.0-alpha04.