Version Compare

Additional

Language
Java
Version
1.2.7 (Apr 16, 2018)
Created
Jan 12, 2018
Updated
May 28, 2018
Owner
Thomas Wirth (G00fY2)
Contributor
Thomas Wirth (G00fY2)
1
Activity
Badge
Generate
Download
Source code
APK file

Announcement

Version Compare

Lightweight Android & Java library to compare version strings.

This library allows you to easily compare version strings. Versions can but do not necessarily have to follow the SemVer convention. Any number of version parts as well as common pre-release suffixes will be taken into account.

Pure Java (java.util), no dependencies, very small method count.

Usage

Include the library in your build.gradle

dependencies {
  implementation 'com.g00fy2:versioncompare:1.2.7'
}

Note: This will download the recommended Android AAR artifact with Android specific data like ProGuard rules. If you need a traditional JAR artifact add @jar to the library version.

To compare two version strings just create a new Version object. Invalid inputs will by default be handled as 0.0.0.

boolean result;

result = new Version("1.2.1").isHigherThan("1.2"); // result = true

result = new Version("1.0.2-rc2").isLowerThan("1.0.2-rc3"); // result = true

result = new Version("1.3").isEqual("1.3.0"); // result = true

result = new Version("1.8.0-rc").isHigherThan("1.8"); // result = false
result = new Version("1.8.0-rc").isAtLeast("1.8"); // result = true

For more detailed usage, check out the documentation.

Version structure example

Version 1.7.3-rc2.xyz
            +-------+   +-------+   +-------+   +-------+
    String  |   1   | . |   7   | . | 3-rc2 | . |  xyz  |
            +-------+   +-------+   +-------+   +-------+
                |           |         |  |          |
  major  [1] <--            |         |   ----      |
  minor  [7] <--------------          |       | ----
  patch  [3] <------------------------        ||
         ...                            +------------+
                                suffix  |  -rc2.xyz  |
                                        +------------+
-------------------------------------------------------------------------
suffix compare logic                          ||
                                         -----  -----
                                        |            |
                                    +-------+    +-------+
              detected pre-release  |  rc2  |    |  xyz  |  ignored part
                                    +-------+    +-------+
                                       ||
                                    ---  ---
                                   |        |
                                +----+    +---+
                                | rc |    | 2 |  pre-release build
                                +----+    +---+

Notes:

  • expected separator between version numbers is .
  • the optional suffix does not need a special separator

Supported pre-release suffixes

order suffix
4 empty or unknown
3 rc
2 beta
1 alpha
0 pre + alpha

Notes:

  • higher order results in higher version 1.0 > 1.0-beta
  • pre-release builds are supported 1.0-rc3 > 1.0-rc2

Sample App

Try out the sample app to compare your version inputs: Download

License

Copyright (C) 2018 Thomas Wirth

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.