简体   繁体   中英

Android: How to Know if any application is already installed in android device using adb?

I've to install an android app with package name like "com.xyz.game" using adb. I want to automate the process using shell script. The process will be like if app is already installed, uninstall it(with command adb uninstall com.xyz.game) and install using "adb install game.apk" otherwise simple "adb install game.apk".

How can I achieve this?

[ Update 2 ]

Without using grep

adb shell pm list packages [your.package.name] as mentioned in the below answer

[ Update ]

According to (also) correct answer below, try grep the result from pm list packages.

adb shell pm list packages | grep com.your.app.package


[ Original ]

If the application is already installed and if you try to install the same app again, adb will return with an error - Failure [INSTALL_FAILED_ALREADY_EXISTS] . However, if you want to re-install the already installed app, then use -r parameter.

Ex:

adb install -r game.apk

Try grep the result from pm list packages.

adb shell pm list packages | grep com.xyz.game

You may see the result if already installed.

package:com.xyz.game

No need to use grep. Using following commands you can simply check if application is already exist or not.

Run ADB command

adb shell pm list packages [your.package.name]

If app is already installed then above command will return,

package:[your.package.name]

Else it won't return anything ie empty String.

Android 7.0 has introduced cmd (a new native code based) tool which allows to interact with Android services like PackageManager much faster than the old java bytecode based tools like pm . So for recent Android versions instead of adb shell pm list packages <package.name.substring> you should use

adb shell cmd package list packages <package.name.substring>

Taking into account @Joel answer, just do it in one-liner

# uninstall only if exists
adb shell pm list packages | grep com.your.app.package && adb uninstall com.your.app.package

Use First adb shell command if you are using adb otherwise you can use use root in same device by su command

Then, we can get path of installed apk files by their pkg names lets say in our case pkg name is com.tencent.ig

pm path com.tencent.ig | sed 's/package://'

If app installed : You will get the path to the apk file

If app not installed : You will get nothing

Previous answers are limited but by using above logic we can also use if statement easily like :

pkg=com.tencent.ig #Add pkg of your app only here
if [ -f $(pm path $pkg | sed 's/package://') ]
then 
echo "YES, app is installed"
#further commands if app is installed
else 
echo "NO, app is not installed"
#further commands if app not installed
fi

I think that will be much handy.

This works fine :

adb shell pm list packages your.package.name

Replace your.package.name with the required package name.

If app installed : You will get the path to the apk file

If app not installed : You will get nothing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM