codelab-android-datastore
codelab-android-datastore copied to clipboard
Proto DataStore: Unresolved reference dataStore
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:
- Download the base project
git clone https://github.com/googlecodelabs/android-datastore
- modify gradle file according to https://developer.android.com/codelabs/android-proto-datastore#4
- create folder
/app/src/main/proto
- create file
user_prefs.proto
in/app/src/main/proto
according to https://developer.android.com/codelabs/android-proto-datastore#5 - Build -> Clean Project
- Build -> Rebuild Project (proto files get successfully generated)
- Generate
UserPreferencesSerializer
in packagedata
and insert Code mentioned in https://developer.android.com/codelabs/android-proto-datastore#5 - Add all missing imports in class
UserPreferencesSerializer
- 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.
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
@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 :) 👍
I made it work with Android Studio Artic Fox Canary 15 and gradle pluging 7.0.0-alpha15, hope it helps someone. Cheers.
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
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
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.
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.
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"