Android Room - Unresolved reference: onConflictStrategy - android

Dao Interface Image
In this in the line
#Insert(onConflict = onConflictStrategy.IGNORE)
android studio says
Unresolved reference: onConflictStrategy
and in the line
#Query("Select * from notes_table order by id ASC")
The whole string is of green color while the words Select, from, order by and ASC should be orange
Here is my build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
namespace 'com.example.scribbles'
compileSdk 33
defaultConfig {
applicationId "com.example.scribbles"
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
}
packagingOptions {
exclude 'META-INF/atomicfu.kotlin_module'
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "androidx.appcompat:appcompat:1.6.1"
implementation "androidx.activity:activity-ktx:1.6.1"
// Dependencies for working with Architecture components
// You'll probably have to update the version numbers in build.gradle (Project)
// Room components
implementation "androidx.room:room-runtime:2.5.0"
implementation "androidx.room:room-ktx:2.5.0"
kapt "androidx.room:room-compiler:2.5.0"
androidTestImplementation "androidx.room:room-testing:2.5.0"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1"
implementation "androidx.lifecycle:lifecycle-common-java8:2.5.1"
// Kotlin components
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
// UI
implementation "androidx.constraintlayout:constraintlayout:2.1.4"
implementation "com.google.android.material:material:1.8.0"
// Testing
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation ("androidx.test.espresso:espresso-core:3.4.0", {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation "androidx.test.ext:junit:1.1.5"
}
package com.example.scribbles
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
#Entity(tableName = "notes_table")
class Note(#ColumnInfo(name = "text")val text:String) {
#PrimaryKey(autoGenerate = true) var id = 0
}
I checked the dependencies version are up to date and imported androidx.room.OnconflictStrategy
but it was saying unused import directive.

Change onConflictStrategy to OnConflictStrategy and yo'll be fine.

Related

How to use Hilt in Jetpack Compose?

I'm trying to create a school project with Jetpack Compose and I try to use Hilt. These are the dependencies I'm using:
dependencies {
implementation platform("androidx.compose:compose-bom:2023.01.00")
implementation "androidx.compose.material:material"
implementation "com.google.dagger:hilt-android:2.45"
kapt "com.google.dagger:hilt-android-compiler:2.45"
}
But when I try to compile the app I get:
Unresolved reference: compose
I read here, that I should implement androidx.hilt:hilt-navigation-compose why? I'm not using any navigation. It's a simple app with one activity that contains a LazyColumn with three names. How to solve that?
Make sure that your added these dependencies in your app level build.gradle file:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.google.dagger.hilt.android'
id 'kotlin-kapt'
}
android {
namespace 'garousi.dev.stackoverflow'
compileSdk 33
defaultConfig {
applicationId "garousi.dev.stackoverflow"
minSdk 21
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 '1.4.2'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.6.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
def composeBom = platform('androidx.compose:compose-bom:2023.01.00')
implementation composeBom
androidTestImplementation composeBom
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation 'androidx.compose.material:material'
debugImplementation "androidx.compose.ui:ui-tooling"
androidTestImplementation "androidx.compose.ui:ui-test-junit4"
debugImplementation "androidx.compose.ui:ui-test-manifest"
implementation 'com.google.dagger:hilt-android:2.45'
kapt 'com.google.dagger:hilt-compiler:2.45'
implementation "androidx.navigation:navigation-compose:2.5.3"
}
kapt {
correctErrorTypes true
}
your project level build.gradle file:
buildscript {
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
id 'com.google.dagger.hilt.android' version '2.45' apply false
}

Build Gradle Errors - API data issue - Kotlin - Android

Please help!
defaultConfig, buildTypes, and composeOptions are throwing these errors:
public open val defaultConfig: [Error type: Unresolved type for DefaultConfig]
public open val buildTypes: [Error type: Unresolved type for NamedDomainObjectContainer ]<[Error type: Unresolved type for
BuildType]>
public open val composeOptions: [Error type: Unresolved type for ComposeOptions]
I upgraded from android chipmunk to dolphin and I noticed this. I am not getting any errors in my application but it takes forever for the api data to load. It wasn't doing this before.
Also, in the Logcat this warning is thrown repeatedly until the api data is loaded.
-> presentDisplay display has no layers to compose, flushing client target buffer.
I have tried to find info on this, but nothing I have found has helped my situation. Any help or advice you may have is welcomed and appreciated.
Module build.gradle:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.samm.brewerysearch"
minSdk 21
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}'
}
}
namespace 'com.samm.brewerysearch'
}
dependencies {
implementation 'androidx.core:core-ktx:1.8.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.5.1'
implementation 'androidx.activity:activity-compose:1.5.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_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
implementation 'com.google.android.material:material:1.6.1'
//Navigation
implementation "androidx.hilt:hilt-navigation-compose:1.0.0"
// retrofit
implementation "com.squareup.retrofit2:retrofit:2.9.0"
// GSON
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
// coroutine
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
// hilt
implementation "com.google.dagger:hilt-android:2.42"
kapt "com.google.dagger:hilt-compiler:2.42"
}
Project build.gradle:
buildscript {
ext {
compose_version = '1.2.0-beta01'
}
dependencies {
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'
classpath 'com.android.tools.build:gradle:7.3.0'
}
}
plugins {
id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
id 'com.google.dagger.hilt.android' version '2.41' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}

android datastore-preferences: Property delegate must have a 'getValue(Context, KProperty<*>)' method

