19

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 {
            [email protected] {
                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.

1

4 Answers 4

23

This seems to be an issue with Datastore 1.1.5, downgrading androidx.datastore:datastore-preferences to 1.1.4 or upgrading to 1.2.0-alpha01 should solve the issue until this is fixed.

Sign up to request clarification or add additional context in comments.

1 Comment

You might want to link the tracking issue as well: issuetracker.google.com/issues/413061399
2

Update to 1.1.6, this issue is fixed in this version.

1 Comment

Yes, it does. Yesterday I tried it out.
1

Changing

implementation 'androidx.datastore:datastore-core-android:1.1.5'
implementation 'androidx.datastore:datastore-preferences-core-android:1.1.5'

to

implementation 'androidx.datastore:datastore-core-android:1.1.4'
implementation 'androidx.datastore:datastore-preferences:1.1.4'

worked for me.

1 Comment

Isn’t this exactly what the top-voted, accepted answer already says?
0

Starting from 1.1.4, and enforced in 1.1.5, the default storage backend for PreferencesDataStore was changed from OkioStorage to FileStorage via a new PreferencesFileSerializer. This aims to reduce CorruptionException, but it introduces incompatibility if your app still has preferences saved with the old serialization format (i.e., from before 1.1.4).

1 Comment

This doesn't explain to me why existing code won't compile with 1.1.5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.