Android Studio:如何在运行或调试之前自动卸载 APK(或执行 adb 命令)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25927124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio : How to uninstall APK (or execute adb command) automatically before Run or Debug?
提问by Jagger
Now I need to uninstall the App every time before Run\Debug
it in Android Studio. Because I need to re-create the database before I run \debug the app. I know I can run the command
现在我每次都需要Run\Debug
在 Android Studio 中卸载该应用程序。因为我需要在运行 \debug 应用程序之前重新创建数据库。我知道我可以运行命令
adb uninstall [package_name]
in terminal to clear the files under /data/data/[package_name]
. But it's not convenient way if I have to execute the command every time. I hope the
在终端中清除/data/data/[package_name]
. 但是如果我每次都必须执行命令,这不是很方便的方法。我希望
adb uninstall
command can be executed automatically when I click Run\Debug
button.
单击Run\Debug
按钮时可以自动执行命令。
回答by aravindsagar
adb uninstall <package_name>
can be used to uninstall an app via your PC. If you want this to happen automatically every time you launch your app via Android Studio, you can do this:
可用于通过您的 PC 卸载应用程序。如果您希望每次通过 Android Studio 启动应用程序时都自动发生这种情况,您可以执行以下操作:
- In Android Studio, click the drop down list to the left of Run button, and select Edit configurations...
- Click on app under Android Application, and in General Tab, find the heading 'Before Launch'
- Click the + button, select Run external tool, click the + button in the popup window.
- Give some name (Eg adb uninstall) and description, and type
adb
in Program: anduninstall <your-package-name>
in Parameters:. Make sure that the new item is selected when you click Ok in the popup window.
- 在 Android Studio 中,单击运行按钮左侧的下拉列表,然后选择编辑配置...
- 单击 Android 应用程序下的应用程序,然后在常规选项卡中找到标题“启动前”
- 单击+按钮,选择运行外部工具,在弹出窗口中单击+按钮。
- 给出一些名称(例如 adb uninstall)和描述,然后输入
adb
程序:和uninstall <your-package-name>
参数:。确保在弹出窗口中单击“确定”时选择了新项目。
Note: If you do not have adb in your PATH environment variable, give the full path to adb in Program: field (eg /home/user/android/sdk/platform-tools/adb).
注意:如果您的 PATH 环境变量中没有 adb,请在 Program: 字段中提供 adb 的完整路径(例如 /home/user/android/sdk/platform-tools/adb)。
回答by Joolah
example
例子
adb uninstall com.my.firstapp
回答by kamran khader
List the packages by:
通过以下方式列出软件包:
adb shell su 0 pm list packages
Review which package you want to uninstall and copy the package name from there. For example:
查看要卸载的软件包并从那里复制软件包名称。例如:
com.android.calculator2
Lastly type in:
最后输入:
adb uninstall com.android.calculator2
and you are done.
你就完成了。
回答by Jimmy
I am using Android Studio 2.1.2
. I had same requirement as OP. Though above two answer seemed to help everyone, it did not work for me . I am sharing what worked for me.
我正在使用Android Studio 2.1.2
. 我和OP有同样的要求。虽然以上两个答案似乎对每个人都有帮助,但对我来说却不起作用。我正在分享对我有用的东西。
Go to main menu/Run/Edit Configuration
. Select app
under Android Application
on the left.This should open multi-tabbed pane . Select General
tab ( would be default), click green +
sing at the bottom ( below text Before launch: Gradle -awake ...
).
去main menu/Run/Edit Configuration
。在左侧选择app
下Android Application
。这应该打开多选项卡窗格。选择General
选项卡(将是默认值),单击+
底部的绿色唱歌(文本下方Before launch: Gradle -awake ...
)。
A drop down will appear, select Gradle-aware-make
option. Another text box will pop up. enter :app:uninstallAll
in this text box . (You can use ctrl
+ space
to use autocomplete todetermine right target without typing everything . And also helps you choose the right app name that is avaiable for you).
and set apply/ok
. Relaunch your app.
将出现一个下拉列表,选择Gradle-aware-make
选项。将弹出另一个文本框。:app:uninstallAll
在此文本框中输入。(您可以使用ctrl
+space
使用自动完成功能来确定正确的目标,而无需输入所有内容。还可以帮助您选择适合您的正确应用程序名称)。并设置apply/ok
。重新启动您的应用。
Note : Every time you launch your app now , this new target will try to uninstall your app from your emulator or device. So if your testing device is not available, your launc will probably fail while uninstalling but will continue to start your emulator. So Either start your emulator first, or re-lauch after first fail again ( as first launch will start emulator though uninstall fails).
注意:每次您现在启动您的应用程序时,这个新目标都会尝试从您的模拟器或设备上卸载您的应用程序。因此,如果您的测试设备不可用,您的 launc 可能会在卸载时失败,但会继续启动您的模拟器。所以要么先启动你的模拟器,要么在第一次失败后重新启动(因为第一次启动将启动模拟器,但卸载失败)。
回答by summerGhost
If you want to uninstall when connected to single device/emulator then use below command
如果要在连接到单个设备/模拟器时卸载,请使用以下命令
adb uninstall <package name>
else with multiple devices then use below command
否则有多个设备然后使用下面的命令
adb -s <device ID> uninstall <package name>
回答by eaccmk
A simple three-step process (checked on mac terminal)
一个简单的三步流程(在mac终端上查看)
Connect your android device (please connect 1 android Device at a time), preferably by a cable & Confirm connection by (this will list Device's ID device ID)
adb devices
Then to list all app packages on the connected device by running, on terminal
adb shell pm list packages -f -3
Then uninstall as explained earlier
adb uninstall <package_name>
连接您的 android 设备(请一次连接 1 个 android 设备),最好通过电缆和确认连接(这将列出设备的 ID 设备 ID)
adb devices
然后通过在终端上运行列出连接设备上的所有应用程序包
adb shell pm list packages -f -3
然后按照前面的解释卸载
adb uninstall <package_name>
回答by Joel Butler
I use an Android Studio plug-in called "adb idea" -- has a drop down menu for various functions (Uninstall, Kill, Start, etc) that you can target at any connected or simulated device. One could argue it takes me a step away from having a deeper understanding of the power of adb commands and I'd probably agree....though I'm really operating at a lower level of understanding anyway so for me it helps to have a helper. ADB Idea
我使用了一个名为“adb idea”的 Android Studio 插件——有一个下拉菜单,用于各种功能(卸载、终止、启动等),您可以将这些功能定位到任何连接或模拟的设备上。有人可能会争辩说,这让我距离对 adb 命令的功能有更深入的了解还有一步之遥,我可能会同意......尽管我的理解水平确实较低,所以对我来说有帮助一个帮手。亚行理念