Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare version numbers in gradle?

I need to use a different Gradle plugin version dependening the Gradle version. How can I compare a version in Gradle? The variable gradle.gradleVersion is a string and can't good compare. The follow does not work if the minor version has only one digit.

buildscript {
  dependencies {
     def ver = gradle.gradleVersion >= '2.12' ? '+' : '1.5.+'
     classpath group: 'de.inetsoftware', name: 'SetupBuilder', version: ver
  }
}
like image 245
Horcrux7 Avatar asked Apr 04 '16 10:04

Horcrux7


People also ask

How do I compare version numbers?

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0 .

How do I check Gradle version?

In Android Studio, go to File > Project Structure. Then select the "project" tab on the left. Your Gradle version will be displayed here.

How do I mention Gradle version in build Gradle?

You can add the --gradle-version X.Y argument to specify which version of Gradle to use. Now you can run any Gradle task in your project using the gradlew shell script or bat file located in your project's root directory.

How do I know if Gradle dependency has new version?

Go to Android Studio -> Preferences -> Plugins (for Mac) and File -> Settings -> Plugins (for windows) and search “Check for Dependency updates plugin”. Install it and restart android studio. You will be able to see Dependencies tab on the right which will show if any dependency has a new update available.


1 Answers

The class VersionNumber is the trick:

buildscript {
  dependencies {
     def ver = VersionNumber.parse( gradle.gradleVersion ) >= VersionNumber.parse( '2.12' ) ? '+' : '1.5.+'
     classpath group: 'de.inetsoftware', name: 'SetupBuilder', version: ver
  }
}

Note: VersionNumber is deprecated in Gradle 7 and is being removed from Gradle 8. There is no public replacement (it was never meant to be used outside of Gradle internal code). See docs.

https://docs.gradle.org/current/javadoc/org/gradle/util/VersionNumber.html

like image 85
Horcrux7 Avatar answered Sep 19 '22 04:09

Horcrux7