Skip to content

Commit c6692ad

Browse files
authored
Add enable_runtime_perms flag to APK install helper function (#995)
Allows installing an APK without runtime permissions automatically granted in order to test permissions flows accurately.
1 parent c8bdffe commit c6692ad

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

mobly/controllers/android_device_lib/apk_utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
import io
16-
from typing import Iterable, Optional
16+
from typing import Iterable
1717

1818
from mobly import utils
1919
from mobly.controllers.android_device import AndroidDevice
@@ -80,8 +80,9 @@ def install(
8080
device: AndroidDevice,
8181
apk_path: str,
8282
timeout: int = DEFAULT_TIMEOUT_INSTALL_APK_SEC,
83-
user_id: Optional[int] = None,
84-
params: Optional[Iterable[str]] = None,
83+
user_id: int | None = None,
84+
params: Iterable[str] | None = None,
85+
enable_runtime_perms: bool = True,
8586
) -> None:
8687
"""Install an apk on an Android device.
8788
@@ -97,6 +98,7 @@ def install(
9798
install for the current user by default. Android's multi-user support
9899
did not realistically work until SDK 24.
99100
params: string list, additional parameters included in the adb install cmd.
101+
enable_runtime_perms: bool, Set the `-g` flag, which allows all runtime permissions.
100102
101103
Raises:
102104
AdbError: Installation failed.
@@ -111,7 +113,7 @@ def install(
111113
if user_id is None:
112114
user_id = device.adb.current_user_id
113115
args = ['--user', str(user_id)] + args
114-
if android_api_version >= 23:
116+
if android_api_version >= 23 and enable_runtime_perms:
115117
args.append('-g')
116118
if android_api_version >= 17:
117119
args.append('-d')

tests/mobly/controllers/android_device_lib/apk_utils_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ def test_install_with_params(self):
8686
stderr=mock.ANY,
8787
)
8888

89+
def test_install_no_enable_perms(self):
90+
self.mock_device.build_info = {'build_version_sdk': 23}
91+
self.mock_device.adb.getprop.return_value = 'none'
92+
apk_utils.install(self.mock_device, APK_PATH, enable_runtime_perms=False)
93+
self.mock_device.adb.install.assert_called_with(
94+
['-r', '-t', '-d', APK_PATH],
95+
timeout=DEFAULT_INSTALL_TIMEOUT_SEC,
96+
stderr=mock.ANY,
97+
)
98+
8999
def test_install_with_user_id_sdk_too_low(self):
90100
self.mock_device.build_info = {'build_version_sdk': 21}
91101
with self.assertRaisesRegex(

0 commit comments

Comments
 (0)