Firestore #PropertyName annotation giving strange error? - android

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,

Related

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.

ERROR: Could not get unknown property 'appcompat' for project ':app' of type org.gradle.api.Project

Error rising while Building Gradle of a code I found online.
implement "com.android.support:appcompat-v7:$project.appcompat"
implement "com.android.support:cardview-v7:$project.appcompat"
implement "com.android.support:recyclerview-v7:$project.appcompat"
implement "android.arch.lifecycle:runtime:$project.arch"
implement "android.arch.lifecycle:extensions:$project.arch"
implement "com.squareup.retrofit2:retrofit:$project.retrofit"
implement "com.squareup.retrofit2:converter-gson:$project.retrofit"
annotationProcessor "android.arch.lifecycle:compiler:$project.arch"
implement "com.android.support.constraint:constraint-layout:$project.constraintLayout"
implement "com.android.support:support-v4:$project.appcompat"
P.S. Problem is solves as i understood how to use a variable in gradle file
ERROR: Could not get unknown property 'appcompat' for project ':app' of type org.gradle.api.Project
It happens because you are trying to use a property $project.appcompat not defined in your script.
Update your script with something like:
ext {
supportLibraryVersion = '28.0.0' //or your version
}
and then (pay attention, implementation and not implement)
dependencies {
// support libraries
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
//....
}
You are just missing this field here $project.appcompat. Go find what's the current version and place it there. I believe this will help.
So your code transforms from implement "com.android.support:appcompat-v7:$project.appcompat" to implement "com.android.support:appcompat-v7:1.0.0<or current version>"

Difficulty using Moshi's Kotlin codegen

I am trying to use Moshi's Kotlin codegen to get annotation support in Kotlin. Despite following the instructions carefully in the moshi codegen documentation, the annotation JsonClass in #JsonClass(generateAdapter = true) is not recognized and I am getting the following error:
error: incompatible types: NonExistentClass cannot be converted to Annotation#error.NonExistentClass()
My app build.gradle file is as follows:
...
apply plugin: 'kotlin-kapt'
android {
...
}
dependencies {
...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.5.0'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.8.0'
}
The #JsonClass annotation is recognized when I add
implementation("com.squareup.moshi:moshi-kotlin:1.8.0").
However, the moshi reflection documentation indicates that this dependency is only required if using reflection instead of codegen.
Any idea what I'm missing? Thanks!
#JsonClass is a type in the standard Moshi artifact. Retrofit's converter-moshi artifact brings in Moshi transitively but does not have the latest version of Moshi. Specify implementation("com.squareup.moshi:moshi:1.8.0").

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

Android Room Database - Unresolved reference #Entity and other annotations

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"

Categories

Resources