Android Architecture Components: Gradle sync error for dependency version - android

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.

Related

Android Gradle - transitive dependencies take precedence (upgrade) the declared (first-level) one

What I have in my build.gradle:
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation 'com.github.kirich1409:viewbindingpropertydelegate-noreflection:1.5.6'
The latter dependency has its own RecyclerView transitive dependency:
implementation "androidx.recyclerview:recyclerview:1.2.1"
What I see then in the dependency tree is (for the RecyclerView dependency I declared):
+--- androidx.recyclerview:recyclerview:1.1.0 -> 1.2.1
Also, in the code, I got the getBindingAdapterPosition method, which appeared after 1.1.0.
Certainly, we should always try to use the latest version of the dependencies, but is it the correct behavior?!
I thought that transitive dependencies wouldn't override the declared ones.
What if my code relies on some functionality present in version 1.1.0 only?
The default behaviour is described in the doc:
Gradle resolves any dependency version conflicts by selecting the latest version found in the dependency graph.
You can enforce a certain version of a transitive dependency, excluding a dependency. Something like:
implementation('commons-beanutils:commons-beanutils:1.9.4') {
exclude group: 'commons-collections', module: 'commons-collections'
}
but please note that:
Excluding a dependency completely requires a conscious decision. Excluding a transitive dependency might lead to runtime errors if external libraries do not properly function without them. If you use excludes, make sure that you do not utilise any code path requiring the excluded dependency by sufficient test coverage.
You can also use a strict versions even if a transitive dependency says otherwise. Something like:
implementation('commons-codec:commons-codec') {
version {
strictly '1.9'
}
}
Also in this case note that:
Forcing a version of a dependency requires a conscious decision. Changing the version of a transitive dependency might lead to runtime errors if external libraries do not properly function without them.

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 declare annotation processor witch is inside of gradle module

I have modules in my Android project. One of modules (moduleA for example) use another (moduleB) as a dependency:
dependencies {
api project(':moduleB')
}
And also I still have apt and old gradle plagin. Now, I delete apt from gradle files, upgrade plugin version and get error:
Annotation processors must be explicitly declared now. The following
dependencies on the compile classpath are found to contain annotation
processor. Please add them to the annotationProcessor configuration.
- moduleB.jar (project :moduleB)
If we use some external dependency we do like this (for example) to resolve this problem:
compile 'com.google.dagger:dagger:2.8'
annotationProcessor 'com.google.dagger:dagger-compiler:2.8'
But what I need to do with my case, when annotattion processor is inside of gradle module? Could you explain it, because I don't have deep understanding in this area and usually I simply take this lines
compile 'com.google.dagger:dagger:2.8'
annotationProcessor 'com.google.dagger:dagger-compiler:2.8'
from repository of library provider. I try to research this case, but don't find anything similar.
project(':moduleB') is just a way to specify an inter-project dependencies, like you do with "full" coordinates (com.google.dagger:dagger:2.8). So, to use another module as an annotation processor just use
dependencies {
…
annotationProcessor(project(":moduleB"))
…
}

Library dependency issues with gradle 3.0.0-alphaX

