每天一個安卓測試開發(fā)小知識之 (五)---常用的adb shell命令第三期 pm命令第二期
每天一個安卓測試開發(fā)小知識之 (五)---常用的adb shell命令第三期 pm命令第二期
上一期我們簡單介紹了pm 命令的安裝卸載,以及查看已安裝的app的功能。本期繼續(xù)介紹
- 查看已安裝的第三方包
- 獲取app的啟動activity
- 清除應(yīng)用數(shù)據(jù)
- 啟用被禁用或者禁用 應(yīng)用/組件
- 隱藏/展示應(yīng)用icon
- 授予應(yīng)用權(quán)限,以及查看app聲明的權(quán)限
1. 查看已安裝的第三方包
包分為設(shè)備制造商應(yīng)用(第一方應(yīng)用)和第三方應(yīng)用,例如
- 手機(jī)里的設(shè)置、電話、通訊錄就屬于第一方應(yīng)用
- 微信、qq、支付寶 就屬于第三方應(yīng)用
adb shell pm list packages 會列出已安裝的包名,如何獲取到已安裝的第三方的包,查看pm命令的幫助可以看到
list packages [-f] [-d] [-e] [-s] [-q] [-3] [-i] [-l] [-u] [-U]
[--show-versioncode] [--apex-only] [--factory-only]
[--uid UID] [--user USER_ID] [FILTER]
Prints all packages; optionally only those whose name contains
the text in FILTER. Options are:
-f: see their associated file
-a: all known packages (but excluding APEXes)
-d: filter to only show disabled packages
-e: filter to only show enabled packages
-s: filter to only show system packages
-3: filter to only show third party packages
-i: see the installer for the packages
-l: ignored (used for compatibility with older releases)
-U: also show the package UID
-u: also include uninstalled packages
--show-versioncode: also show the version code
--apex-only: only show APEX packages
--factory-only: only show system packages excluding updates
--uid UID: filter to only show packages with the given UID
--user USER_ID: only list packages belonging to the given user
--match-libraries: include packages that declare static shared and SDK libraries
有很多參數(shù),例如 -f -a -e -s -3 等,具體的參數(shù)含義可以自行查看,只在這里起一個拋磚引玉的作用。
-3 查看第三方應(yīng)用

2. 獲取app的啟動activity
在做自動化測試時,經(jīng)常需要啟動應(yīng)用,可以通過adb命令打開應(yīng)用的啟動activity,或者UI點(diǎn)擊app的icon
例如獲取小米天氣的啟動activity
adb shell pm dump-package com.miui.weather2
com.miui.weather2 小米天氣的包名,執(zhí)行命令可以看到很多輸出,我們需要重點(diǎn)過濾 android.intent.action.MAIN: 的下一行,來獲取啟動activity。以linux為例 執(zhí)行以下命令
adb shell pm dump-package com.miui.weather2 | grep -A 2 -B 2 "android.intent.action.MAIN:"

小米天氣的啟動activity就是 com.miui.weather2/.ActivityWeatherMain,打開activity的命令就是 adb shell am start 啟動activity

執(zhí)行該命令就會在小米手機(jī)上打開天氣,am命令將后續(xù)進(jìn)行介紹
3. 清除應(yīng)用數(shù)據(jù)
adb shell pm clear packageName 該操作不僅會清除app的全部數(shù)據(jù),還會殺死app進(jìn)程

同樣pm clear命令也有參數(shù)
clear [--user USER_ID] [--cache-only] PACKAGE
Deletes data associated with a package. Options are:
--user: specifies the user for which we need to clear data
--cache-only: a flag which tells if we only need to clear cache data
--cache-only 只清除緩存數(shù)據(jù),不殺死進(jìn)程

4. 啟用/禁用應(yīng)用/組件
4.1 安卓四大組件
組件名稱 | 中文名 | 主要作用
Activity | 活動 | 提供用戶界面,與用戶交互
Service | 服務(wù) | 在后臺執(zhí)行長時間運(yùn)行操作
Broadcast Receiver | 廣播接收器 | 響應(yīng)系統(tǒng)范圍內(nèi)的廣播消息
Content Provider | 內(nèi)容提供器 | 管理應(yīng)用數(shù)據(jù)的共享
四大組件的詳細(xì)介紹,后續(xù)文章給出
4.2 啟用被禁用的應(yīng)用/組件
adb shell pm enable packageName/componentName packageName/componentName 應(yīng)用/組件
4.3 禁用的應(yīng)用/組件
adb shell pm disenable packageName/componentName packageName/componentName 應(yīng)用/組件
5. 隱藏/展示應(yīng)用icon
adb shell pm hide com.android.settings 隱藏設(shè)置的icon 【icon 應(yīng)用的啟動圖標(biāo)】
adb shell pm unhide com.android.settings 顯示設(shè)置的icon

6. 授予應(yīng)用權(quán)限
adb shell pm grant perssionName perssionName權(quán)限名稱
例如授予天氣 發(fā)送通知的權(quán)限 android.permission.POST_NOTIFICATIONS

授予app權(quán)限時需要保證app unhide
6.1 如何查詢app需要授予哪些權(quán)限
adb shell pm dump-package packageName 命令過濾出關(guān)鍵字 permission
例如 獲取天氣應(yīng)用聲明的權(quán)限
adb shell pm dump-package com.miui.weather2 | grep -i permission
mi@mi-ThinkCentre-M760t:~$ adb shell pm dump-package com.miui.weather2 | grep -i permission
1ddc1d3 com.miui.weather2/.backup.cloud.WeatherCloudBackupService filter 8ac0110 permission com.xiaomi.permission.CLOUD_MANAGER
1ddc1d3 com.miui.weather2/.backup.cloud.WeatherCloudBackupService filter 8ac0110 permission com.xiaomi.permission.CLOUD_MANAGER
Permissions:
Permission [com.miui.weather2.permission.MIPUSH_RECEIVE] (8f2e443):
perm=PermissionInfo{58b4f95 com.miui.weather2.permission.MIPUSH_RECEIVE}
Permissions:
Permission [com.miui.weather2.permission.WRITE_DB] (fc9cac0):
perm=PermissionInfo{e295c9b com.miui.weather2.permission.WRITE_DB}
installPermissionsFixed=false
declared permissions:
com.miui.weather2.permission.WRITE_DB: prot=signature
com.miui.weather2.permission.MIPUSH_RECEIVE: prot=signature
requested permissions:
android.permission.POST_NOTIFICATIONS
android.permission.ACCESS_FINE_LOCATION
miui.permission.USE_INTERNAL_GENERAL_API
android.permission.FOREGROUND_SERVICE
android.permission.READ_MEDIA_VISUAL_USER_SELECTED
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.FOREGROUND_SERVICE_SPECIAL_USE
android.permission.GET_TASKS
android.permission.INTERNET
com.miui.securitycenter.permission.SYSTEM_PERMISSION_DECLARE
android.permission.ACCESS_COARSE_LOCATION
com.xiaomi.mihomemanager.permission.receiveNormalBroadcast
com.miui.weather2.permission.MIPUSH_RECEIVE
android.permission.READ_MEDIA_IMAGES
android.permission.CHANGE_WIFI_STATE
android.permission.ACCESS_NETWORK_STATE
android.permission.SCHEDULE_EXACT_ALARM
com.miui.weather2.permission.WRITE_DB
android.permission.VIBRATE
com.miui.securitycenter.permission.PERM_USE_INFO
android.permission.ACCESS_WIFI_STATE
android.permission.QUERY_ALL_PACKAGES
android.permission.WAKE_LOCK
miui.permission.EXTRA_NETWORK
android.permission.ACCESS_BACKGROUND_LOCATION
install permissions:
android.permission.FOREGROUND_SERVICE: granted=true
android.permission.RECEIVE_BOOT_COMPLETED: granted=true
android.permission.FOREGROUND_SERVICE_SPECIAL_USE: granted=true
android.permission.GET_TASKS: granted=true
android.permission.INTERNET: granted=true
com.xiaomi.mihomemanager.permission.receiveNormalBroadcast: granted=true
com.miui.weather2.permission.MIPUSH_RECEIVE: granted=true
android.permission.CHANGE_WIFI_STATE: granted=true
android.permission.ACCESS_NETWORK_STATE: granted=true
com.miui.weather2.permission.WRITE_DB: granted=true
android.permission.VIBRATE: granted=true
com.miui.securitycenter.permission.PERM_USE_INFO: granted=true
android.permission.ACCESS_WIFI_STATE: granted=true
android.permission.QUERY_ALL_PACKAGES: granted=true
android.permission.WAKE_LOCK: granted=true
runtime permissions:
android.permission.POST_NOTIFICATIONS: granted=true, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
android.permission.ACCESS_FINE_LOCATION: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
android.permission.READ_MEDIA_VISUAL_USER_SELECTED: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
android.permission.ACCESS_COARSE_LOCATION: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
android.permission.READ_MEDIA_IMAGES: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
android.permission.ACCESS_BACKGROUND_LOCATION: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED|RESTRICTION_UPGRADE_EXEMPT]
會發(fā)現(xiàn)有些權(quán)限上 granted=false ,對于這些權(quán)限就可以通過 adb shell pm grant 命令授予權(quán)限,授予完后 granted=true
今天先介紹到這里,每天進(jìn)步一點(diǎn)點(diǎn)!!
浙公網(wǎng)安備 33010602011771號