Replace <version>-android with <version>-jre
I am currently using the following snippet in my build.gradle.kts but would love to replace this with a rule for the plugin:
configurations.all {
resolutionStrategy {
eachDependency {
if (requested.group == "com.google.guava" && requested.version?.contains("-android") == true) {
useVersion(requested.version!!.replace("-android", "-jre"))
because("Android Guava lacks certain optimizations: https://github.com/google/guava/issues/2914")
}
}
}
}
I couldn't figure out how to do this based on the information available in the Wiki.
It's too opinionated to make a default rule, but I wrote one internally that I have sitting on a branch - the only way we can do it with rules is with one specific to every version.
Feel free to PR us over at https://github.com/nebula-plugins/gradle-resolution-rules if you want to add this as an optional rule (filename must start with optional-. Rule and tests below.
"substitute": [
{
"module" : "com.google.guava:guava:24.0-android",
"with" : "com.google.guava:guava:24.0-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:23.6-android",
"with" : "com.google.guava:guava:23.6-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:23.5-android",
"with" : "com.google.guava:guava:23.5-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:23.4-android",
"with" : "com.google.guava:guava:23.4-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:23.3-android",
"with" : "com.google.guava:guava:23.3-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:23.2-android",
"with" : "com.google.guava:guava:23.2-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:23.1-android",
"with" : "com.google.guava:guava:23.1-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:23.0-android",
"with" : "com.google.guava:guava:23.0-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:23.0-rc1-android",
"with" : "com.google.guava:guava:23.0-rc1-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:22.0-android",
"with" : "com.google.guava:guava:22.0-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
},
{
"module" : "com.google.guava:guava:22.0-rc1-android",
"with" : "com.google.guava:guava:22.0-rc1-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
}
]
class SubstituteGuavaSpec extends RulesBaseSpecification {
def setup() {
buildFile << """\
dependencies {
resolutionRules files('${new File('src/main/resources/substitute-guava-android.json').absolutePath}')
}
""".stripIndent()
}
def "substitute latest release of android variant"() {
given:
buildFile << """\
def guavaLatest = dependencies.create('com.google.guava:guava:latest.release')
def detached = configurations.detachedConfiguration(guavaLatest)
def guavaVersion = detached.resolvedConfiguration.firstLevelModuleDependencies.first().module.id.version.replace('-jre', '-android')
dependencies {
compile "com.google.guava:guava:\${guavaVersion}"
}
""".stripIndent()
when:
BuildResult result = runWithArgumentsSuccessfully('dependencies', '--configuration', 'compile')
then:
def group = result.output =~ /com\.google\.guava:guava:([0-9]+\.[0-9])/
def version = group[0][1]
result.output.contains("com.google.guava:guava:${version}-android -> ${version}-jre")
}
def "substitute android variant brought in by guice"() {
given:
buildFile << """\
dependencies {
compile 'com.google.inject:guice:4.2.0'
}
""".stripIndent()
when:
BuildResult result = runWithArgumentsSuccessfully('dependencies', '--configuration', 'compile')
then:
result.output.contains('com.google.guava:guava:23.6-android -> 23.6-jre')
}
}
I feared that this would be the result, keeping this up to date will be a pain. There are already many more versions out. I guess that #42 needs to be resolved and support for capturing groups needs to be added so that one could write:
"substitute": [
{
"module" : "com.google.guava:guava:(.*)-android",
"with" : "com.google.guava:guava:$1-jre",
"reason" : "Avoid Android/JDK 7 subset of Guava",
"author" : "Danny Thomas <[email protected]>",
"date" : "2018-03-14"
}
]