Get Android .apk file VersionName or VersionCode WITHOUT installing apk
Solution 1
Following worked for me from the command line:
aapt dump badging myapp.apk
NOTE: aapt.exe is found in a build-tools
sub-folder of SDK. For example:
<sdk_path>/build-tools/23.0.2/aapt.exe
Solution 2
final PackageManager pm = getPackageManager();
String apkName = "example.apk";
String fullPath = Environment.getExternalStorageDirectory() + "/" + apkName;
PackageInfo info = pm.getPackageArchiveInfo(fullPath, 0);
Toast.makeText(this, "VersionCode : " + info.versionCode + ", VersionName : " + info.versionName , Toast.LENGTH_LONG).show();
Solution 3
If you are using version 2.2 and above of Android Studio then in Android Studio use Build → Analyze APK then select AndroidManifest.xml file.
Solution 4
aapt dump badging test.apk | grep "versionName" | sed -e "s/.*versionName='//" -e "s/' .*//"
This answers the question by returning only the version number as a result. However......
The goal as previously stated should be to find out if the apk on the server is newer than the one installed BEFORE attempting to download or install it. The easiest way to do this is include the version number in the filename of the apk hosted on the server eg myapp_1.01.apk
You will need to establish the name and version number of the apps already installed (if it is installed) in order to make the comparison. You will need a rooted device or a means of installing the aapt binary and busybox if they are not already included in the rom.
This script will get the list of apps from your server and compare with any installed apps. The result is a list flagged for upgrade/installation.
#/system/bin/sh
SERVER_LIST=$(wget -qO- "http://demo.server.com/apk/" | grep 'href' | grep '\.apk' | sed 's/.*href="//' | \
sed 's/".*//' | grep -v '\/' | sed -E "s/%/\\\\x/g" | sed -e "s/x20/ /g" -e "s/\\\\//g")
LOCAL_LIST=$(for APP in $(pm list packages -f | sed -e 's/package://' -e 's/=.*//' | sort -u); do \
INFO=$(echo -n $(aapt dump badging $APP | grep -e 'package: name=' -e 'application: label=')) 2>/dev/null; \
PACKAGE=$(echo $INFO | sed "s/.*package: name='//" | sed "s/'.*$//"); \
LABEL=$(echo $INFO | sed "s/.*application: label='//" | sed "s/'.*$//"); if [ -z "$LABEL" ]; then LABEL="$PACKAGE"; fi; \
VERSION=$(echo $INFO | sed -e "s/.*versionName='//" -e "s/' .*//"); \
NAME=$LABEL"_"$VERSION".apk"; echo "$NAME"; \
done;)
OFS=$IFS; IFS=$'\t\n'
for REMOTE in $SERVER_LIST; do
INSTALLED=0
REMOTE_NAME=$(echo $REMOTE | sed 's/_.*//'); REMOTE_VER=$(echo $REMOTE | sed 's/^[^_]*_//g' | sed 's/[^0-9]*//g')
for LOCAL in $LOCAL_LIST; do
LOCAL_NAME=$(echo $LOCAL | sed 's/_.*//'); LOCAL_VER=$(echo $LOCAL | sed 's/^[^_]*_//g' | sed 's/[^0-9]*//g')
if [ "$REMOTE_NAME" == "$LOCAL_NAME" ]; then INSTALLED=1; fi
if [ "$REMOTE_NAME" == "$LOCAL_NAME" ] && [ ! "$REMOTE_VER" == "$LOCAL_VER" ]; then echo remote=$REMOTE ver=$REMOTE_VER local=$LOCAL ver=$LOCAL_VER; fi
done
if [ "$INSTALLED" == "0" ]; then echo "$REMOTE"; fi
done
IFS=$OFS
As somebody asked how to do it without using aapt. It is also possible to extract apk info with apktool and a bit of scripting. This way is slower and not simple in android but will work on windows/mac or linux as long as you have working apktool setup.
#!/bin/sh
APK=/path/to/your.apk
TMPDIR=/tmp/apktool
rm -f -R $TMPDIR
apktool d -q -f -s --force-manifest -o $TMPDIR $APK
APK=$(basename $APK)
VERSION=$(cat $TMPDIR/apktool.yml | grep "versionName" | sed -e "s/versionName: //")
LABEL=$(cat $TMPDIR/res/values/strings.xml | grep 'string name="title"' | sed -e 's/.*">//' -e 's/<.*//')
rm -f -R $TMPDIR
echo ${LABEL}_$(echo $V).apk
Also consider a drop folder on your server. Upload apks to it and a cron task renames and moves them to your update folder.
#!/bin/sh
# Drop Folder script for renaming APKs
# Read apk file from SRC folder and move it to TGT folder while changing filename to APKLABEL_APKVERSION.apk
# If an existing version of the APK exists in the target folder then script will remove it
# Define METHOD as "aapt" or "apktool" depending upon what is available on server
# Variables
METHOD="aapt"
SRC="/home/user/public_html/dropfolders/apk"
TGT="/home/user/public_html/apk"
if [ -d "$SRC" ];then mkdir -p $SRC
if [ -d "$TGT" ]then mkdir -p $TGT
# Functions
get_apk_filename () {
if [ "$1" = "" ]; then return 1; fi
local A="$1"
case $METHOD in
"apktool")
local D=/tmp/apktool
rm -f -R $D
apktool d -q -f -s --force-manifest -o $D $A
local A=$(basename $A)
local V=$(cat $D/apktool.yml | grep "versionName" | sed -e "s/versionName: //")
local T=$(cat $D/res/values/strings.xml | grep 'string name="title"' | sed -e 's/.*">//' -e 's/<.*//')
rm -f -R $D<commands>
;;
"aapt")
local A=$(aapt dump badging $A | grep -e "application-label:" -e "VersionName")
local V=$(echo $A | sed -e "s/.*versionName='//" -e "s/' .*//")
local T=$(echo $A | sed -e "s/.*application-label:'//" -e "s/'.*//")
;;
esac
echo ${T}_$(echo $V).apk
}
# Begin script
for APK in $(ls "$SRC"/*.apk); do
APKNAME=$(get_apk_filename "$APK")
rm -f $TGT/$(echo APKNAME | sed "s/_.*//")_*.apk
mv "$APK" "$TGT"/$APKNAME
done
Solution 5
At the moment, this can be done as follows
$ANDROID_HOME/build-tools/28.0.3/aapt dump badging /<path to>/<app name>.apk
In General, it will be:
$ANDROID_HOME/build-tools/<version_of_build_tools>/aapt dump badging /<path to>/<app name>.apk
Related videos on Youtube
Big.Child
Updated on July 28, 2022Comments
-
Big.Child almost 2 years
How can I get programmatically get the version code or version name of my apk from the AndroidManifest.xml file after downloading it and without installing it.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xxx.xx.xxx" android:versionCode="1" android:versionName="1.1" >
For example I want to check if a new version is uploaded on my IIS service, after install it on device, if it is not a new version I don't want to install it.
-
nandeesh over 11 yearscheck this stackoverflow.com/a/4761689/1109425
-
ToolmakerSteve over 7 yearsBTW, I hope no one thinks a good solution is to download the file, and then check whether it is needed - Which is what the first sentence suggests. Presumably you would instead want code that runs on your server, and responds with the version number that is available.
-
LF00 over 4 years
./aapt d badging release.apk | grep -Po "(?<=\sversion(Code|Name)=')([0-9.]+)"
-
LF00 over 4 yearsaapt will cost about
2.6M
disk space, you can also write a parser to parse the apk file withAXMLResource
which will just cost about52k
. Refer to Java parse xml with undeclared namespace
-
-
talha06 about 10 yearshow can I read
apk
content without installing the application? @PatrickCho -
Quentin Klein over 9 yearsWorks fine, this should be marked as the correct answer, for information,
aapt
is in build-tools/XX in the sdk. -
Daniel Zolnai about 9 yearsThis does indeed work. And you don't need to install it. You just have an apk somewhere on the your device's storage, and supply the path to it to the package manager. It will indeed return the package info.
-
gmuhammad about 9 yearsI found it under "<sdk_path>/build-tools/21.1.2"
-
ToolmakerSteve over 7 yearsThis is not an answer to the question. This is code that, inside a running app, gets some version info, and displays it. The question is how to get this information from an APK file, without installing the app.
-
ToolmakerSteve over 7 yearsYou are on the right track - should not download file, just to get its version. I just want to mention that I wouldn't recommend creating the text file MANUALLY - too easy to have it not match the actual apk file. Instead, write a script (that uses one of the other answers) to look inside the apk to see what its version is, storing that in file or database, to be retrieved by the means you discuss.
-
grego over 7 yearsYeah in this scenario the case is not that it needs to match the APK at all, just that the latest version number is somewhere publicly accessible.
-
cscott530 about 7 yearsUsing Xamarin on a Mac, it was in
~/Library/Developer/Xamarin/android-sdk-macosx/build-tools/{version}
-
forresthopkinsa over 6 yearsOP asked how to get it
programmatically
-
vovahost over 6 yearsBecause he didn't know that a tool like this is built in Android Studio. He also asked without installing the apk.
-
forresthopkinsa over 6 yearsOf course he didn't know that, because Android Studio didn't exist when this question was asked. Additionally, even if it did, this does not solve his problem -- he wants his device to programmatically check for updates from his IIS server, as he stated in the question. Patrick Cho's answer explains how to do it programmatically without installing.
-
Brian S over 6 yearsHave you upgraded to the new Gradle or Android Studio? Does this still work? The Robio code does not work to decompress the XML for me anymore.
-
Sevastyan Savanyuk about 6 yearsWhat is
platformBuildVersionName
that gets printed when I use this command and why is it empty? -
user924 about 6 yearsyou can create bat file
aapt dump badging %1 pause
to drag n drop any apk -
auspicious99 about 5 yearsStill works in 2019 with aapt in build tools 28.0.3
-
Mig82 about 5 yearsTried this on Mac with build tools 28.0.3.
/Library/Android/android-sdk/build-tools/28.0.3/aapt dump badging foo.apk
. Works great. @Big.Child this should be marked as the correct answer. -
Rhusfer about 5 yearsThis is only for already installed application not for an .apk file.
-
BenjyTec about 4 yearsI had to use .\aapt command on my Windows, just in case somebody also gets an error.
-
lvl4fi4 over 3 yearsin windows use "d" instead of "dump". dump did not worked for me
-
buradd over 3 yearsthis is the best answer imo
-
crgarridos almost 3 yearsMaybe off-topic, but I'm getting
?
as output. not sure why, but with the oldappt
it gets me the good value -
antaki93 about 2 yearsThank you! This is the simplest way to known versionCode of APK. It is what I searched.