# VSCode Android 开发常用指令汇总 > 别摇小鸡App开发调试指令集合 - 适用于VSCode终端 ## 📋 快速导航 - [🔨 构建指令](#-构建指令) - [📱 安装卸载](#-安装卸载) - [🐛 调试日志](#-调试日志) - [⚡ 快速测试流程](#-快速测试流程) - [🔍 应用状态检查](#-应用状态检查) - [📊 性能监控](#-性能监控) - [🧪 单元测试](#-单元测试) - [🛠️ 开发工具](#️-开发工具) --- ## 🔨 构建指令 ### 基础构建 ```bash # 完整构建Debug版本 ./gradlew assembleDebug # 快速增量构建 (推荐日常使用) ./gradlew build # 清理后重新构建 ./gradlew clean build # 构建Release版本 ./gradlew assembleRelease ``` ### 构建信息查看 ```bash # 查看构建详情 ./gradlew assembleDebug --info # 查看构建错误堆栈 ./gradlew assembleDebug --stacktrace # 构建并运行测试 ./gradlew build connectedDebugAndroidTest ``` --- ## 📱 安装卸载 ### 安装应用 ```bash # 安装Debug APK (最常用) adb install -r "E:\chick_mood\app\build\outputs\apk\debug\app-debug.apk" # 强制安装 (覆盖现有版本) adb install -r -d "app\build\outputs\apk\debug\app-debug.apk" # 安装Release APK adb install -r "E:\chick_mood\app\build\outputs\apk\release\app-release.apk" ``` ### 卸载应用 ```bash # 完全卸载应用 adb uninstall com.piyomood # 卸载并清除数据 adb uninstall -k com.piyomood ``` ### APK文件路径 ```bash # Debug APK位置 E:\chick_mood\app\build\outputs\apk\debug\app-debug.apk # Release APK位置 E:\chick_mood\app\build\outputs\apk\release\app-release.apk ``` --- ## 🐛 调试日志 ### 日志查看 ```bash # 清除日志缓存 (测试前执行) adb logcat -c # 实时查看所有日志 adb logcat # 查看应用相关日志 adb logcat | grep -E "(PiyoMood|com.piyomood)" # 查看崩溃日志 adb logcat | grep -E "(AndroidRuntime|FATAL|ERROR)" # 查看最近的50条应用日志 adb logcat -d | grep -E "(PiyoMood|com.piyomood)" | tail -50 ``` ### 崩溃调试 ```bash # 启动应用并捕获崩溃 adb logcat -c && adb shell monkey -p com.piyomood -c android.intent.category.LAUNCHER 1 && sleep 3 && adb logcat -d | grep -E "(AndroidRuntime|FATAL)" | tail -20 # 查看详细崩溃信息 adb logcat -d | grep -A 30 -B 5 "FATAL EXCEPTION" ``` ### 传感器调试 ```bash # 查看传感器相关日志 adb logcat | grep -E "(Sensor|Accelerometer)" # 查看动画相关日志 adb logcat | grep -E "(Lottie|Animation)" ``` --- ## ⚡ 快速测试流程 ### 完整测试流程 (推荐) ```bash # 1. 构建并安装 ./gradlew assembleDebug && adb install -r "app\build\outputs\apk\debug\app-debug.apk" # 2. 启动应用测试 adb logcat -c && adb shell monkey -p com.piyomood -c android.intent.category.LAUNCHER 1 # 3. 查看启动结果 sleep 5 && adb logcat -d | grep -E "(AndroidRuntime|FATAL|PiyoMood)" | tail -20 ``` ### 快速迭代测试 ```bash # 一键测试脚本 (复制到VSCode终端使用) ./gradlew assembleDebug && adb install -r "app\build\outputs\apk\debug\app-debug.apk" && adb logcat -c && adb shell monkey -p com.piyomood -c android.intent.category.LAUNCHER 1 ``` ### 应用启动测试 ```bash # 清除日志并启动应用 adb logcat -c && adb shell monkey -p com.piyomood -c android.intent.category.LAUNCHER 1 # 检查应用是否正常启动 sleep 3 && adb logcat -d | grep -E "(ActivityTaskManager.*Displayed|Activity idle)" ``` --- ## 🔍 应用状态检查 ### Activity状态检查 ```bash # 查看当前运行的Activities adb shell dumpsys activity activities | grep "com.piyomood" # 查看顶部Activity adb shell dumpsys activity top | grep -A 10 -B 10 "PiyoMood" # 查看应用进程状态 adb shell ps | grep piyomood ``` ### 应用信息查看 ```bash # 查看应用包信息 adb shell dumpsys package com.piyomood # 查看应用权限 adb shell dumpsys package com.piyomood | grep "declared permissions" # 查看应用版本信息 adb shell dumpsys package com.piyomood | grep -A 5 -B 5 "versionName" ``` --- ## 📊 性能监控 ### 内存使用 ```bash # 查看应用内存使用情况 adb shell dumpsys meminfo com.piyomood # 实时监控内存使用 adb shell top | grep piyomood ``` ### CPU使用 ```bash # 查看应用CPU使用情况 adb shell top | grep piyomood # 查看系统整体性能 adb shell dumpsys cpuinfo | grep piyomood ``` ### 电池使用 ```bash # 查看电池使用情况 adb shell dumpsys batterystats | grep piyomood # 重置电池统计数据 adb shell dumpsys batterystats --reset ``` --- ## 🧪 单元测试 ### 运行测试 ```bash # 运行所有单元测试 ./gradlew test # 运行特定测试类 ./gradlew test --tests "com.piyomood.data.model.*" # 运行Debug测试 ./gradlew connectedDebugAndroidTest # 查看测试报告 ./gradlew test --continue ``` ### 测试报告位置 ```bash # 单元测试报告 app/build/reports/tests/testDebugUnitTest/index.html # Android测试报告 app/build/reports/androidTests/connected/index.html ``` --- ## 🛠️ 开发工具 ### 设备管理 ```bash # 查看连接的设备 adb devices # 查看设备详细信息 adb shell getprop # 查看设备屏幕密度 adb shell wm density # 截屏 adb shell screencap -p /sdcard/screenshot.png && adb pull /sdcard/screenshot.png ``` ### 文件操作 ```bash # 推送文件到设备 adb push local_file.txt /sdcard/ # 从设备拉取文件 adb pull /sdcard/device_file.txt # 查看设备文件 adb shell ls /sdcard/ # 查看应用数据目录 adb shell run-as com.piyomood ls -la /data/data/com.piyomood/ ``` ### 数据库调试 ```bash # 查看应用数据库文件 adb shell run-as com.piyomood find /data/data/com.piyomood/databases -name "*.db" # 导出数据库文件 adb shell run-as com.piyomood cat /data/data/com.piyomood/databases/your_database.db > local_database.db ``` --- ## 🚨 常见问题解决 ### 构建问题 ```bash # 清理构建缓存 ./gradlew clean # 清理Gradle缓存 ./gradlew --refresh-dependencies # 重置项目 ./gradlew clean build --refresh-dependencies ``` ### 安装问题 ```bash # 安装失败时尝试 adb uninstall com.piyomood && adb install -r "app\build\outputs\apk\debug\app-debug.apk" # 签名问题 (Release构建) ./gradlew assembleRelease ``` ### 连接问题 ```bash # 重启ADB服务 adb kill-server && adb start-server # 检查设备连接 adb devices -l # 重新连接设备 adb reconnect ``` --- ## 📝 VSCode集成提示 ### 集成终端快捷键 - `Ctrl + ` (反引号) - 打开集成终端 - `Ctrl + Shift + ` - 新建终端 - `Ctrl + C` - 终止当前命令 ### 常用组合指令 (可保存为VSCode任务) ```json // tasks.json 示例 { "version": "2.0.0", "tasks": [ { "label": "Build and Install", "type": "shell", "command": "./gradlew assembleDebug && adb install -r \"app\\build\\outputs\\apk\\debug\\app-debug.apk\"", "group": "build", "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" } }, { "label": "Start App and Check Logs", "type": "shell", "command": "adb logcat -c && adb shell monkey -p com.piyomood -c android.intent.category.LAUNCHER 1 && sleep 3 && adb logcat -d | grep -E \"(AndroidRuntime|FATAL|PiyoMood)\" | tail -20", "group": "test" } ] } ``` --- ## 🎯 推荐工作流程 ### 日常开发流程 1. **代码修改** → 2. **快速构建测试** → 3. **安装运行** → 4. **查看日志** ```bash # 推荐的一键命令 ./gradlew assembleDebug && adb install -r "app\build\outputs\apk\debug\app-debug.apk" && adb logcat -c && adb shell monkey -p com.piyomood -c android.intent.category.LAUNCHER 1 ``` ### 问题调试流程 1. **复现问题** → 2. **查看崩溃日志** → 3. **定位问题代码** → 4. **修复** → 5. **验证** ```bash # 调试专用命令 adb logcat -c && adb shell monkey -p com.piyomood -c android.intent.category.LAUNCHER 1 && sleep 5 && adb logcat -d | grep -E "(AndroidRuntime|FATAL|ERROR)" | tail -30 ``` --- *最后更新: 2025-10-22* *适用于项目: 别摇小鸡心情记录App (Chick_Mood)*