I have a library project with submodules that include many dependencies that I'd like to pass to the developer's application. For example, module A may include all the necessary appcompat dependencies.
With the migration changes, I've updated all compile cases to api, which should not affect anything. However, I no longer have access to any of the libraries dependencies. I can only use code and references from my library itself.
Is there any way around this?
One of the build gradle files of my library submodules can be found here for reference.
The dependencies:
dependencies {
api "org.jetbrains.kotlin:kotlin-stdlib:${KOTLIN}"
api "com.android.support:appcompat-v7:${ANDROID_SUPPORT_LIBS}"
api "com.android.support:support-v13:${ANDROID_SUPPORT_LIBS}"
api "com.android.support:design:${ANDROID_SUPPORT_LIBS}"
api "com.android.support:recyclerview-v7:${ANDROID_SUPPORT_LIBS}"
api "com.android.support:cardview-v7:${ANDROID_SUPPORT_LIBS}"
api "com.android.support.constraint:constraint-layout:${CONSTRAINT_LAYOUT}"
api "com.mikepenz:iconics-core:${ICONICS}#aar"
api "com.mikepenz:google-material-typeface:${IICON_GOOGLE}.original#aar"
api "com.afollestad.material-dialogs:core:${MATERIAL_DIALOG}"
api "com.jakewharton.timber:timber:${TIMBER}"
api "org.jetbrains.anko:anko-commons:${ANKO}"
}
Edit:
To clarify, the sample project in the module actually does build properly, but there's an issue with using the dependencies in any other app, where it pulls from jitpack. See this gradle as an example that won't build.
I've tried using combinations of api, implementation, #aar, and transitive.
Come to think of it, this may be a jitpack issue and not a gradle issue, but if anyone else has a resolution I'd like to hear it.
I no longer have access to any of the libraries dependencies. I can only use code and references from my library itself.
It is correct.
From the gradle docs :
dependencies {
api 'commons-httpclient:commons-httpclient:3.1'
implementation 'org.apache.commons:commons-lang3:3.5'
}
Dependencies appearing in the api configurations will be
transitively exposed to consumers of the library, and as such will
appear on the compile classpath of consumers.
Dependencies found in the implementation configuration will, on the
other hand, not be exposed to consumers, and therefore not leak into
the consumers' compile classpath. This comes with several benefits:
dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive
dependency
faster compilation thanks to reduced classpath size
less recompilations when implementation dependencies change: consumers would not need to be recompiled
cleaner publishing: when used in conjunction with the new maven-publish plugin, Java libraries produce POM files that
distinguish exactly between what is required to compile against the
library and what is required to use the library at runtime (in other
words, don't mix what is needed to compile the library itself and what
is needed to compile against the library).
The issue seems to be related to the android-maven-gradle-plugin
Issue Report
It's has been fixed in version "2.0" of android-maven-gradle-plugin
just update to
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
}
or using the new syntax since Gradle 2.1
plugins {
id "com.github.dcendents.android-maven" version "2.0"
}
using api in your library module allows you to access the transient dependencies only in your library code; not the apps that consume it.
so to achieve the desired effect you need to change in your sample module.
implementation project(':core')
to
api project(':core')
note you don't need to use api in your library it's better to use implementation as it speeds up your build.

Issue Setting up Dagger 2.x for Android on AndroidStudio3.0 Canary4

I get following errors while setting up dagger 2.x in Android Studio 3.0 Canary 4
Error:(71, 20) Failed to resolve: com.google.dagger:dagger:2.x
Error:(73, 20) Failed to resolve: com.google.dagger:dagger-android:2.x
Error:(74, 20) Failed to resolve: com.google.dagger:dagger-android-support:2.x
My build files are like below:
dependencies {
//For DI - Dagger 2
implementation 'com.google.dagger:dagger:2.x'
annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
implementation 'com.google.dagger:dagger-android:2.x' // If you're using classes in dagger.android
implementation 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'
}
The project build file has below snippets
allprojects {
repositories {
jcenter()
maven { url "https://maven.google.com" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
}
Your help is appreciated...
If you're like me to have got into this problem, this is what I did to get out of this situation.
I went to https://github.com/google/dagger/releases to figure out the latest release version of dagger, and found v2.11 to be the latest as on date. I replaced 2.x with 2.11 in the version portion for this libraries configuration in the build file and bingo the build is successful.
dependencies {
//For DI - Dagger 2
implementation 'com.google.dagger:dagger:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
implementation 'com.google.dagger:dagger-android:2.11' // If you're using classes in dagger.android
implementation 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
}
Edit:
I completely agree with the comments mentioning the usage of specific version of this library instead of +, so for example 2.11 should be used instead of 2.+
2.+ was intended to fix the issue with 2.x as most of beginners take 2.x literally like I did when I first used it.
The x here meant to be latest minor version of the stable release. Please check the latest release notes and replace x with latest minor version of library.
Original Answer:
I am sure by now you have resolved your issue, though after trying few others and this one too, I found a reliable solution and posting it for helping others. Instead of 2.x use 2.+.
It solved all issues for me, not only it resolved the above problem,it also make sure to pull the latest version dagger 2.x available.
It should look like this:
dependencies {
implementation 'com.google.dagger:dagger:2.+'
annotationProcessor 'com.google.dagger:dagger-compiler:2.+'
implementation 'com.google.dagger:dagger-android:2.+' // If you're using classes in dagger.android
implementation 'com.google.dagger:dagger-android-support:2.+' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.+'
}
Thanks!

Categories

Resources