Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace <version>-android with <version>-jre #97

Open
Fleshgrinder opened this issue Jun 6, 2019 · 2 comments
Open

Replace <version>-android with <version>-jre #97

Fleshgrinder opened this issue Jun 6, 2019 · 2 comments

Comments

@Fleshgrinder
Copy link

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.

@DanielThomas
Copy link
Contributor

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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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 <dannyt@netflix.com>",
      "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')
    }
}

@Fleshgrinder
Copy link
Author

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 <dannyt@netflix.com>",
      "date" : "2018-03-14"
    }
  ]

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants