android / codelab-android-datastore

Apache License 2.0
227 stars 105 forks source link

Proto DataStore: Unresolved reference dataStore #27

Open SudoDev2021 opened 3 years ago

SudoDev2021 commented 3 years ago

Problem:

I followed every step mentined in the Codelab tutorial https://developer.android.com/codelabs/android-proto-datastore, but Android Studio can't resolve dataStore (unable to import androidx.datastore.dataStore)

How to reproduce the issue:

  1. Download the base project git clone https://github.com/googlecodelabs/android-datastore
  2. modify gradle file according to https://developer.android.com/codelabs/android-proto-datastore#4
  3. create folder /app/src/main/proto
  4. create file user_prefs.proto in /app/src/main/proto according to https://developer.android.com/codelabs/android-proto-datastore#5
  5. Build -> Clean Project
  6. Build -> Rebuild Project (proto files get successfully generated)
  7. Generate UserPreferencesSerializer in package data and insert Code mentioned in https://developer.android.com/codelabs/android-proto-datastore#5
  8. Add all missing imports in class UserPreferencesSerializer
  9. Modify TasksActivity according to https://developer.android.com/codelabs/android-proto-datastore#6

Now Android Studio marks by dataStore as Unresolved reference: dataStore and I'm also unable to import import androidx.datastore.dataStore

private val Context.userPreferencesStore: DataStore<UserPreferences> by dataStore(
    fileName = DATA_STORE_FILE_NAME,
    serializer = UserPreferencesSerializer
)

Please can someone tell me how I can resolve this issue? I followed every steps mentioned in the tutorial and compared the solution files with my local project, but it still doesn't work.

gerardoepitacio commented 3 years ago

Check your dependencies, I was using (note -core at the end), as documentation says.

implementation "androidx.datastore:datastore-core:1.0.0-beta01"

Using this dependency instead fixed my problem.

implementation "androidx.datastore:datastore:1.0.0-beta01"

https://developer.android.com/jetpack/androidx/releases/datastore#declaring_dependencies

SudoDev2021 commented 3 years ago

@gerardoepitacio Thanks for you answer :)

In the meanwhile I have found my own "workaround" by lowering the gradle version from gradle:7.0.0-alpha09 to gradle:4.1.3, but I honestly don't know why datastore doesn't work with the newest gradle version.

Anyway, I will try out your solution tomorrow, and I really can't thank you enough for trying to help me and others :) 👍

gerardoepitacio commented 3 years ago

I made it work with Android Studio Artic Fox Canary 15 and gradle pluging 7.0.0-alpha15, hope it helps someone. Cheers.

chrisdonovan1006 commented 3 years ago

Hey I am having this issue also, even though I declare:

implementation "androidx.datastore:datastore-preferences:1.0.0"

Whenever I look to the call to datastore the import is:

import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences

I really don't want to be downgrading the gradle build in order to get this to work.

Any ideas why this is happening? - I have tried removing the core package but then Unresolved reference: DataStore

gerardoepitacio commented 3 years ago

If you changed the dependency from "-core" to the one with no "-core" make sure to sync the project, clear and rebuild. The configuration mentioned before is working for me.

Notice that I used the Typed version, not the DataStore Preferences version.

Make sure to apply the gradle plugin accordingly https://github.com/google/protobuf-gradle-plugin

dependencies https://developer.android.com/jetpack/androidx/releases/datastore#declaring_dependencies

chrisdonovan1006 commented 3 years ago

I have tried both versions of the dependency. Even invalidating the cache, and rebuilding doesn't seem to matter when I click on import the core dependency is imported.

I will just use shared-prefs for now.

thomasphillips3 commented 3 years ago

I also had this problem while working on a project.

I found that I was using datastore-core and needed datastore-preferences. So I changed my dependency declaration from:

implementation 'androidx.datastore:datastore-core:1.0.0-alpha08'

to:

implementation 'androidx.datastore:datastore-preferences:1.0.0'

Possibly there was a breaking change between alpha08 and the 1.0.0 release.

Also posted on SO.

dbeqiraj commented 2 years ago

Problem still persistent in February 16, 2022. I had to switch from:

implementation "androidx.datastore:datastore-core:1.0.0"

to

implementation "androidx.datastore:datastore:1.0.0"