I'm writing a jetpack compose android app, I need to store some settings permanently.
I decided to use androidx.datastore:datastore-preferences:1.0.0 library, I have added this to my classpath.
According to the https://developer.android.com/topic/libraries/architecture/datastore descripton I have added this line of code to my kotlin file at the top level:
val Context.prefsDataStore: DataStore by preferencesDataStore(name = "settings")
But I get a compile error:
e: ...SettingsViewModel.kt: (13, 50): Property delegate must have a 'getValue(Context, KProperty<*>)' method. None of the following functions is suitable:
public abstract operator fun getValue(thisRef: Context, property: KProperty<*>): DataStore<Preferences> defined in kotlin.properties.ReadOnlyProperty
How can I use the datastore-preferences?
My build.gradle file:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlinx-serialization'
android {
compileSdk 31
defaultConfig {
applicationId "hu.homedashboard.mobile"
minSdk 22
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'
useIR = true
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerVersion "$kotlinVersion"
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation "androidx.activity:activity-compose:1.3.1"
implementation "androidx.appcompat:appcompat:1.3.1"
implementation "androidx.datastore:datastore-preferences:1.0.0"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "com.google.accompanist:accompanist-swiperefresh:0.20.3"
implementation "androidx.core:core-ktx:1.6.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.navigation:navigation-compose:2.4.0-alpha10"
implementation "com.google.android.material:material:1.4.0"
implementation "com.google.dagger:hilt-android:2.40.1"
implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0"
implementation "org.ocpsoft.prettytime:prettytime:5.0.2.Final"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'
kapt "com.google.dagger:hilt-compiler:2.38.1"
kapt "com.google.dagger:dagger-android-processor:2.40.1"
kapt "com.google.guava:guava:31.0.1-android"
api "com.google.guava:guava:31.0.1-android"
testImplementation 'junit:junit:4.+'
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"
}
kapt {
correctErrorTypes true
javacOptions {
option("-Xmaxerrs", 500)
}
}
I got this error because of an incorrect import:
import java.util.prefs.Preferences
So fix it by
import androidx.datastore.preferences.core.Preferences
or
val Context.dataStore by preferencesDataStore(name = "settings")

Unresolved reference: launch in viewModelScope

I am trying to call a suspend function using viewModelScope.launch in which "launch" is showing as Unresolved reference.
Here is build.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.mynotes"
minSdk 27
targetSdk 31
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'
}
packagingOptions {
exclude 'META-INF/atomicfu.kotlin_module'
}
}
dependencies {
implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
implementation "androidx.activity:activity-ktx:$rootProject.activityVersion"
// Dependencies for working with Architecture components
// You'll probably have to update the version numbers in build.gradle (Project)
// Room components
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion"
// Kotlin components
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
// UI
implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
implementation "com.google.android.material:material:$rootProject.materialVersion"
// Testing
testImplementation "junit:junit:$rootProject.junitVersion"
androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
androidTestImplementation ("androidx.test.espresso:espresso-core:$rootProject.espressoVersion", {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation "androidx.test.ext:junit:$rootProject.androidxJunitVersion"
}
and the ViewModel.kt
package com.example.mynotes
import android.app.Application
//import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.*
class NotesViewModel(application: Application) : ViewModel() {
private val repository : NotesRepository
val allNotes : LiveData<List<Notes>>
init {
val dao = NotesDatabase.getDatabase(application).getNotesDao()
repository = NotesRepository(dao)
allNotes = repository.allNotes
}
fun deleteNotes(notes: Notes) = viewModelScope.launch(Dispatchers.IO) {
repository.delete(notes)
}
}
In the function deleteNotes "viewModelScope.launch" is showing Unresolved reference: launch but there is no error while building the app but my app crash while running.
I think it's because of the way you are declaring the variable, since you are declaring it as a direct function, you can try the following:
fun deleteNotes(notes: Notes) {
viewModelScope.launch(Dispatchers.IO) {
repository.delete(notes)
}
}
It's possible the issue is related that when the method is declared and stored in memory, the scope hasnt been assigned yet.
Which version are you using?
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
Try to use this case is different.
Try to invalide cache and restart if still not recognized

Strange static method error for normal method invocation in minimal Android app

I get a strange runtime exception when running some very simple code (8-line method; only 2 lines relevant) on my Android 10 device, after compiling with Android Studio Canary 7 (or 6) using Jetpack Compose alpha12.
Error:
java.lang.NoSuchMethodError: No static method copy-0d7_KjU$default(JFFFFILjava/lang/Object;)J in class Landroidx/compose/ui/graphics/Color; or its super classes (declaration of 'androidx.compose.ui.graphics.Color' appears in /data/app/com.example.myapplication-GbQdisqKhWdQawA6_DsPkQ==/base.apk)
at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:15)
at android.app.Activity.performCreate(Activity.java:7955)
Code:
package com.example.myapplication
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.activity.compose.setContent
import androidx.compose.ui.graphics.Color
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val x = Color(0xFFBB86FC)
val y = x.copy() // ******* line 15 *******
setContent {
Surface(color = MaterialTheme.colors.background) {
Text(text = "hello world")
}
}
}
}
Top-level build.gradle:
buildscript {
ext {
compose_version = '1.0.0-alpha12'
}
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0-alpha07"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30-RC"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module level build.gradle:
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 29
targetSdkVersion 30
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'
useIR = true
freeCompilerArgs += [
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
]
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
The culprit is the line classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30-RC" which should instead be 1.4.30 without the -RC. The reason I had the initial -RC version was because the latest Android Studio Kotlin plugin version is indeed the -RC (frustrating that IntelliJ doesn't release the correct version even though it seems to be available in gradle), and using 1.4.30 gave a warning that it was different from the IDE version. Ignoring that warning and using the release version fixed that problem.
Sigh!

Categories

Resources