Android ViewModel with appCompat v27.0.0 - android

I am trying to use the Android architecture components ViewModel in a project with the latest com.android.support:appcompat-v7:27.0.0. The appcompat already contains everything from android.arch.lifecycle:runtime:1.0.3 and android.arch.lifecycle:compiler:1.0.0. However it does not seem to contain for example android.arch.lifecycle:extensions:1.0.0which includes the ViewModel class. There still does not seem to be a version 1.0.3 of the extensions but if I try using 1.0.0 I get the following error:
Error:Execution failed for task ':app:preReleaseBuild'.
Android dependency 'android.arch.lifecycle:runtime' has different version for the compile (1.0.0) and runtime (1.0.3) classpath. You should manually set the same version via DependencyResolution
I can fix this like that:
implementation ("android.arch.lifecycle:extensions:1.0.0"){
exclude module: "runtime"
}
but this can not be the final solution. What am I missing?

LiveData , ViewModel , Room and Paging Library all of these libraries are part of android architecture components . None of these library are part of support library .
To add architecture components as a dependency on your gradle file you need to add them separately and their associate annotation processor like this
// Architecture Components
compile 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
compile 'android.arch.lifecycle:extensions:1.0.0'
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0'
for detail documentation please see the official documentation adding architecture components to your project

Related

error: package android.arch.lifecycle does not exist

I am getting error: package android.arch.lifecycle does not exist and i tried to resolve it,
i used below links : but none of these resolve it:
Failed to resolve: android.arch.lifecycle:extensions:1.0.0-alpha1 android studio 3.0
Could not resolve android.arch.lifecycle:extensions:1.1.0
i have below dependencies :
// Room components
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"
// Lifecycle components
implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"
Recently I got this link:
https://developer.android.com/jetpack/androidx/releases/lifecycle#java
which says : The APIs in lifecycle-extensions have been deprecated. Instead, add dependencies for the specific Lifecycle artifacts you need.
Does this mean now I need to use individual packages?
The android.arch packages have been deprecated, try using androidx packages instead. Also make sure you've added google() in your build.gradle.
Finally i got the answer.
android.support is deprecated , i migrated to androidx via following links:
https://medium.com/androiddevelopers/migrating-to-androidx-tip-tricks-and-guidance-88d5de238876
https://developer.android.com/jetpack/androidx/migrate
And at last if you get error like buttler knife/ (or any third party lib) not resolved, find android x compatible version
i used:
// butter knife
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'

Duplicate class kotlin classes kotlin version 1.3.70

Error while adding MoshiPack Library in Kotlin latest version 1.3.70 to gradle.build application
Moshi pack
implementation 'com.daveanthonythomas.moshipack:moshipack:1.0.1'
Error Message
Duplicate class kotlin.reflect.KClasses found in modules jetified-kotlin-reflect-1.1.1.jar (org.jetbrains.kotlin:kotlin-reflect:1.1.1) and jetified-kotlin-stdlib-1.3.70.jar (org.jetbrains.kotlin:kotlin-stdlib:1.3.70)
Any suggestions how to solve this issue or any other library I can use in Kotlin so I can use Message Pack.
Thanks in advance
Try to add this to your dependencies:
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
and make sure you specified your Android NDK location under File>Project Structure...>SDK Location
Starting Kotlin 1.3.70 some basic useful members on KClass included in Kotlin standard library (they were in a kotlin-reflect before).
See "Working with KClass" in https://blog.jetbrains.com/kotlin/2020/03/kotlin-1-3-70-released/
In your case MoshiPack adds a kotlin-reflect library that conflicts with standard library.
You should exclude transitive dependency to resolve the conflict.
If you use KotlinDSL, in build.gradle.kts:
implementation ("com.daveanthonythomas.moshipack:moshipack:1.0.1") {
exclude(group = "org.jetbrains.kotlin", module = "kotlin-reflect")
}
If you use Groovy, in build.gradle:
implementation ('com.daveanthonythomas.moshipack:moshipack:1.0.1') {
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-reflect'
}
I tried this and it worked
implementation "org.jetbrains.kotlin:kotlin-reflect:1.4.10"
i think the only way to solve it , to go back to kotlin version 1.3.61 , so remove 1.3.70 and use 1.3.61
So I finally figured it out and here's how :-
So the main problem is you have injected 2 dependency of same class. Here he used 2 dependency for Kotlin which conflict in runtime to fix that you have to check which dependency is duplicated. (It is most case scenario. It can be with any other dependency i.e. Hilt)
Go to File > Project structure > Dependencies
Check which dependency are repeating. In this case it will have (androidx.core:core:1.8.0) and (androidx.core:core:+)
as you can see there are 2 dependency with same classes version 1.8.0 will have all the class which core:+ will have and this is causing a error.
Now delete (androidx.core:core:+) and hit Apply and sync project.
Now you should be good to go. here is structure after changing dependency.
Note:- This method will show all the android dependency which you might be not included but you will see all the dependency which any app has. Please remove the dependency who are you familiar with do not remove any dependency without any proper knowledge.

