Android开发-使用pm命令进行package的管理,包括apk的安装和卸载(pm install && pm uninstall)

pm命令简介:

pm全称是package manager,是一个可执行程序。顾名思义,pm命令用来进行package的管理,例如安装apk,卸载apk,列出所有的应用的包名,列出权限等。

pm在Android系统中的源码位置:

frameworks\base\cmds\am\src\com\android\commands\pm

pm help:

在adb shell命令行,输入pm或者pm help,可以列出pm的使用帮助信息,部分信息如下:

% adb shell

shell@bullhead:/ $ pm

usage: pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]

       pm list permission-groups

       pm list permissions [-g] [-f] [-d] [-u] [GROUP]

       pm list instrumentation [-f] [TARGET-PACKAGE]

       pm list features

       pm list libraries

       pm list users

       pm path PACKAGE

       pm dump PACKAGE

       pm install [-lrtsfd] [-i PACKAGE] [--user USER_ID] [PATH]

       pm install-create [-lrtsfdp] [-i PACKAGE] [-S BYTES]

               [--install-location 0/1/2]

               [--force-uuid internal|UUID]

       pm install-write [-S BYTES] SESSION_ID SPLIT_NAME [PATH]

       pm install-commit SESSION_ID

       pm install-abandon SESSION_ID

       pm uninstall [-k] [--user USER_ID] PACKAGE

       pm set-installer PACKAGE INSTALLER

       pm move-package PACKAGE [internal|UUID]

       pm move-primary-storage [internal|UUID]

       pm clear [--user USER_ID] PACKAGE

       pm enable [--user USER_ID] PACKAGE_OR_COMPONENT

       pm disable [--user USER_ID] PACKAGE_OR_COMPONENT

       pm disable-user [--user USER_ID] PACKAGE_OR_COMPONENT

       pm disable-until-used [--user USER_ID] PACKAGE_OR_COMPONENT

       pm hide [--user USER_ID] PACKAGE_OR_COMPONENT

       pm unhide [--user USER_ID] PACKAGE_OR_COMPONENT

       pm grant [--user USER_ID] PACKAGE PERMISSION

       pm revoke [--user USER_ID] PACKAGE PERMISSION

       pm reset-permissions

       pm set-app-link [--user USER_ID] PACKAGE {always|ask|never|undefined}

       pm get-app-link [--user USER_ID] PACKAGE

       pm set-install-location [0/auto] [1/internal] [2/external]

       pm get-install-location

       pm set-permission-enforced PERMISSION [true|false]

       pm trim-caches DESIRED_FREE_SPACE [internal|UUID]

       pm create-user [--profileOf USER_ID] [--managed] USER_NAME

       pm remove-user USER_ID

       pm get-max-users

pm list packages: prints all packages, optionally only

  those whose package name contains the text in FILTER.  Options:

    -f: see their associated file.

    -d: filter to only show disbled 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.

    -u: also include uninstalled packages.

pm list permission-groups: prints all known permission groups.

pm list permissions: prints all known permissions, optionally only

  those in GROUP.  Options:

    -g: organize by group.

    -f: print all information.

    -s: short summary.

    -d: only list dangerous permissions.

    -u: list only the permissions users will see.

pm list instrumentation: use to list all test packages; optionally

  supply <TARGET-PACKAGE> to list the test packages for a particular

  application.  Options:

    -f: list the .apk file for the test package.

pm list features: prints all features of the system.

pm list users: prints all users on the system.

pm path: print the path to the .apk of the given PACKAGE.

pm dump: print system state associated with the given PACKAGE.

pm install: install a single legacy package

pm install-create: create an install session

    -l: forward lock application

    -r: replace existing application

    -t: allow test packages

    -i: specify the installer package name

    -s: install application on sdcard

    -f: install application on internal flash

    -d: allow version code downgrade

    -p: partial application install

    -g: grant all runtime permissions

    -S: size in bytes of entire session

pm install-write: write a package into existing session; path may

  be '-' to read from stdin

    -S: size in bytes of package, required for stdin

pm install-commit: perform install of fully staged session

pm install-abandon: abandon session

pm set-installer: set installer package name

