diff --git a/adb/adb_commands.py b/adb/adb_commands.py index fae84f9..e0fe15f 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -109,7 +109,7 @@ def Devices(cls): def GetState(self): return self._device_state - def Install(self, apk_path, destination_dir='', timeout_ms=None): + def Install(self, apk_path, destination_dir='', replace_existing=True, timeout_ms=None): """Install an apk to the device. Doesn't support verifier file, instead allows destination directory to be @@ -119,6 +119,7 @@ def Install(self, apk_path, destination_dir='', timeout_ms=None): apk_path: Local path to apk to install. destination_dir: Optional destination directory. Use /system/app/ for persistent applications. + replace_existing: whether to replace existing application timeout_ms: Expected timeout for pushing and installing. Returns: @@ -129,8 +130,30 @@ def Install(self, apk_path, destination_dir='', timeout_ms=None): basename = os.path.basename(apk_path) destination_path = destination_dir + basename self.Push(apk_path, destination_path, timeout_ms=timeout_ms) - return self.Shell('pm install -r "%s"' % destination_path, - timeout_ms=timeout_ms) + + cmd = ['pm install'] + if replace_existing: + cmd.append('-r') + cmd.append('"%s"' % destination_path) + + return self.Shell(' '.join(cmd), timeout_ms=timeout_ms) + + def Uninstall(self, package_name, keep_data=False, timeout_ms=None): + """Removes a package from the device. + + Args: + package_name: Package name of target package. + keep_data: whether to keep the data and cache directories + timeout_ms: Expected timeout for pushing and installing. + + Returns: + The pm uninstall output. + """ + cmd = ['pm uninstall'] + if keep_data: + cmd.append('-k') + cmd.append('"%s"' % package_name) + return self.Shell(' '.join(cmd), timeout_ms=timeout_ms) def Push(self, source_file, device_filename, mtime='0', timeout_ms=None): """Push a file or directory to the device. diff --git a/adb/adb_debug.py b/adb/adb_debug.py index f396bdb..b590bc6 100755 --- a/adb/adb_debug.py +++ b/adb/adb_debug.py @@ -123,6 +123,7 @@ def main(): common_cli.MakeSubparser( subparsers, parents, adb_commands.AdbCommands.Install) + common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.Uninstall) common_cli.MakeSubparser(subparsers, parents, List) common_cli.MakeSubparser(subparsers, parents, Logcat) common_cli.MakeSubparser( diff --git a/test/adb_test.py b/test/adb_test.py index 29bce3a..1b8800c 100755 --- a/test/adb_test.py +++ b/test/adb_test.py @@ -116,6 +116,15 @@ def testBigResponseShell(self): self.assertEqual(b''.join(responses).decode('utf8'), adb_commands.Shell(command)) + def testUninstall(self): + package_name = "com.test.package" + response = 'Success' + + usb = self._ExpectCommand(b'shell', ('pm uninstall "%s"' % package_name).encode('utf8'), response) + + adb_commands = self._Connect(usb) + self.assertEquals(response, adb_commands.Uninstall(package_name)) + def testStreamingResponseShell(self): command = b'keepin it real big' # expect multiple lines