Hello, i want to force a version of direct depende...
# kotlin-dsl
s
Hello, i want to force a version of direct dependency to the same version of a transitive dependency. i have a dependency on retrofit 2.9.0 which has a transitive dependency om okhttp 3.14.9
Copy code
com.squareup.retrofit2:retrofit:2.9.0
-- com.squareup.okhttp3:okhttp:3.14.9
now I want to add a direct dependency on com.squareup.okhttp3:logging-interceptor, and i want it to be the same version of okhttp that retrofit brings,3.14.9. of course i can declare com.squareup.okhttp3:logging-interceptor to the same version, but i want to know if there is a way to do it using some resolution strategy? so if i declare com.squareup.okhttp3logging interceptor4.12.0 i want to force it to the same version of okhttp 3.14.9.
t
You can use a component metadata rule to do so-called "dependency version alignment" and ensure that all
com.squareup.okhttp3
dependencies have the same version (will resolve to the higher version of all dependencies by default though; and I don't think there's a way of downgrading to "the version of the transitive dependency", so declare logging-interceptor with the lowest version to get it upgraded): https://docs.gradle.org/current/userguide/dependency_version_alignment.html#sec:align-versions-unpublished
You may want to explicitly set the OkHttp version though; use the okhttp-bom as a platform instead of the component metadata rule.
s
Thanks!. ComponentMetadataRule works. but it still needs some level of manual control. retrofit brings okhttp 3.14.9, my main concern is that retrofit doesn't break, if someone adds a dependency to okhttp that is higher then 3.14.9 then this version will be selected and retrofit may break. I wanted to do something like: "for all com.squareup.okhttp3 take the version that 'com.squareup.retrofit2:retrofit' brings as transitive dependency" i don't know if that's possible with standard dependency resolution tools. i can enforce platform with
Copy code
implementation(enforcedPlatform("com.squareup.okhttp3:okhttp-virtual-platform:3.14.9"))
but still i need to write down the version somewhere which i don't want to
t
Retrofit won't break, because OkHttp is very conservative with forwards compatibility: https://jakewharton.com/java-interoperability-policy-for-major-version-updates/ (tl;dr: if there's a risk it could break, the groupId and package name are updated; this is why OkHttp currently is
com.squareup.okhttp3
and not
com.squareup.okhttp
)
Fwiw, I'd just go with:
Copy code
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.12.0"))
(avoid
enforcePlatform
if you can, you can keep the component metadata rule to continue aligning even once version 5 is released and possibly brought in transitively)
s
OK, Thank you ! So I will just keep it that way with the ComponentMetadataRule