Android: Room stopped working after upgrading to version 2.4.3 - android

Android: The Room database stopped working after upgrading to version 2.4.3. The application complains about the AppDatabase_Impl generated file.
AppDatabase_Impl.java:72: error: onCreate(SupportSQLiteDatabase) in
<anonymous com.mobile_solutions.mycar.database.AppDatabase_Impl$1>
cannot override onCreate(SupportSQLiteDatabase) in Delegate
protected void onCreate(SupportSQLiteDatabase _db) {
^ attempting to assign weaker access privileges; was public
AppDatabase:
#Database(
entities = [
Service::class,
Profile::class,
WorkItem::class,
Auto::class,
Recommendation::class,
TechVisit::class,
NotificationSimple::class,
Expense::class
], version = 85, exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
abstract fun myCarDao(): MyCarDao?
}
I am getting such error:
AppDatabase_Impl.java:72: error: onCreate(Support SQLite Database) in <anonymous com.mobile_solutions.mycar.database.AppDatabase_Impl$1> cannot override onCreate(SupportSQLiteDatabase) in Delegate
protected void onCreate(SupportSQLiteDatabase _db) {
^
attempting to assign weaker access privileges; was public
What to do about it?

I see a very related question solved 7 months ago:
To fix this error for Jetpack Compose and Paging 3 you only need to use only this libraries
//ROOM
implementation "androidx.room:room-runtime:2.4.2"
kapt "androidx.room:room-compiler:2.4.2"
implementation "androidx.room:room-ktx:2.4.2"
implementation "androidx.room:room-paging:2.4.2"
// Paging 3.0
implementation 'androidx.paging:paging-compose:1.0.0-alpha15'
Hope this helps or try following some of the other suggestions from the source! Note I see this answer is about 2.4.3 specifically so check out an answer from the related question that came in December 2022 for that specifically.

Related

Firestore #PropertyName annotation giving strange error?

Here is my data class:
import com.google.firebase.firestore.PropertyName
data class MHatchery(
#set:PropertyName("hName")
var hName:String,
val hLocation:String,
)
Now when I try to enter values, it's giving this error:
Found setter on com...***.models.MHatchery with invalid case-sensitive name: setHName
I am finding it difficult to understand, what's this setHName?? Why am I getting this error? What I am using is hName. Then what's this setHName all about? Please help me understand where am I getting it wrong?
Firestore versions I'm using:
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:28.2.1')
//Firebase
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.firebase:firebase-core'
//Firebase Auth UI
implementation 'com.firebaseui:firebase-ui-auth:7.2.0'
//Firebase Database
implementation "com.google.firebase:firebase-database-ktx"
//FireStore
implementation 'com.google.firebase:firebase-firestore:23.0.2'
and this is my gradle dependencies
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
This is my database screenshot. I want hname to be like hName. That's why I am trying #PropertyName annotation
You are getting the following error:
Found setter on com...***.models.MHatchery with invalid case-sensitive name: setHName
Because the names of the fields in your MHatchery class are different from the ones in the database. For example, in your class, you have a property called hName, with a capital N, while in your database the field is called hname, with a lower-case n. To be able to map a Firestore document into an object of type MHatchery, the name of the properties must match.
There are two ways in which you can solve this issue. The first one would be to change the name of the fields in your class to match the ones in the database. So you should change:
var hName:String,
To:
var hname:String,
And remove the annotation:
//#set:PropertyName("hName")
Or you add the following annotations in front of the property like this:
#get:PropertyName("hName")
#set:PropertyName("hName")
var hName:String,

Room - Schema export directory is not provided to the annotation processor so we cannot import the schema

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

How to solve missing InjectedFieldSignature Error implementing Dagger 2 in android?

I have implemented dependency injection in android before using dagger 2 but, recently, I have tried to use it in a new project but I get the following error:
error: cannot find symbol
import dagger.internal.InjectedFieldSignature;
^
symbol: class InjectedFieldSignature
location: package dagger.internal/location/to/App_MembersInjector.java:30: error: cannot find symbol
Here is my Application component:
#Singleton
#Component(
modules = [
(AndroidInjectionModule::class),
(VmModule::class),
(InjectorModule::class),
]
)
interface ApplicationComponent: AndroidInjector<Application> {
#Component.Builder
interface Builder{
#BindsInstance
fun application(application: App): Builder
fun build() : ApplicationComponent
}
fun inject(home: Home)
}
Then in my App class:
class App: Application(), HasAndroidInjector {
#Inject
lateinit var anAndroidInjector: DispatchingAndroidInjector<Any>
override fun onCreate() {
super.onCreate()
DaggerApplicationComponent.builder().application(this).build().inject(this)
}
override fun androidInjector(): AndroidInjector<Any> {
return anAndroidInjector
}
}
Then the injector module:
#Module
abstract class InjectorModule {
#ContributesAndroidInjector
abstract fun bindHomeActivity(): Home
}
The following is a small excerpt of my app Gradle to show the dagger version:
implementation 'com.google.dagger:dagger-android:2.24'
implementation 'com.google.dagger:dagger-android-support:2.24'
kapt 'com.google.dagger:dagger-android-processor:2.24'
kapt 'com.google.dagger:dagger-compiler:2.28'
If you have any clue, kindly let me know where the problem might be.
Your Dagger artifact versions don't match. Specifically, you are using dagger-compiler:2.28 to generate code, but including an dependency on Dagger 2.24 instead.
In the specific case of dagger.internal.InjectedFieldSignature, that class appears to have been introduced in Dagger version 2.25.3. Any later version of the Dagger compiler will expect that InjectedFieldSignature exists and can be used in generated code. However, since you're only including Dagger 2.24 in your project, the generated code ends up referring to a class that doesn't exist.
To fix this, make sure all of your Dagger dependencies use the same version.

Using kotlin coroutines with room

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.

Not sure how to convert a Cursor to this method's return type

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"
}

Categories

Resources