简体   繁体   English

Android:如何知道是否已经使用 adb 在 android 设备中安装了任何应用程序?

[英]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.我必须使用 adb 安装一个包名称如“com.xyz.game”的 android 应用程序。 I want to automate the process using shell script.我想使用 shell 脚本自动化这个过程。 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".这个过程就像已经安装了应用程序,卸载它(使用命令 adb uninstall com.xyz.game)并使用“adb install game.apk”安装,否则简单的“adb install game.apk”。

How can I achieve this?我怎样才能做到这一点?

[ Update 2 ] [更新 2 ]

Without using grep不使用grep

adb shell pm list packages [your.package.name] as mentioned in the below answer adb shell pm list packages [your.package.name]以下答案中所述

[ Update ] [更新]

According to (also) correct answer below, try grep the result from pm list packages.根据下面的(也)正确答案,尝试 grep 来自 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] .如果应用程序已经安装,并且如果您尝试再次安装相同的应用程序,adb 将返回错误 - Failure [INSTALL_FAILED_ALREADY_EXISTS] However, if you want to re-install the already installed app, then use -r parameter.但是,如果要重新安装已安装的应用程序,请使用-r参数。

Ex:前任:

adb install -r game.apk

Try grep the result from pm list packages.尝试 grep 来自 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.无需使用 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] 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 . Android 7.0 引入了cmd (一种新的基于原生代码的)工具,它允许与像PackageManager这样的 Android 服务进行交互,比像pm这样的基于 Java 字节码的旧工具要快得多。 So for recent Android versions instead of adb shell pm list packages <package.name.substring> you should use因此,对于最近的 Android 版本而不是adb shell pm list packages <package.name.substring>您应该使用

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

Taking into account @Joel answer, just do it in one-liner考虑到@Joel 的回答,只需单线即可

# 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如果您正在使用 adb,请使用 First adb shell命令,否则您可以通过su命令在同一设备中使用 root

Then, we can get path of installed apk files by their pkg names lets say in our case pkg name is com.tencent.ig然后,我们可以通过它们的 pkg 名称获取已安装 apk 文件的路径,假设在我们的例子中 pkg 名称是 com.tencent.ig

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

If app installed : You will get the path to the apk file如果安装了应用程序:您将获得 apk 文件的路径

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 :以前的答案是有限的,但通过使用上述逻辑,我们也可以轻松地使用 if 语句,例如:

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.将 your.package.name 替换为所需的包名称。

If app installed : You will get the path to the apk file如果安装了应用程序:您将获得 apk 文件的路径

If app not installed : You will get nothing如果未安装应用程序:您将一无所获

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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