Home  >  Article  >  Backend Development  >  Use Python scripts and ADB commands to uninstall apps

Use Python scripts and ADB commands to uninstall apps

高洛峰
高洛峰Original
2017-02-13 13:29:102162browse

Preface

This article implements a Python script to batch uninstall apps on the simulator or physical machine and clear the LogCat cache.

Friends who develop Android, there are often a large number of debugging demos in the simulator or mobile phone. This is fine for mobile phones, but for the simulator, it may cause a decrease in debugging speed and startup speed. Moreover, it is very troublesome to delete apps one by one in the simulator. Using ADB commands, we can do many things, including batch operations on the simulator or apps on the mobile phone. Of course, this includes deletion operations. Using Python scripts, ADB shell commands and the CMD window that comes with AS, we can condense all this into a command line.

Core code

# 删除所有你指定包名的 APP
def delAllapp( ):
 print 'start delete all your app in your Phone or Simulator '
 os.popen('adb wait-for-device');
 corename = raw_input("input your app package corename:")
 oriPackages = os.popen('adb shell pm list packages {name}'.format(name=corename));
 # list all PackageName
 for oriPackage in oriPackages:
  deletePackage = oriPackage.split(':')[1]
  os.popen('adb uninstall ' + deletePackage );
  print deletePackage + "is deleted"
  
# 删除所有你指定包名的特定 APP
def listAllpackage( ):
 i = 0
 os.popen('adb wait-for-device');
 corename = raw_input("input your app package corename:")
 oriPackages = os.popen('adb shell pm list packages {name}'.format(name=corename));
 
 for oriPackage in oriPackages:
  deletePackage = oriPackage.split(':')[1]
  print str(i) + ":" + deletePackage
  deleteList.append(deletePackage)
  i += 1

# 删除指定 App
def deleteApp(number):
 os.popen('adb uninstall ' + deleteList[number] );
 print 'delete '+ deleteList[number] + "success"
 
# 清除 LogCat 缓存 
def clearLogcat( ):
 print 'start clear logcat buffer in your Phone or Simulator'
 os.popen('adb wait-for-device');
 os.popen('adb logcat -c');
 print 'logcat is cleared success'

Effect implementation

Use Python scripts and ADB commands to uninstall apps

##How to use

  1. Make sure your AS can use the ADB command

  2. Configure the Python 2.7 environment (3+ should be no problem)

  3. Provided in AS Find the current script path in CMD and enter:

    python unistall.py

  4. Enter the core keyword of the package you want to delete the App according to the command prompt, such as:

    com.example.RxCacheDemo, just enter example (this configuration of everyone's AS should be the same)

  5. After completing the above steps, there will be a prompt that the deletion is successful and no.

For more articles related to using Python scripts and ADB commands to uninstall apps, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn