Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proto DataStore: Unresolved reference dataStore #27

Open
SudoDev2021 opened this issue Apr 2, 2021 · 8 comments
Open

Proto DataStore: Unresolved reference dataStore #27

SudoDev2021 opened this issue Apr 2, 2021 · 8 comments
Assignees

Comments

@SudoDev2021
Copy link

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
Copy link

gerardoepitacio commented May 11, 2021

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
Copy link
Author

SudoDev2021 commented May 20, 2021

@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
Copy link

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

@chrisdonovan1006
Copy link

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
Copy link

gerardoepitacio commented Aug 11, 2021

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
Copy link

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
Copy link

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
Copy link

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants