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

Exception or error:

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?

How to solve:

[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 i.e. 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>

Leave a Reply

Your email address will not be published. Required fields are marked *