This question already has answers here:
ViewModel in Kotlin: Unresolved Reference
(12 answers)
Closed 2 years ago.
I just started an Android App and I want to use ViewModel. I looked into documentation and I put all the dependencies from Lifecycle but when i try to instantiate a view model like this viewMyViewModel by viewModels() i get this error 'Unresolved reference: viewModels()'.
These are the dependencies:
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.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"
implementation "android.arch.lifecycle:extensions:1.1.1"
kapt "android.arch.lifecycle:compiler:1.1.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version"
testImplementation "androidx.arch.core:core-testing:$arch_version"
}
Include this in your dependency,
implementation "android.arch.lifecycle:extensions:1.1.1"
then, add apply plugin: 'kotlin-kapt' in build.gradle
then add,
kapt "android.arch.lifecycle:compiler:1.1.1"
Replace other annotationProcessor with kapt
See if it works or not, if not, then go to
file -> invalidate cache and restart
Related
I am using a number of DialogFraments in my Android app. To set up, I have imported "androidx.fragment.app.DialogFragment;" into each DialogFragment class. I have not added the Fragment dependency to the app's build.gradle file and the app is stable, working fine and the DialogFragments load and dismiss as expected.
Since DialogFragment extends Fragment though, should I be adding the below AndroidX Fragment library into the project's build.gradle file? If yes, what is the benefit of adding this dependency since the app is working fine now without it?
build.gradle
dependencies {
def fragment_version = "1.5.5"
implementation "androidx.fragment:fragment:$fragment_version"
}
Existing build.gradle (:app)
dependencies {
def room_version = "2.4.3"
def lifecycle_version = "2.2.0"
def work_version = "2.7.1"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.core:core:1.9.0'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:2.5.1"
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "androidx.work:work-runtime:$work_version"
}
If you already use fragments you apparently already have dependency that supports it, so no need to add another one.
You can post your gradle file so we can see which dependencies you already have.
I am new to android kotlin and I am using viewModel in my OCR App."import androidx.fragment.app.viewModels" is not showing.I have added all the neccessary dependencies.What am doing wrong ?
Here are the dependencies.
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.google.android.material:material:1.2.1'
//lottie animation
implementation 'com.airbnb.android:lottie:3.4.1'
// Add CameraX dependencies
def camerax_version = "1.0.0-beta05"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:1.0.0-alpha12"
// Add ML Kit dependencies
implementation 'com.google.android.gms:play-services-mlkit-text-recognition:16.0.0'
implementation 'com.google.mlkit:language-id:16.0.0'
implementation 'com.google.mlkit:translate:16.0.0'
}
And these is the import error.
Add this dependency to use by viewModels() in a Fragment:
implementation 'androidx.fragment:fragment-ktx:1.2.5'
After refactoring dependencies to androidx dependencies with android studio I keep getting errors when building. I looks like the application still uses the old depedencies, but I have tried to clean projekt and rebuild project... nothing helped.
Below is the dependencies I use in my application.
def retrofit_version = "2.4.0"
def glide_version = "4.3.1"
implementation 'androidx.appcompat:appcompat:1.0.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'
// Design Support
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable-animated:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation 'androidx.paging:paging-runtime:2.0.0'
implementation "com.github.bumptech.glide:glide:$glide_version"
annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.0.0'
implementation 'androidx.preference:preference:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Why in my APK there is GSON even though I have nothing to implement GSON in Gradle?
Result Analyze APK
My Gradle :
//main
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.anko:anko-commons:0.10.5"
implementation "androidx.appcompat:appcompat:1.1.0"
implementation "androidx.constraintlayout:constraintlayout:1.1.3"
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.paging:paging-runtime:2.1.1"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.multidex:multidex:2.0.1"
implementation "com.android.volley:volley:1.1.1"
//google
implementation "com.google.android.gms:play-services-location:17.0.0"
implementation "com.google.android.material:material:1.0.0"
implementation "com.google.firebase:firebase-auth:19.2.0"
implementation "com.google.firebase:firebase-firestore:21.3.1"
implementation "com.google.firebase:firebase-storage:19.1.0"
implementation "com.google.firebase:firebase-messaging:20.1.0"
implementation "com.firebaseui:firebase-ui-storage:3.3.0"
// third party dependencies
implementation "com.balysv:material-ripple:1.0.2"
implementation "com.facebook.shimmer:shimmer:0.5.0"
implementation "com.squareup.moshi:moshi-kotlin:1.9.2"
implementation "com.tbuonomo.andrui:viewpagerdotsindicator:4.1.2"
implementation "com.github.bumptech.glide:glide:$glide_version"
//noinspection GradleDependency
implementation "com.journeyapps:zxing-android-embedded:3.6.0"
//kapt
kapt "com.github.bumptech.glide:compiler:$glide_version"
kapt "androidx.room:room-compiler:$room_version"
kapt "androidx.annotation:annotation:1.1.0"
// test
testImplementation 'junit:junit:4.13'
androidTestImplementation "androidx.test:runner:1.2.0"
androidTestImplementation "androidx.test.espresso:espresso-core:3.2.0"
I want to reduce my apk size, so I deleted the libary that I didn't use.
One or more of your dependencies has GSON in self.
i think it might be Glide or someone else.
for redecuing apk size check this topic might be helpful: Reduce the APK size - Android Developers
I have made the following changes to my build.gradle(app) file.
apply plugin: 'kotlin-kapt'
and
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha05'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha07'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha01'
kapt 'androidx.lifecycle:lifecycle-compiler:2.0.0-rc01'
implementation 'androidx.room:room-runtime:2.1.0-rc01'
kapt 'androidx.room:room-compiler:2.0.0-rc01'
implementation 'androidx.paging:paging-runtime:2.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'io.sentry:sentry-android:1.7.16'
implementation 'org.slf4j:slf4j-nop:1.7.25'
}
And now the Kotlin compiler is throwing the following error about my EntryDAO.kt and EntryDatabase.kt files:
The error message is quite clear.
The room’s Delete, Update methods signature has changed. It needs to return void or int.
Edit: I checked again, your gradle file mixed androidx library and pre-AndroidX versions of Lifecycle.
Please follow the link to use the correct library combination: https://developer.android.com/jetpack/androidx/releases/lifecycle