In 2.1 Room added support for coroutines, but I can't get it working. It should be as easy as adding the dependency but somehow I'm overlooking something.
In my build.gradle I got the dependencies for coroutines, room and room-coroutines
dependencies {
def room_version = "2.2.0-beta01"
// Room components
implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
androidTestImplementation "android.arch.persistence.room:testing:$room_version"
def coroutines_version = "1.1.1"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
}
I allready tryed resyncing gradle, cleaning and rebuilding the project.
In my Doa i have methods like the following
#Dao
interface PlanDao {
#Insert
suspend fun insertVerPlan(verPlan: SqlVerPlan)
}
When trying to build the project Room doesn't know how to handle the suspending functions and the following error occurs:
error: Type of the parameter must be a class annotated with #Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super kotlin.Unit> p1);
^
error: Methods annotated with #Insert can return either void, long, Long, long[], Long[] or List<Long>.
public abstract java.lang.Object insertVerPlan(#org.jetbrains.annotations.NotNull()
^
I seriously don't know what I'm missing and I can't find anyone with the same problem since the new Room version.
You're mixing different versions of the room library.
android.arch.persistence.room:runtime should be androidx.room:room-runtime
android.arch.persistence.room:compiler should be androidx.room:room-compiler
android.arch.persistence.room:testing should be androidx.room:room-testing
as per Room#Declaring dependencies
Since you're using the old coordinates for the compiler it does not know about the suspend support.
Related
I have updated kotlin plugin to 1.5.20 and this issue happened. If I return it 1.5.10 all works fine.
Schema export directory is not provided to the annotation processor so we cannot import the schema. To generate auto migrations, you must provide `room.schemaLocation` annotation processor argument AND set exportSchema to true.
public abstract class BatteryInfoDatabase extends androidx.room.RoomDatabase {
I'm using the latest version of Room persistence library alpha 3, because it offers auto migration
def room_version = "2.4.0-alpha03"
implementation("androidx.room:room-ktx:$room_version")
kapt("androidx.room:room-compiler:$room_version")
javaCompileOptions {
annotationProcessorOptions {
arguments += [
"room.schemaLocation":"$projectDir/schemas".toString()]
}
}
I had a similar problem to yours after updating my Kotlin version (my schema would not be generated anymore). I could fix it by moving from javaCompileOptions to kapt like so:
kapt {
arguments {
arg("room.schemaLocation", "$projectDir/schemas".toString())
}
}
So replacing your javaCompileOptions with the above block should do the trick.
Edit: Apparently it's a bug in Kotlin. You can follow the discussion (and see other workarounds) in this ticket and its related tickets: https://youtrack.jetbrains.com/issue/KT-47416
I try to add viewModelScope to a basic viewModel but android studio doesn't recognize it.
I tried to change my gradle build file with some solution I found but nothing works.
Here an extract of my build.gradle app
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01"
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01"
When I type viewModelScope in my viewModel it say Unresolved reference: viewModelScope.
for now its in alpha, so please update your gradle to use the following dependencies:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
In my case i forgot to extends ViewModel in that class, the class you use for viewModelScope must be like yourModelClass : ViewModel() in kotlin and for java yourModelClass extends ViewModel
Hope its help
I've had the same issue and I've just imported:
"androidx.navigation:navigation-fragment-ktx:2.2.0-rc03"
"androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-rc03"
Even though I thought fragment-ktx was not really related. Took me a while to figure that out. Hope it helps!
Also check that you are in the correct file. I had the same problem for a moment and I came to this page, but later on, I realized I accidentally tried to run viewModelScope.launch on my Fragment.
viewModelScope.launch is only available in your ViewModels and
lifecycleScope.launch in your lifecycle aware components.
For latest version of the artifact refer
Maven Repository Android Lifecycle ViewModel Kotlin Extensions
In app level build.gradle file add the following :-
def lifecycle_version = "2.2.0-rc03"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
Don't forget to add apply plugin: 'kotlin-kapt' at the top of app/build.gradle file
viewModelScope was introduced with release 2.1.0, see here.
Check whether lifecycle-viewmodel-ktx-2.2.0-alpha01.aar is installed. For me there is no error message with the settings you wrote. However, there is an error message when using an earlier version:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0"
But this works:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0"
Maybe you are not extending the activityViewModel with ViewModel class
class SampleActivityViewModel: ViewModel() {
fun getData(){
viewModelScope.launch{
// Make an API call
}
}
}
Remove below config from build.gradle(:app)
configurations {
all {
exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel-ktx'
}
}
It looks like you've got two different versions of the androidX lifecycle libraries in use.
Change your app/build.gradle to be:
...
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0-alpha01"
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01"
...
ViewModelScope only added in version 2.2.0, not available on higher versions too. I tried with 2.6.0 but got same error.
In Build.gradle (App Level)
Change your code from:
def lifecycle_version = "2.0.0" or if you are using any lower version than this to:
def lifecycle_version = "2.2.0"
viewModelScope was launched in 2.2.0 version of lifecycle module so you won't find it before that.
Build fails with error "cannot find symbol DataBindingComponent" in all generated binding classes.If I remove the room compiler dependency from my module gradle, then it unable to find room db at run time saying "Db_Impl does not exist".
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
//kapt "android.arch.persistence.room:compiler:$room_version"
//kapt 'com.android.databinding:compiler:3.2.1'
dependencies {
def room_version = "2.1.0-alpha06"
implementation "androidx.room:room-runtime:1.1.1"
annotationProcessor "androidx.room:room-compiler:1.1.1" // For Kotlin use kapt instead of annotationProcessor
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:1.1.1"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:1.1.1"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:1.1.1"
// Test helpers
testImplementation "androidx.room:room-testing:1.1.1"
}
Try this..
add this dependency into app level gradle file.
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
more information refer this link
https://www.simplifiedcoding.net/android-room-database-example/
You are missing kotlin's annotation processor plugin. Add this to the top of your app level gradle file
apply plugin: 'kotlin-kapt'
and uncomment the data binding dependency. Sync after this.
I am quite baffled here. So maybe someone can shed some light for me. I have what seems to be a simple pattern (full code below), and yet I am getting compiler error saying
error: Not sure how to convert a Cursor to this method's return
type.
What's the problem? I am on room_version = "2.1.0-alpha02"
#Entity(tableName = "big_dog")
class BigDog(val big: Boolean, #PrimaryKey val id:Int=1)
#Dao
interface BigDogDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(dog: BigDog)
#Query("SELECT * FROM big_dog LIMIT 1")
fun getBigDog(): LiveData<BigDog>
}
Check the logs when you deploy to see if there are any notifications besides this error. If there are go through and fix them. It is possible to get this error if there is something wrong somewhere else wrong with your room code.
For my specific problem it was that I accidentally added a duplicate variable to one of my entities.
public int routeId;
/* other code */
public int routeid;
This caused this warning to pop up in the build log.
Room cannot create an SQLite connection to verify the queries. Query verification will be disabled. Error: [SQLITE_ERROR] SQL error or missing database (duplicate column name: routeId)
I went through and fixed everything up so there was no warnings and Query verification was re-enabled and I no longer got
error: Not sure how to convert a Cursor to this method's return type.
Check your app gradle file. Try don't mix Pre-AndroidX and AndroidX components dependencies in your project.Pay attention to dependencies namespace group. They should have a similar beginning.In my example it's "android.arch.____:X.X.X"
dependencies{
...
def lifecycle_version = "1.1.1"
def room_version = "1.1.1"
// Lifecycle components
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version"
// Room components
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
}
I am using Android Room Persistence library (v.1.0.0-alpha1) in my app.
Although it is working fine, when I open model class (Kotlin Data class) in Android studio, it shows Unresolved reference for all annotations used for Room database like #Entity, #ColumnInfo etc. I tried changing version of arch library to 1.0.0-alpha5 but result was same.
In Lint inspection it is showing Remove deprecated symbol import for all imported annotations.AS was not showing this error previously.
How can I resolve this issue
Edit
Following are imports I have added in my build.gradle
compile "android.arch.persistence.room:runtime:1.0.0-alpha5"
compile "android.arch.persistence.room:rxjava2:1.0.0-alpha5"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha5"
kapt "android.arch.persistence.room:compiler:1.0.0-alpha5"
Here you have an example.
https://github.com/jsperk/PocRoom
Remember, you need add:
Gradle (Project)--> maven
Gradle (Module App) dependencies -->
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
testImplementation "android.arch.persistence.room:testing:1.0.0"
implementation "android.arch.persistence.room:rxjava2:1.0.0"
In my project, I have this question cause I'm using
android.arch.lifecycle:livedata:1.1.1
than no matter using room with version 1.1.1 or 1.0.0, it still can't find the android.arch.persistence.room.Entity.
I've searched for a long time, till I found that, when I delete the LiveData implementation, the problem solved. Then I notice that, the version of these two libraries conflict. At last, I use the same version of 1.1.0 for livedata and room (since livedata have no version of 1.0.0), and solved it.
def arch_version = "1.1.0"
implementation "android.arch.persistence.room:runtime:$arch_version"
annotationProcessor "android.arch.persistence.room:compiler:$arch_version"
implementation "android.arch.persistence.room:rxjava2:$arch_version"
implementation "android.arch.persistence.room:common:$arch_version"
implementation "android.arch.lifecycle:livedata:$arch_version"
implementation "android.arch.lifecycle:extensions:$arch_version"
Adding these dependencies to your Gradle(Module App) file will solve it:-
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
testImplementation "android.arch.persistence.room:testing:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"