pm uninstall: removes a package from the system. Options:

    -k: keep the data and cache directories around after package removal.

pm clear: deletes all data associated with a package.

pm enable, disable, disable-user, disable-until-used: these commands

  change the enabled state of a given package or component (written

  as "package/class").

pm grant, revoke: these commands either grant or revoke permissions

    to apps. The permissions must be declared as used in the app's

    manifest, be runtime permissions (protection level dangerous),

    and the app targeting SDK greater than Lollipop MR1.

pm reset-permissions: revert all runtime permissions to their default state.

pm get-install-location: returns the current install location.

    0 [auto]: Let system decide the best location

    1 [internal]: Install on internal device storage

    2 [external]: Install on external media

pm set-install-location: changes the default install location.

  NOTE: this is only intended for debugging; using this can cause

  applications to break and other undersireable behavior.

    0 [auto]: Let system decide the best location

    1 [internal]: Install on internal device storage

    2 [external]: Install on external media

pm trim-caches: trim cache files to reach the given free space.

pm create-user: create a new user with the given USER_NAME,

  printing the new user identifier of the user.

pm remove-user: remove the user with the given USER_IDENTIFIER,

  deleting all data associated with that user

pm命令常用参数说明:

pm命令的格式:

pm [list|path|install|uninstall] 

说明:

list用于查询

install用户安装

uninstall用于卸载

 (1)查询:

pm list:

参数

描述

-f显示每个包的文件位置
-d使用过滤器,只显示禁用的应用的包名
-e使用过滤器,只显示可用的应用的包名
-s使用过滤器,只显示系统应用的包名
-3使用过滤器,只显示第三方应用的包名
-i查看应用的安装者

例如:

adb shell pm list instrumentation

adb shell pm list instrumentation TARGET_PACKAGE

pm命令常用参数:

(2)安装:

pm install [-lrtsfd] [-i PACKAGE] [PATH]

通过指定路径安装apk到手机中(与adb install不同的是adb install安装的.apk是在你的电脑上,而pm install安装的apk是存储在你的手机中)

参数

说明

-l锁定应用程序
-r重新安装应用,且保留应用数据
-t允许测试apk被安装
-i <INSTALLER_PACKAGE_NAME>指定安装包的包名
-s安装到sd卡
-f安装到系统内置存储中(默认安装位置)
-d允许降级安装(同一应用低级换高级)
-g授予应用程序清单中列出的所有权限(只有6.0系统可用)

(3) 卸载:

pm uninstall [options] <PACKAGE>

#卸载应用

参数

说明

-k卸载应用且保留数据与缓存(如果不加-k则全部删除)

(4)其它命令:

1. pm dump PACKAGE  打印给定的包的系统状态

2. list feature    打印系统的所有功能
                        列出所有硬件相关信息
3. list libraries    打印当前设备所支持的所有库 

4. pm clear <PACKAGE_NAME> 清除应用数据

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android 代码中使用 pm 命令安装 APK 的方法如下: 1. 在 Android 设备上启用开发者选项。您可以在设备设置中找到此选项,并启用它。 2. 在 Android 代码中,使用以下命令执行 pm 命令: ```java Process process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); os.writeBytes("pm install /path/to/apk/file.apk\n"); os.flush(); ``` 其中,`/path/to/apk/file.apk` 是您要安装APK 文件的路径。请注意,您需要将此路径替换为实际的 APK 文件路径。 如果您不知道 APK 文件的路径,可以使用以下命令列出设备上所有已安装的应用程序的名: ```java Process process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); os.writeBytes("pm list packages\n"); os.flush(); ``` 这将返回一个含所有已安装应用程序的名列表。您可以在列表中找到您要安装的应用程序的名,并使用以下命令卸载它: ```java Process process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); os.writeBytes("pm uninstall package_name\n"); os.flush(); ``` 其中,`package_name` 是应用程序的名,可以在 AndroidManifest.xml 文件中找到。 请注意,在使用 pm 命令安装卸载应用程序之前,请确保您已经连接了 Android 设备并启用了 USB 调试选项。您可以在设备设置中找到此选项,并启用它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值