Skip to content

Commit 69754a7

Browse files
MohammadAGfahhem
authored andcommitted
Added app uninstallation feature and make -r in installation optional (google#81)
* Added app uninstallation feature * Make -r optional in app installation * Fixed unit test * Changed variable name * Change quotes for consistency
1 parent 209d8fa commit 69754a7

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

adb/adb_commands.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def Devices(cls):
109109
def GetState(self):
110110
return self._device_state
111111

112-
def Install(self, apk_path, destination_dir='', timeout_ms=None):
112+
def Install(self, apk_path, destination_dir='', replace_existing=True, timeout_ms=None):
113113
"""Install an apk to the device.
114114
115115
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):
119119
apk_path: Local path to apk to install.
120120
destination_dir: Optional destination directory. Use /system/app/ for
121121
persistent applications.
122+
replace_existing: whether to replace existing application
122123
timeout_ms: Expected timeout for pushing and installing.
123124
124125
Returns:
@@ -129,8 +130,30 @@ def Install(self, apk_path, destination_dir='', timeout_ms=None):
129130
basename = os.path.basename(apk_path)
130131
destination_path = destination_dir + basename
131132
self.Push(apk_path, destination_path, timeout_ms=timeout_ms)
132-
return self.Shell('pm install -r "%s"' % destination_path,
133-
timeout_ms=timeout_ms)
133+
134+
cmd = ['pm install']
135+
if replace_existing:
136+
cmd.append('-r')
137+
cmd.append('"%s"' % destination_path)
138+
139+
return self.Shell(' '.join(cmd), timeout_ms=timeout_ms)
140+
141+
def Uninstall(self, package_name, keep_data=False, timeout_ms=None):
142+
"""Removes a package from the device.
143+
144+
Args:
145+
package_name: Package name of target package.
146+
keep_data: whether to keep the data and cache directories
147+
timeout_ms: Expected timeout for pushing and installing.
148+
149+
Returns:
150+
The pm uninstall output.
151+
"""
152+
cmd = ['pm uninstall']
153+
if keep_data:
154+
cmd.append('-k')
155+
cmd.append('"%s"' % package_name)
156+
return self.Shell(' '.join(cmd), timeout_ms=timeout_ms)
134157

135158
def Push(self, source_file, device_filename, mtime='0', timeout_ms=None):
136159
"""Push a file or directory to the device.

adb/adb_debug.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def main():
123123

124124
common_cli.MakeSubparser(
125125
subparsers, parents, adb_commands.AdbCommands.Install)
126+
common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.Uninstall)
126127
common_cli.MakeSubparser(subparsers, parents, List)
127128
common_cli.MakeSubparser(subparsers, parents, Logcat)
128129
common_cli.MakeSubparser(

test/adb_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ def testBigResponseShell(self):
116116
self.assertEqual(b''.join(responses).decode('utf8'),
117117
adb_commands.Shell(command))
118118

119+
def testUninstall(self):
120+
package_name = "com.test.package"
121+
response = 'Success'
122+
123+
usb = self._ExpectCommand(b'shell', ('pm uninstall "%s"' % package_name).encode('utf8'), response)
124+
125+
adb_commands = self._Connect(usb)
126+
self.assertEquals(response, adb_commands.Uninstall(package_name))
127+
119128
def testStreamingResponseShell(self):
120129
command = b'keepin it real big'
121130
# expect multiple lines

0 commit comments

Comments
 (0)