1. 概述
RK3588 Android12开发涉及大量编译、调试、测试工作,掌握效率提升技巧能显著缩短开发周期。本文介绍编译加速、代码编辑、调试工具、自动化脚本等实用技巧。
1.1 效率提升方向
- 编译加速(ccache、增量编译)
- 代码编辑(VSCode配置、代码跳转)
- 快速调试(日志分析、远程调试)
- 自动化脚本(一键编译、批量测试)
- 工具集成(Git、Repo、Gerrit)
2. 编译加速
2.1 ccache配置
ccache可以缓存编译结果,大幅加速重复编译。
安装ccache
# Ubuntu安装
sudo apt install ccache
# 验证安装
ccache --version
配置ccache
# 设置缓存大小(建议50G以上)
ccache -M 100G
# 查看配置
ccache -p
# 查看统计
ccache -s
# 清空缓存
ccache -C
集成到编译系统
# 方法1:环境变量
export USE_CCACHE=1
export CCACHE_DIR=/home/$USER/.ccache
export CCACHE_MAXSIZE=100G
# 添加到 ~/.bashrc
echo 'export USE_CCACHE=1' >> ~/.bashrc
echo 'export CCACHE_DIR=/home/$USER/.ccache' >> ~/.bashrc
echo 'export CCACHE_MAXSIZE=100G' >> ~/.bashrc
source ~/.bashrc
# 方法2:prebuilts配置
# Android编译系统会自动使用ccache
验证ccache生效
# 首次编译
./build.sh -UKAu
# 查看ccache统计
ccache -s
# 应该看到 cache hit 增加
# 第二次编译(应该快很多)
./build.sh -UKAu
2.2 增量编译
# 只编译修改的模块
# 1. 修改kernel代码后
./build.sh -K
# 2. 修改Android代码后
./build.sh -A
# 3. 修改U-Boot后
./build.sh -U
# 4. 只打包固件
./build.sh -u
# 5. 编译单个模块
cd android
source build/envsetup.sh
lunch rk3588_s-userdebug
mmm packages/apps/Settings
2.3 并行编译优化
# 根据CPU核心数设置并行任务数
# 建议:核心数 * 1.5
# 查看CPU核心数
nproc
# 设置编译线程数(16核CPU)
export MAKE_JOBS=24
# 或在build.sh中指定
./build.sh -j24 -UKAu
# Android编译
make -j24
2.4 编译输出优化
# 减少编译日志输出
./build.sh -UKAu 2>&1 | tee build.log
# 只显示错误和警告
./build.sh -UKAu 2>&1 | grep -E "error|warning"
# 编译时间统计
time ./build.sh -UKAu
3. VSCode开发环境配置
3.1 安装VSCode
# Ubuntu安装
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -o root -g root -m 644 packages.microsoft.gpg /etc/apt/trusted.gpg.d/
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
sudo apt update
sudo apt install code
# 启动VSCode
code
3.2 必装插件
// 推荐插件列表
{
"recommendations": [
"ms-vscode.cpptools", // C/C++支持
"ms-vscode.cpptools-extension-pack",
"ms-python.python", // Python支持
"vscjava.vscode-java-pack", // Java支持
"redhat.java",
"vscjava.vscode-maven",
"ms-vscode.makefile-tools", // Makefile支持
"twxs.cmake", // CMake支持
"ms-azuretools.vscode-docker", // Docker支持
"eamodio.gitlens", // Git增强
"mhutchie.git-graph", // Git图形化
"donjayamanne.githistory", // Git历史
"streetsidesoftware.code-spell-checker", // 拼写检查
"shd101wyy.markdown-preview-enhanced", // Markdown预览
"yzhang.markdown-all-in-one", // Markdown工具
"ms-vscode-remote.remote-ssh", // SSH远程开发
"ms-vscode.hexeditor", // 十六进制编辑器
"zxh404.vscode-proto3" // Protobuf支持
]
}
3.3 C/C++配置
// .vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "RK3588",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/kernel-5.10/include",
"${workspaceFolder}/kernel-5.10/arch/arm64/include",
"${workspaceFolder}/android/system/core/include",
"${workspaceFolder}/android/frameworks/native/include",
"${workspaceFolder}/android/hardware/libhardware/include"
],
"defines": [
"__KERNEL__",
"CONFIG_ARM64",
"ANDROID"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-arm64"
}
],
"version": 4
}
3.4 代码跳转配置
# 生成ctags索引
sudo apt install universal-ctags
cd ~/rk3588_sdk
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
# 或使用cscope
sudo apt install cscope
cd kernel-5.10
make cscope
3.5 任务配置
// .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Kernel",
"type": "shell",
"command": "./build.sh -K",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "Build Android",
"type": "shell",
"command": "./build.sh -A",
"group": "build",
"problemMatcher": []
},
{
"label": "Build All",
"type": "shell",
"command": "./build.sh -UKAu",
"group": "build",
"problemMatcher": []
}
]
}
4. 快捷编译脚本
4.1 模块编译脚本
#!/bin/bash
# quick_build.sh
function show_usage() {
echo "Usage: $0 [option]"
echo "Options:"
echo " -k Build kernel only"
echo " -a Build Android only"
echo " -u Build U-Boot only"
echo " -m Build specific module"
echo " -f Build and flash"
echo " -h Show this help"
}
function build_kernel() {
echo "=== Building Kernel ==="
cd ~/rk3588_sdk
./build.sh -K
}
function build_android() {
echo "=== Building Android ==="
cd ~/rk3588_sdk
./build.sh -A
}
function build_module() {
echo "=== Building Module: $1 ==="
cd ~/rk3588_sdk/android
source build/envsetup.sh
lunch rk3588_s-userdebug
mmm $1
}
function build_and_flash() {
echo "=== Building and Flashing ==="
cd ~/rk3588_sdk
./build.sh -UKAu
# 等待设备进入loader模式
echo "Please put device into loader mode..."
read -p "Press Enter when ready..."
# 烧录
sudo upgrade_tool uf rockdev/update.img
}
case "$1" in
-k) build_kernel ;;
-a) build_android ;;
-u) ./build.sh -U ;;
-m) build_module $2 ;;
-f) build_and_flash ;;
-h) show_usage ;;
*) show_usage ;;
esac
4.2 增量编译脚本
#!/bin/bash
# incremental_build.sh
# 检测修改的文件
MODIFIED_FILES=$(git status --short | awk '{print $2}')
if [ -z "$MODIFIED_FILES" ]; then
echo "No modified files"
exit 0
fi
echo "Modified files:"
echo "$MODIFIED_FILES"
# 判断修改类型
KERNEL_MODIFIED=0
ANDROID_MODIFIED=0
UBOOT_MODIFIED=0
for file in $MODIFIED_FILES; do
if [[ $file == kernel* ]]; then
KERNEL_MODIFIED=1
elif [[ $file == android/* ]]; then
ANDROID_MODIFIED=1
elif [[ $file == u-boot/* ]]; then
UBOOT_MODIFIED=1
fi
done
# 增量编译
if [ $KERNEL_MODIFIED -eq 1 ]; then
echo "=== Building Kernel ==="
./build.sh -K
fi
if [ $ANDROID_MODIFIED -eq 1 ]; then
echo "=== Building Android ==="
./build.sh -A
fi
if [ $UBOOT_MODIFIED -eq 1 ]; then
echo "=== Building U-Boot ==="
./build.sh -U
fi
# 打包
echo "=== Packing Image ==="
./build.sh -u
4.3 自动测试脚本
#!/bin/bash
# auto_test.sh
DEVICE_IP="192.168.1.100"
function wait_device() {
echo "Waiting for device..."
adb wait-for-device
sleep 5
}
function test_gpio() {
echo "=== Testing GPIO ==="
adb shell "echo 1 > /sys/class/gpio/gpio100/value"
sleep 1
adb shell "cat /sys/class/gpio/gpio100/value"
}
function test_uart() {
echo "=== Testing UART ==="
adb shell "echo 'test' > /dev/ttyS4"
}
function test_i2c() {
echo "=== Testing I2C ==="
adb shell "i2cdetect -y 0"
}
function collect_logs() {
echo "=== Collecting Logs ==="
mkdir -p logs/$(date +%Y%m%d_%H%M%S)
adb logcat -d > logs/$(date +%Y%m%d_%H%M%S)/logcat.txt
adb shell dmesg > logs/$(date +%Y%m%d_%H%M%S)/dmesg.txt
}
# 执行测试
wait_device
test_gpio
test_uart
test_i2c
collect_logs
echo "=== Test Complete ==="
5. 日志分析工具
5.1 logcat过滤脚本
#!/bin/bash
# logcat_filter.sh
# 彩色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
adb logcat | while read line; do
if echo "$line" | grep -q "E/"; then
echo -e "${RED}$line${NC}"
elif echo "$line" | grep -q "W/"; then
echo -e "${YELLOW}$line${NC}"
elif echo "$line" | grep -q "I/"; then
echo -e "${GREEN}$line${NC}"
else
echo "$line"
fi
done
5.2 dmesg分析脚本
#!/bin/bash
# dmesg_analyze.sh
echo "=== Kernel Errors ==="
adb shell dmesg | grep -i "error"
echo ""
echo "=== Kernel Warnings ==="
adb shell dmesg | grep -i "warning"
echo ""
echo "=== Kernel Panics ==="
adb shell dmesg | grep -i "panic"
echo ""
echo "=== Driver Probe ==="
adb shell dmesg | grep "probe"
echo ""
echo "=== I2C Errors ==="
adb shell dmesg | grep "i2c"
5.3 性能分析
#!/bin/bash
# performance_check.sh
echo "=== CPU Usage ==="
adb shell top -n 1 | head -20
echo ""
echo "=== Memory Usage ==="
adb shell cat /proc/meminfo | grep -E "MemTotal|MemFree|MemAvailable"
echo ""
echo "=== Disk Usage ==="
adb shell df -h
echo ""
echo "=== Process List ==="
adb shell ps -A | grep -v "\[" | head -20
6. Git工作流优化
6.1 Git别名配置
# ~/.gitconfig
[alias]
st = status
co = checkout
br = branch
ci = commit
df = diff
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
last = log -1 HEAD
unstage = reset HEAD --
amend = commit --amend
undo = reset --soft HEAD^
6.2 Repo快捷命令
# ~/.bashrc
alias repo-sync='repo sync -c -j8 --no-tags'
alias repo-status='repo status'
alias repo-diff='repo diff'
alias repo-forall='repo forall -c'
6.3 代码提交模板
# ~/.gitmessage
# <type>: <subject>
#
# <body>
#
# <footer>
#
# Type:
# feat: 新功能
# fix: 修复bug
# docs: 文档修改
# style: 代码格式
# refactor: 重构
# test: 测试
# chore: 构建/工具
#
# Example:
# feat: 添加GPIO驱动
#
# - 实现GPIO控制接口
# - 添加设备树配置
#
# Issue: #123
# 配置模板
git config --global commit.template ~/.gitmessage
7. 远程开发配置
7.1 SSH免密登录
# 生成密钥
ssh-keygen -t rsa -b 4096
# 复制公钥到服务器
ssh-copy-id user@192.168.1.100
# 配置SSH
# ~/.ssh/config
Host rk3588
HostName 192.168.1.100
User root
Port 22
IdentityFile ~/.ssh/id_rsa
# 使用别名连接
ssh rk3588
7.2 VSCode远程开发
// 安装Remote-SSH插件后
// .vscode/settings.json
{
"remote.SSH.remotePlatform": {
"rk3588": "linux"
},
"remote.SSH.configFile": "~/.ssh/config"
}
7.3 文件同步
# 使用rsync同步
rsync -avz --progress ~/rk3588_sdk/ user@192.168.1.100:/home/user/rk3588_sdk/
# 排除不需要的文件
rsync -avz --progress \
--exclude='.git' \
--exclude='out' \
--exclude='*.o' \
~/rk3588_sdk/ user@192.168.1.100:/home/user/rk3588_sdk/
# 自动同步脚本
#!/bin/bash
# auto_sync.sh
inotifywait -m -r -e modify,create,delete ~/rk3588_sdk/ | while read path action file; do
rsync -avz ~/rk3588_sdk/ user@192.168.1.100:/home/user/rk3588_sdk/
done
8. 调试工具集成
8.1 GDB调试配置
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "GDB Remote Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/android/out/target/product/rk3588_s/system/bin/app_process64",
"miDebuggerServerAddress": "192.168.1.100:5039",
"miDebuggerPath": "/usr/bin/gdb-multiarch",
"setupCommands": [
{
"text": "set sysroot ${workspaceFolder}/android/out/target/product/rk3588_s/system"
}
]
}
]
}
8.2 串口调试工具
# 安装minicom
sudo apt install minicom
# 配置串口
sudo minicom -s
# 设置:115200 8N1, 无流控
# 或使用screen
sudo screen /dev/ttyUSB0 115200
# Python串口工具
pip3 install pyserial
python3 -m serial.tools.miniterm /dev/ttyUSB0 115200
9. 常见问题
9.1 编译速度慢
解决方法:
# 1. 启用ccache
export USE_CCACHE=1
ccache -M 100G
# 2. 增加并行任务
export MAKE_JOBS=24
# 3. 使用SSD存储源码
# 4. 增加内存(建议32GB+)
# 5. 关闭不必要的服务
sudo systemctl stop docker
sudo systemctl stop mysql
9.2 VSCode卡顿
解决方法:
// settings.json
{
"files.watcherExclude": {
"**/out/**": true,
"**/.git/**": true,
"**/node_modules/**": true
},
"search.exclude": {
"**/out": true,
"**/build": true
},
"C_Cpp.intelliSenseEngine": "Tag Parser"
}
9.3 磁盘空间不足
解决方法:
# 清理编译输出
cd ~/rk3588_sdk
rm -rf android/out
rm -rf kernel-5.10/out
# 清理ccache
ccache -C
# 清理Git仓库
repo forall -c 'git clean -fdx'
repo forall -c 'git gc --aggressive'
10. 总结
提升开发效率的关键:
- 编译加速:ccache + 并行编译 + 增量编译
- 工具配置:VSCode + 插件 + 代码跳转
- 自动化脚本:快捷编译 + 自动测试 + 日志分析
- 远程开发:SSH + 文件同步 + 远程调试
- Git工作流:别名 + 模板 + Repo优化
合理使用这些技巧,可以将开发效率提升50%以上。