Member-only story
How to read version and other information from Android and iOS apps using Java
Easily retrieve version information from APK, IPA, and APP packages

Many times, especially when running automated tests against mobile apps, it is important to verify that the correct version of an app is used. This article describes easy ways to query the version information from both Android and iOS app packages using either command line tools or Java libraries. Both approaches can be easily integrated into a build pipeline.
The command-line way
For Android, one approach to parse meta information from an APK file is to use aapt
like this:
aapt dump badging /path/test.apk
This prints a lot of information so we need to trim the output to what we are interested in:
aapt dump badging test.apk | grep -o 'versionName=[^,]*' | cut -d'=' -f 2 | cut -d ' ' -f 1 | tr -d "'"
Similarly, we can also query the version code (or any other information for that matter):
aapt dump badging /path/test.apk | grep -o ‘versionCode=[^,]*’ | cut -d’=’ -f 2 | cut -d ‘ ‘ -f 1 | tr -d "'"
If we have the app installed on a device (physical or simulator) which is connected via adb
…