As the android version names are in String data type, I want to compare the existing apk on my device with the new apk update version name. I know how to get the version name programmatically and I have two version names but string comparision between two version names does not show correct result
if ( oldVersionName.compareTo( newVersionName ) < 0 ) {
}
This compareTo does not work in few scenarioes like 1.0.9 and 1.0.12, 1.0.9 and 1.0.10.
Please give me solution on how to compare two string version names programatically? Thank you!
newVersion = 1.2.1 -> 121 -> 121
oldVersion = 1.2 -> 12 -> 120
121 > 120 ?
return true
newVersion = 1.5.15 -> 1515 -> 1515
oldVersion = 2.0 -> 20 -> 2000
1515 > 2000 ?
return false
public static boolean checkForUpdate(String existingVersion, String newVersion) {
if (existingVersion.isEmpty() || newVersion.isEmpty()) {
return false;
}
existingVersion = existingVersion.replaceAll("\\.", "");
newVersion = newVersion.replaceAll("\\.", "");
int existingVersionLength = existingVersion.length();
int newVersionLength = newVersion.length();
StringBuilder versionBuilder = new StringBuilder();
if (newVersionLength > existingVersionLength) {
versionBuilder.append(existingVersion);
for (int i = existingVersionLength; i < newVersionLength; i++) {
versionBuilder.append("0");
}
existingVersion = versionBuilder.toString();
} else if (existingVersionLength > newVersionLength){
versionBuilder.append(newVersion);
for (int i = newVersionLength; i < existingVersionLength; i++) {
versionBuilder.append("0");
}
newVersion = versionBuilder.toString();
}
return Integer.parseInt(newVersion) > Integer.parseInt(existingVersion);
}
You can get the 'int form' of Android versions using
Build.VERSION.SDK_INT
Anyway, if all you have is version names in 'dot notation', you can split
the strings by '.' char, and iterate to compare part by part. Off the top of my head, it'd look something like.-
public int compareVersionNames(String oldVersionName, String newVersionName) {
int res = 0;
String[] oldNumbers = oldVersionName.split("\\.");
String[] newNumbers = newVersionName.split("\\.");
// To avoid IndexOutOfBounds
int maxIndex = Math.min(oldNumbers.length, newNumbers.length);
for (int i = 0; i < maxIndex; i ++) {
int oldVersionPart = Integer.valueOf(oldNumbers[i]);
int newVersionPart = Integer.valueOf(newNumbers[i]);
if (oldVersionPart < newVersionPart) {
res = -1;
break;
} else if (oldVersionPart > newVersionPart) {
res = 1;
break;
}
}
// If versions are the same so far, but they have different length...
if (res == 0 && oldNumbers.length != newNumbers.length) {
res = (oldNumbers.length > newNumbers.length)?1:-1;
}
return res;
}
Just wrote without trying it, so sure can be optimized, but it's a start.
public boolean checkVersion() {
boolean isLastVersion = false;
Double lastVersion = convertVersion(getAppVersion());
Double runningVersion = convertVersion(BuildConfig.VERSION_NAME);
if (runningVersion >= lastVersion) {
isLastVersion = true;
}
return isLastVersion;
}
public Double convertVersion(String version) {
Double convertedVersion = 0.0;
version = version.replace(".", "");
String versionAux1 = version.substring(0,1);
String versionAux2 = version.substring(1, version.length());
version = versionAux1 + "." + versionAux2;
convertedVersion = Double.valueOf(version);
return convertedVersion;
}
getAppVersion() returns version to compare
convertVersion(String version) converts a String value with none, one or more dots to a Double value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With