Resolving jar conflicts in Gradle

If two dependencies refer to different versions of the same jar package, gradle will use the latest version of the jar package by default, which can be excluded by the exclude option.

First, look at the format when relying on a library, name is module

compile group:'com.android.support',name:'appcompat-v7',version:'26.1.0'

1. Exclude directly in the configuration

    configurations{
        //Exclude appcompat-v7 at compile time
        compile.exclude module: 'appcompat-v7'
        // Exclude com.android.support:appcompat-v7 throughout the build process
        all*.exclude group: 'com.android.support', module: 'appcompat-v7'
    }

2. Exclude in a specific dependency, for example, appcompat-v7 needs to be excluded in the jar package com.common:lib:1.0 we introduced

dependencies{

    compile 'com.common:lib:1.0'{
        exclude module: 'appcompat-v7'
        //or
        exclude group: 'com.common',module: 'appcompat-v7'
    }
}

Excluding multiple jars can use multiple excludes

3. Of course, we can also force a unified version. When conflicts arise, use this version first to resolve

compile('com.common:lib:1.0') {
     force = true
}

4. Force the use of a unified version of the dependency, we can also configure it globally

configurations.all {
   resolutionStrategy {
       force 'com.common:lib:1.0'
   }
}

5. Speaking of this, I have to say transitive, which is transitive since management. For example, okhttp is used in com.common:lib:1.0. If it can be passed, it will automatically add okhttp for us when using it. If If it cannot be passed, you need to configure okhttp additionally

Turn off dependency transit

compile('com.common:lib:1.0') {
    transitive = false
}

You can also ignore all transitive dependencies of the dependency by adding @jar

compile `com.common:lib:1.0@jar`

Of course, you can also configure global configuration to turn off dependency transfer.

configurations.all {
   transitive = false
}
6. Let’s talk about dynamic dependencies again. We sometimes make the jar always the latest version, and instead of manually changing the version every time, we can use + ; gradle checks whether the dependency exists in the remote warehouse every time the build is executed The new version, if there is a new version, download and select the latest version.
compile 'com.common:lib:+'
Of course, you can also specify a dependency on the latest subversion under a major version. 1.+ means that the latest dependency of the latest 1.x version of the dependency is always used.
compile 'com.common:lib:1.+'

Lastly, when there is a conflict, to check the dependencies of a conflicting jar, you can use dependencyInsight. You can find and learn the specific usage. For example, see where the compile of v7 in the app project appears.

gradlew -q app:dependencyInsight --dependency appcompat-v7 --configuration compile
end!



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325173046&siteId=291194637