I'm getting error in room database , :app:kaptDebugKotlin - kotlin

I got this error when I used the room database
Error:
Entity class must be annotated with #Entity - androidx.room.Entity
Error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - androidx.room.Entity
Error:
An entity must have at least 1 field annotated with #PrimaryKey -
androidx.room.Entity
Error:
[SQLITE_ERROR] SQL error or missing database (near ")": syntax
error) - androidx.room.Entity
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'kotlin-android'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.example.todolist"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")
//room
def room_version = '2.4.3'
implementation("androidx.room:room-runtime:$room_version")
annotationProcessor "androidx.room:room-compiler:$room_version"
kapt("androidx.room:room-compiler:$room_version")
implementation("androidx.room:room-ktx:$room_version")
still facing issues
// this is my entity class
import androidx.room.Entity
import androidx.room.PrimaryKey
#Entity(tableName = "To_do")
data class Entity(
#PrimaryKey(autoGenerate = true)
var id: Int,
var title: String,
var priority: String
)

Related

Duplicate Class Error in kotlin gradle implementation

I get duplicate class errors as gradle implementation app's build.gradle file. But I don't know which implementation is actually a duplicate. How can i resolve this? Is there a better way like using bom for the dependencies?
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.bettehomes'
compileSdk 33
defaultConfig {
applicationId "com.example.bettehomes"
minSdk 25
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
// Import the Compose BOM
implementation 'com.google.android.gms:play-services-maps:18.1.0'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.7.0-alpha04'
implementation "androidx.compose.ui:ui-tooling:1.3.3"
implementation "androidx.compose.ui:ui-tooling-preview:1.3.3"
implementation 'androidx.compose.material3:material3:1.1.0-alpha04'
// Android Studio Preview Support
debugImplementation "androidx.compose.ui:ui-tooling:1.3.3"
//testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.4.0-alpha05"
debugImplementation "androidx.compose.ui:ui-test-manifest:1.3.3"
}
I had this issue today and used gradle dependencies to figure out the dependency tree
I figured out that a lib used a latest version of appcompat which used kotlin 1.8.0, a version that has this issue.
In my case the fix was to add the kotlinVersion manually:
buildscript {
ext {
buildToolsVersion = "32.0.0"
minSdkVersion = 21
compileSdkVersion = 33
targetSdkVersion = 32
androidXBrowser = "1.4.0"
kotlinVersion = "1.8.0"
}
...
}
For more information check this issue: https://github.com/proyecto26/react-native-inappbrowser/issues/398

android Jetpack Room Export schemas failed

