I'm writing an Android app that is using DataStore. I'm failing to compile even a stripped-down version of it - I always get a compilation error for the import statements saying unresolved Reference to edit
, stringPreferencesKey
and so on of PreferenceDataStore:
Task :app:compileDebugKotlin FAILED
e: file:///C:/Users/\<...\>/MainActivity.kt:14:44 Unresolved reference 'edit'.
e: file:///C:/Users/\<...\>/MainActivity.kt:15:44 Unresolved reference 'stringPreferencesKey'.
e: file:///C:/Users/\<...\>//MainActivity.kt:25:44 Unresolved reference 'stringPreferencesKey'.
e: file:///C:/Users/\<...\>//MainActivity.kt:31:41 Unresolved reference 'edit'.
e: file:///C:/Users/\<...\>//MainActivity.kt:32:17 Unresolved reference 'it'.
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import androidx.lifecycle.lifecycleScope
import de.dzwfd.example.glucocollect.ui.theme.MyTheme
import kotlinx.coroutines.launch
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
class MainActivity : ComponentActivity() {
val DS_USER: Preferences.Key<String> = stringPreferencesKey("DS_USER")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val user = "Frank"
lifecycleScope.launch {
this@MainActivity.dataStore.edit {
it[DS_USER] = user
}
}
enableEdgeToEdge()
setContent {
Surface(modifier = Modifier.fillMaxSize()) {
Text("Hallo")
}
}
}
}
build.gradle.kts (dependency group):
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.datastore.preferences)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.lifecycle.runtime.compose.android)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
I stripped-down the app as far as possible and checked the gradle file. I'm using the latest version of 1.1.5 for androidx.datastore:datastore-preferences
and the latest Kotlin version kotlin = "2.1.20"
.
This seem to only happen when building the app, the built-in compiler of Android Studio doesn't show any issues. This also seems to be exclusive to clean builds with data store version 1.1.5.
My build environment is up to date - I'm completely clueless.
This seems to be an issue with Datastore 1.1.5, downgrading androidx.datastore:datastore-preferences
to 1.1.4 should solve the issue until this is fixed.