在使用这些命令之前,请安装 Android SDK 命令行工具、平台工具,以及所需的平台或系统镜像。将 ANDROID_HOME 设置为 SDK 目录,并将其 cmdline-tools/latest/bin、platform-tools 和 emulator 目录加入 PATH。
Android Debug Bridge #
adb 将主机端客户端连接到本地 ADB 服务器和设备端的 adbd 守护进程。当有多个设备或模拟器可用时,始终使用 -s SERIAL 明确选择目标。
adb devices -l
adb -s SERIAL get-state
adb -s SERIAL shell getprop ro.build.fingerprint
adb -s SERIAL rebootadb devices -l 会将目标报告为 device、offline 或 unauthorized。要解决 unauthorized,请接受设备显示的 RSA 密钥确认提示;只有在本地服务器状态过期时,才使用 adb kill-server,然后执行 adb start-server。在生产版本中,shell 用户不是 root 用户。
软件包和文件 #
使用 --replace 安装 APK 可以保留其数据。对于拆分 APK,使用 install-multiple;对于软件包元数据和权限管理,从 shell 中使用 pm。
adb -s SERIAL install --replace app.apk
adb -s SERIAL install-multiple base.apk config.arm64_v8a.apk config.en.apk
adb -s SERIAL uninstall com.example.app
adb -s SERIAL shell pm path com.example.app
adb -s SERIAL push local.txt /sdcard/Download/
adb -s SERIAL pull /sdcard/Download/report.txt .adb push 和 pull 在 shell 可用的权限范围内运行。分区存储和应用私有目录可能会限制访问。仅对可调试应用使用 run-as PACKAGE。
Shell、日志和诊断 #
对于二进制或未经修改的命令输出,使用 exec-out,因为它不会分配伪终端。为避免传输无界的日志流,请在源端过滤 Logcat。
adb -s SERIAL shell cmd package list packages
adb -s SERIAL exec-out screencap -p > screenshot.png
adb -s SERIAL logcat -c
adb -s SERIAL logcat -v threadtime MyTag:D '*:S'
adb -s SERIAL bugreport report.zip
adb -s SERIAL shell dumpsys activity activitiesbugreport 会捕获日志和系统状态,并且可能包含敏感数据。dumpsys 会公开特定于服务的诊断输出;运行 adb shell dumpsys -l 可列出可用服务。
网络和无线调试 #
forward 使设备服务可以通过主机端口访问。reverse 使模拟器或通过 USB 连接的设备可以访问主机服务。
adb -s SERIAL forward tcp:8080 tcp:8080
adb -s SERIAL reverse tcp:3000 tcp:3000
adb pair HOST:PAIRING_PORT
adb connect HOST:CONNECT_PORT无线调试需要 Android 的已认证配对流程。仅在受信任的网络上使用,完成后使用 adb disconnect HOST:CONNECT_PORT 断开连接。
Fastboot #
fastboot 与引导加载程序通信,而不是与正在运行的 Android 系统通信。通过设备 UI 进入 fastboot 模式,或执行 adb reboot bootloader。命令集、可用分区和解锁策略因设备而异。
fastboot devices -l
fastboot getvar product
fastboot getvar current-slot
fastboot reboot fastboot使用 fastboot getvar all 检查引导加载程序变量,但部分设备会限制其输出。A/B 设备通过 current-slot 暴露槽位;请使用镜像软件包文档中的刷写流程,不要推断分区名称。
刷写和恢复 #
执行写入操作前,请确认所选设备、镜像哈希、分区名称、槽位和引导加载程序策略。
shasum -a 256 boot.img
fastboot flash boot boot.img
fastboot --set-active=a
fastboot reboot解锁、刷写关键分区以及更改槽位可能会擦除数据,或导致设备无法启动。不要对未知设备或为其他产品构建的镜像使用 fastboot flash。
其他 #
SDK 和虚拟设备管理 #
sdkmanager 用于安装 SDK 软件包,avdmanager 用于创建 Android Virtual Device(AVD)。
sdkmanager --list
sdkmanager "platform-tools" "platforms;android-35"
sdkmanager "system-images;android-35;google_apis;x86_64"
avdmanager create avd --name api-35 --package "system-images;android-35;google_apis;x86_64"使用 sdkmanager --licenses 接受许可证。在 CI 中固定软件包修订版本,因为不限定版本的软件包更新可能改变构建环境。
模拟器 #
emulator -list-avds
emulator -avd api-35 -no-snapshot
emulator -avd api-35 -wipe-data使用 -no-snapshot 进行干净启动;仅在有意重置状态时使用 -wipe-data。
APK 和 App Bundle 检查 #
apkanalyzer 无需安装应用即可读取 APK 元数据和字节码。
apkanalyzer manifest application-id app.apk
apkanalyzer apk summary app.apk
apkanalyzer dex packages app.apkbundletool 将 Android App Bundle 转换为可安装的 APK 集合,并选择特定于设备的拆分 APK。
java -jar bundletool-all.jar build-apks --bundle=app.aab --output=app.apks
java -jar bundletool-all.jar install-apks --apks=app.apks重现拆分选择时,在 get-device-spec 中使用 --connected-device。将签名密钥和生成的 .apks 归档视为发布构件。
Gradle #
Gradle wrapper 提供可复现的项目本地构建工具。
./gradlew assembleDebug
./gradlew installDebug
./gradlew lintDebug
./gradlew connectedDebugAndroidTest请运行 wrapper,而不是全局安装的 Gradle 版本。connectedDebugAndroidTest 需要可用的模拟器或通过 USB 连接的设备。
评论
在 GitHub 上阅读或评论(发表评论需要登录)