无法在 Jetpack 数据存储中写入首选项 class 未找到异常

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

我正在使用 Jetpack 数据存储来存储用户首选项。我要完美地检索数据但是当我尝试在数据存储中写入数据时它给我一个错误。错误已附加,它是一个 class 未找到的异常我不知道是依赖项出了问题还是只是我的问题。

这是我用来处理数据存储的class

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")
    }
}

这是我尝试将数据存储到数据存储中的函数

   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)
}

这是我遇到的错误

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.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.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) 

我遇到了同样的问题。问题是因为版本 1.0.0-alpha03 缺少 datastore-preferences-proto 库。

您可以查看问题here

我的临时解决方案是使用以前的版本 1.0.0-alpha02 等待修复。

更新

此问题已在 1.0.0-alpha04 中修复。