Version conflicts happen every day
Recently, I met with a red mistake of Mr. AS very often.
Annotations have two versions (26.1.0 and 27.1.1).
It felt like it was an instant problem, didn’t it?
Because build.gradle doesn’t have an obvious dependency on Support-Annotations.
dependencies { implementation fileTree(dir: 'libs', include: [' *. Jar ']) implementation 'com. Android. Support: appcompat - v7:26.1.0' implementation 'com. Android. Support. The constraint, the constraint - layout: 1.1.2' testImplementation junit: junit: '4.12' androidTestImplementation 'com. Android. Support. Test: runner: 1.0.2' androidTestImplementation 'com. Android. Support. Test. Espresso: espresso - core: 3.0.2'}Copy the code
Rely on tree tracing
With regard to the above issues, my years of experience dealing with the explosion of AS tells me that it is time to print the dependency tree of the project!
./gradlew :app:dependencyInsight --dependency support-annotations --configuration debugAndroidTestCompileClasspath
Copy the code
Use the dependencyInsight command to support viewing the version dependencies of a library in a project. Error message shows that the conflict in the for the app and for the test app, so choose print debugAndroidTestCompileClasspath configuration depends on the tree.
Get the dependency tree, combine the project build.gradle, and compare the three libraries that cause the conflict:
-
Com. Android. Support: appcompat – v7:26.1.0 – > (26.1.0)
-
Com. Android. Support. Test: runner: 1.0.2 – > (27.1.1)
-
Com. Android. Support. Test. Espresso: espresso – core: 3.0.2 – > (27.1.1)
Once the cause is found, the problem is easily resolved by eliminating one version of the conflict. Because support libraries generally maintain a uniform version, I chose to exclude conflicting versions of other libraries.
AndroidTestImplementation (' com. Android. Support. Test: runner: 1.0.2 ') {exclude group: 'com. Android. Support'} androidTestImplementation (' com. Android. Support. The test. The espresso: espresso - core: 3.0.2 ') {exclude group: 'com.android.support' }Copy the code
Another way
In the development work, it is very common to encounter the dependency library version conflict problem, through the above steps can be basically solved, but sometimes the project is very large, it is very difficult to find. There is a simple and crude solution to this.
Add the following code to project build.gradle. The function is to iterate over all dependencies at project build time, and then replace the dependencies under com.android.support with the same version.
buildscript { subprojects { project.configurations.all { resolutionStrategy.eachDependency { details -> if (details.requested.group == 'com.android.support' && ! The requested. Name. The contains (' multidex)) {the useVersion "26.1.0"}}}}}Copy the code
This approach is very efficient, but each time you build, you add a traversal process, which increases the build time. Therefore, it is recommended to use the exclude keyword only for emergencies.
The above is the latest experience to solve the problem to share, I hope to help you.
If you think it’s okay, or you see a mistake, or you have a better idea. Please don’t be shy, speak up, the author warmly welcome your likes and comments.