How to get the latest Room version?

Would you know where to get the latest version for Room ?
On this Android Studio page, it is quoted that the latest Room version is 2.1.0-alpha3 but when I put this version in my build.gradle file, the project cannot compile and the following error is shown :
ERROR: Failed to resolve: android.arch.persistence.room:runtime:2.1.0-alpha3
Show in Project Structure dialog
Affected Modules: app
it is quoted that the latest Room version is 2.1.0-alpha3
It is.
when I put this version in my build.gradle file, the project cannot compile and the following error is shown
Your version is fine. Your artifact is the problem.
All new libraries are AndroidX. Your choices are:
Stick with whatever version you are using right now and keep your android.arch.persistence.room:runtime artifact and its classes, or
Migrate to AndroidX, in which case you would use androidx.room:room-runtime as the artifact, and need to change your code to reference the androidx classes for Room and everything else
The thing is, if you'r using AndroidX then the latest is 2.1.0-alpha3, or, if you are using Support libraries then it's 1.1.1 or whatever is latest and that's what mentioned in the documentation. As mentioned by CommonsWare, it's the artifact, and to find out the mappings, see this mapping documentation. And AndroidX is:
The AndroidX library contains the existing support library and also
includes the latest Jetpack components.
Example using support libraries:
dependencies {
// Other libraries...
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
}
The solution is to check AndroidX when starting a new project in Android Studio, then in the App Module Build.Gradle file the following works perfectly :
def room_version = "2.1.0-alpha03"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version" // use kapt for Kotlin

WorkManager: Dependency Issue with Kotlin

I'm getting the following error while trying to use Work Manager. I've migrated my project to AndroidX and all other architecture components are working.
def work_version = "1.0.0-alpha02"
/* Work Manager for Background Tasks */
implementation "android.arch.work:work-runtime:$work_version"
implementation "android.arch.work:work-firebase:$work_version"
I'm quite sure I need some dependencies from the Support Library. But I have no clue which one's they are.
I've tried adding the annotations package since the error says it can't find a class file for RestrictTo$Scope. Still doesn't work.
implementation "com.android.support:support-annotations:28.0.0-alpha1"
You are including a reference to the wrong support library, you want the androidx one.
androidx.annotation
Look in
AndroidX refactoring
you will find
androidx.annotation:annotation:1.0.0-alpha1
to correspond with
com.android.support:support-annotations
Make sure that you have only libraries for one or the other kind in your dependencies (project wide).

Android Architecture Components: Gradle sync error for dependency version

I'm trying to add ViewModel and LiveData to a Kotlin app. I have the following dependencies added to my module's build.gradle:
implementation "android.arch.lifecycle:extensions:1.1.1"
kapt "android.arch.lifecycle:compiler:1.1.1"
testImplementation "android.arch.core:core-testing:1.1.1"
I'm given the following error:
Android dependency 'android.arch.lifecycle:runtime' has different version for the compile (1.0.0) and runtime (1.1.1) classpath. You should manually set the same version via DependencyResolution
Removing the first line (extensions) fixes the issue, indicating that the error is coming from there, but I can't figure out why.
As #RedBassett mentions Support libraries depends on this lightweight import (runtime library) as explained at android developers documentation.
This is, android.arch.lifecycle:runtime:1.0.0 is spreading up in the dependency tree as a result of an internal api (transitive) import so in my case I only had to include extensions library as "api" instead of "implementation" so that it will override its version to the highest (1.1.1).
In conclusion, change
implementation "android.arch.lifecycle:extensions:1.1.1"
to
api "android.arch.lifecycle:extensions:1.1.1"
In your main build.gradle file
allprojects {
...
configurations {
all {
resolutionStrategy {
force "android.arch.lifecycle:runtime:1.1.1"
}
}
}
}
This will enforce version 1.1.1
Apparently support-v4 was causing the conflict. In the case of this question, the Gradle dependency task wasn't working correctly, but for anyone else who runs into this issue:
./gradlew :app:dependencies will show the sub-dependencies used by your dependencies. Search the output of this command (changing app for your module name) for the dependency causing the conflict.
#RedBassett is right. However I was still having some problem excluding android.arch.lifecycle related sub dependencies.
In my case the conflict was caused in com.android.support:appcompat-v7:27.1.1.
This is how my gradle dependency looks like after excluding it.
implementation ('com.android.support:appcompat-v7:27.1.1') {
exclude group: 'android.arch.lifecycle'
}
api "android.arch.lifecycle:runtime:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
Also, you will have to add this exclude in every imported module.
I searched for all dependencies with ./gradlew :app:dependencies as #RedBassett mentioned. I noticed the incompatible version of android.arch.core:runtime that Gradle was complaining about was stemming from my version of com.android.support:appcompat-v7, so I just updated that version to the latest and everything worked.

Categories

Resources