admin 管理员组

文章数量: 887021

使用 adb shell 和 pm 安装 apk 包到安卓手机

安装好 Android Studio 之后,adb 一般是在 /Users/exchen/Library/Android/sdk/platform-tools 目录

设置好环境变量或者切换到 adb 所在的目录,执行 adb devices 查看本机连接的设备,命令如下:

./adb devices

List of devices attached

0123456789ABCDEF device

emulator-5554 device

1

2

3

4

5

./adbdevices

Listofdevicesattached

0123456789ABCDEFdevice

emulator-5554device

可以看到上面的信息显示连接了两台设备,一个是真机,一个是模拟器,由于有两台设备执行操作时需要指定目标设备,比如安装 apk,命令如下:

./adb -s 0123456789ABCDEF install ~/Downloads/test.apk

Performing Push Install

adb: error: failed to copy '/Users/exchen/Downloads/test.apk' to '/data/local/tmp/test.apk': couldn't read from device

/Users/momo/Downloads/test.apk: 0 files pushed. 92.5 MB/s (131056 bytes in 0.001s)

1

2

3

4

5

./adb-s0123456789ABCDEFinstall~/Downloads/test.apk

PerformingPushInstall

adb:error:failedtocopy'/Users/exchen/Downloads/test.apk'to'/data/local/tmp/test.apk':couldn'treadfromdevice

/Users/momo/Downloads/test.apk:0filespushed.92.5MB/s(131056bytesin0.001s)

此时我们看到安装失败,原因是上传 apk 包到 /data/local/tmp/ 目录失败,执行 adb shell,尝试执行 chmod 赋予权限,但是提示拒绝访问,信息如下:

./adb -s 0123456789ABCDEF shell

shell@Coolpad5367:/ $ chmod -R 755 data

Unable to chmod data: Permission denied

1

2

3

4

./adb-s0123456789ABCDEFshell

shell@Coolpad5367:/$chmod-R755data

Unabletochmoddata:Permissiondenied

没办法,看样子这个目录是没权限读写,尝试将 test.apk 推送到 SD 卡,命令如下:

./adb -s 0123456789ABCDEF push ~/Downloads/test.apk /sdcard

1

2

./adb-s0123456789ABCDEFpush~/Downloads/test.apk/sdcard

推送成功,SD 卡是有读写权限的,在手机上尝试用文件管理点击安装,但是安装的按钮是灰色的,不能点击。最后尝试使用 pm 命令进行安装,命令如下:

./adb shell

shell@Coolpad5367:/ chmod 755 /sdcard/test.apk

shell@Coolpad5367:/ $ pm install -f /sdcard/test.apk

pkg: /sdcard/test.apk

Success

1

2

3

4

5

6

./adbshell

shell@Coolpad5367:/chmod755/sdcard/test.apk

shell@Coolpad5367:/$pminstall-f/sdcard/test.apk

pkg:/sdcard/test.apk

Success

终于成功。

本文标签: 手机 apk shell Android 包到安卓