I refer to setting Export schemas in the documentation, But it failed, the error is as follows:
The following is my Gradle configuration:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
// hilt
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
}
android {
namespace 'com.freedom.android'
compileSdk 32
defaultConfig {
applicationId "com.freedom.android"
minSdk 26
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
// Room
javaCompileOptions {
annotationProcessorOptions {
compilerArgumentProviders(
new RoomSchemaArgProvider(new File(projectDir, "schemas"))
)
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.1.1'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
applicationVariants.all { variant ->
variant.getRuntimeConfiguration().exclude group: 'com.google.code.findbugs', module: 'jsr305'
}
}
dependencies {
/**
* Room
*/
def room_version = "2.4.3"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// To use Kotlin annotation processing tool (kapt)
kapt "androidx.room:room-compiler:$room_version"
// To use Coroutine features, you must add `ktx` artifact from Room as a dependency. androidx.room:room-ktx:<version>
implementation "androidx.room:room-ktx:$room_version"
// Could not find method ksp() for arguments [androidx.room:room-compiler:2.4.3] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
// To use Kotlin Symbol Processing (KSP)
// ksp "androidx.room:room-compiler:$room_version"
// hilt
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")
def lifecycle_viewmodel_version = "2.5.1"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_viewmodel_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_viewmodel_version"
// private val articleViewModel: ArticleViewModel by viewModels()
// java.lang.IllegalArgumentException: CreationExtras must have a value by `SAVED_STATE_REGISTRY_OWNER_KEY`
implementation "androidx.fragment:fragment-ktx:1.5.1"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
implementation 'androidx.compose.material:material:1.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
}
// hilt
// Allow references to generated code
kapt {
correctErrorTypes = true
}
// Room
class RoomSchemaArgProvider implements CommandLineArgumentProvider {
#InputDirectory
#PathSensitive(PathSensitivity.RELATIVE)
File schemaDir
RoomSchemaArgProvider(File schemaDir) {
this.schemaDir = schemaDir
}
#Override
Iterable<String> asArguments() {
// Note: If you're using KSP, you should change the line below to return
// ["room.schemaLocation=${schemaDir.path}"]
return ["-Aroom.schemaLocation=${schemaDir.path}"]
}
}
I am basically sure that it is an error caused by setting Export schemas. When I set exportSchema = false, the program can run normally. Sorry my settings file is a bit messy, but like I said, it works.
Convert string like this:
return ["-Aroom.schemaLocation=${schemaDir.path}".toString()]
About bug here:
https://github.com/gradle/gradle/issues/4730#

I'm trying to get stock prices from Yahoo Finance API, getting an java.io.FileNotFoundException

I'm trying to get stock prices from Yahoo Finance,
but I'm getting an error. This was the code I wrote before, and it worked well before, but after a long time, it doesn't work, returning the following error code.
java.io.FileNotFoundException while trying to execute YahooFinance.get operation
I found a question similar to my problem, and I tried to update Java according to the comments to correct the Java version,but the problem was not resolved.
When I click the URL given with the error code, it seems to get the data from Yahoo Finance, but it doesn't work programmatically.
package com.jym.yftest
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.jym.yftest.databinding.ActivityMainBinding
import yahoofinance.YahooFinance
class MainActivity : AppCompatActivity() {
private val vBinding by lazy { ActivityMainBinding.inflate(layoutInflater)}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(vBinding.root)
vBinding.testBtn.setOnClickListener {
Thread(Runnable {
try{
val stock = YahooFinance.get("APL")
Log.d("test", stock.toString())
}catch(err:Exception){
Log.d("test", err.toString())
}
this#MainActivity.runOnUiThread(java.lang.Runnable {
})
}).start()
}
}
}
build.gradle(module)
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.jym.yftest"
minSdk 22
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
buildFeatures{
viewBinding true
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}
build.gradle(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

Jetpack Compose - unresolved refence when using viewmodel value in LazyColumn

For my college project I want to use data from Firestore, and then use MVVM pattern to display the items on the Lazy Column.
Here is the code of the screen of where I want the values of viewmodel to be displayed:
fun HomeScreen (navController: NavController, viewModel: ArticleViewModel = hiltViewModel()){
var expanded = remember { mutableStateOf(false) }
val articleResponse = viewModel.articleState.value
Scaffold() {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.background(Color.White)
) {
items(items = articleResponse.data) {
ArticleList(model = it) { model ->
Log.d("TAG", "Clicked on $model")
navController.navigate(route = NavScreens.DetailScreen.name +"/$model")
}
}
}
}
}
Here is the field of where exactly I get the error that is I mentioned in the title:
here
Previously I used an array to display the item in the LazyColumn and it worked normally.
Perhaps, the problem lies in the ViewModel? Here is the ViewModel that I'm using:
#HiltViewModel
class ArticleViewModel #Inject constructor(
private val useCases: UseCases
):ViewModel() {
private val _articleState = mutableStateOf<Response<List<Article>>>(Response.Loading)
val articleState: State<Response<List<Article>>> = _articleState
init {
getArticles()
}
private fun getArticles() {
viewModelScope.launch {
useCases.getArticle().collect { response ->
_articleState.value = response
}
}
}
}
When I tried searching for the solution, I've read that the problem could be with the dependencies/plugins, I have tried changing couple of the dependencies but the issue still persists. I've heard that some of the plugins doesn't work with others since I'm new to jetpack compose it is harder for me to identify the issues within the dependencies.
Here is my Gradle files:
buildscript {
ext {
compose_version = '1.0.1'
hilt_version = "2.40.5"
gradle_version = "7.2.0"
kotlin_version = "1.6.20"
coroutines_version = "1.6.1"
google_services_version = "4.3.10"
hilt_version = "2.40.5"
core_version = "1.7.0"
appcompat_version = "1.4.1"
live_data_version = "2.4.1"
activity_compose_version = "1.4.0"
material_version = "1.6.0"
hilt_navigation_compose_version = "1.0.0"
firebase_bom_version = "30.0.0"
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.4'
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
classpath "com.android.tools.build:gradle:$gradle_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.gms:google-services:$google_services_version"
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'org.jetbrains.kotlin.android' version '1.5.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
id 'dagger.hilt.android.plugin'
id 'kotlin-kapt'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.flow"
minSdk 28
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerVersion '1.5.21'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation "androidx.compose.compiler:compiler:1.0.0-beta08"
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
implementation "androidx.appcompat:appcompat:$appcompat_version"
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.core:core-splashscreen:1.0.0-beta02"
implementation "androidx.navigation:navigation-compose:2.4.0-alpha10"
implementation("io.coil-kt:coil-compose:2.1.0")
implementation "com.google.firebase:firebase-firestore:22.0.1"
implementation "com.google.dagger:hilt-android:$hilt_version"
implementation platform("com.google.firebase:firebase-bom:$firebase_bom_version")
implementation "com.google.firebase:firebase-firestore-ktx"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation "androidx.hilt:hilt-navigation-compose:$hilt_navigation_compose_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutines_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$live_data_version"
}
I would be grateful if you are able to assist me on finding the solution.
The problem is that articleState.value is Response not List. You can do something like that:
val articleResponse = viewModel.articleState.value
when (articleResponse) {
Response.Loading -> <LoadingComponenet />
Response.Error -> <ErrorComponent />
Response.Success -> Scaffold() {
.............
}
}

Android Kotlin Two-Way databinding BR class error

I'am trying to do a basic android application using kotlin and two way data binding to show on a text view the changes in a edit text. But i'am getting this error :
w: [kapt] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: android.databinding.annotationprocessor.ProcessDataBinding (DYNAMIC).
when i'am trying to do with one way data binding it is working well it show me the Contact class's name field successful
my codes:
build.gradle(Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(module:app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
dataBinding{
enabled = true
}
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.mvvm.demo"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
my Contact.kt
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
class Contact( var Id:Int,
var Name:String,
var Email:String):BaseObservable() {
#Bindable
var _name: String = Name
set(value) {
field = value
notifyPropertyChanged(BR._name)
}
get() = field
}
unfortunately, this error has not yet been resolved. I hope the warning will be resolved in the next update. I've been waiting for updates since November because of this warning
source: https://issuetracker.google.com/issues/110061530#comment28