安卓源码环境搭建,启动模拟器及单步调试方法

清华大学开源软件镜像站

介绍如何下载代码,可参考 https://mirror.tuna.tsinghua.edu.cn/help/AOSP/
注意此文章里的source.android.com/可能无法访问,可以改成:source.android.google.cn/

python环境准备:

默认已安装python3;
使python命令指向python3:sudo apt install python-is-python3

repo下载:

https://mirrors.tuna.tsinghua.edu.cn/help/git-repo/
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -o ~/bin/repo
chmod +x ~/bin/repo
repo的运行过程中会尝试访问AOSP官方的git源更新自己(可能有网络问题),想使用tuna的镜像源进行更新,可以将如下内容复制到你的~/.bashrc里
export REPO_URL=‘https://mirrors.tuna.tsinghua.edu.cn/git/git-repo’

电脑环境

ubuntu16.04 i5 64位 + 12G内存 + 300G可用硬盘空间
ubuntu18.04 i7 64位 + 32G内存

安装所需的软件包

电脑环境软件下载,参考https://source.android.com/setup/build/initializing
https://source.android.google.cn/setup/build/initializing
Ubuntu 18.04:
sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g+±multilib libc6-dev-i386 libncurses5-dev lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig
Ubuntu 14.04:
sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g+±multilib libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev libgl1-mesa-dev libxml2-utils xsltproc unzip
Ubuntu 12.04:
sudo apt-get install git gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 libgl1-mesa-dev g+±multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so

代码下载

-b分支查询:https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds

repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-4.0.1_r1
安卓11代码下载:
mkdir android-11.0.0_r40
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-11.0.0_r40
repo sync -c --no-tags -j4

安卓11版本代码编译

source ./build/envsetup.sh
lunch aosp_car_x86_64-userdebug
make -j2

启动模拟器

源码整编之后的代码环境android根目录,执行以下命令,即可启动模拟器
source ./build/envsetup.sh
lunch aosp_car_x86_64-userdebug
emulator -nocache

问题

无法跟硬件交互,如麦克风,GPS,音响
emulator -nocache -writable-system,加writable-system后,无法启动,无法remount,想要修改代码,只能改源码重新编译

源码导入AndroidStudio

参考https://blog.csdn.net/turtlejj/article/details/83857897
source ./build/envsetup.sh
lunch aosp_car_x86_64-userdebug
make idegen -j4
development/tools/idegen/idegen.sh
idegen.sh报错(find: ‘out/target/product/generic_x86_64/root/d’: Permission denied)不影响;

编译完成后,会生成这两个文件android.iml、android.ipr
执行以下命令:
sudo chmod 777 android.iml
sudo chmod 777 android.ipr

使用gedit编辑android.iml文件,将不需要查看的文件夹添加到excludeFolder标签中并保存;
注意out目录的部分代码编译产物如aidl生成的java文件,最不好不要exclude。

<excludeFolder url="file://$MODULE_DIR$/.repo" />
<excludeFolder url="file://$MODULE_DIR$/art" />
<excludeFolder url="file://$MODULE_DIR$/bionic" />
<excludeFolder url="file://$MODULE_DIR$/bootable" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/compatibility" />
<excludeFolder url="file://$MODULE_DIR$/dalvik" />
<excludeFolder url="file://$MODULE_DIR$/developers" />
<excludeFolder url="file://$MODULE_DIR$/developers/samples" />
<excludeFolder url="file://$MODULE_DIR$/development" />
<excludeFolder url="file://$MODULE_DIR$/device/google" />
<excludeFolder url="file://$MODULE_DIR$/device/sample" />
<excludeFolder url="file://$MODULE_DIR$/docs" />
<excludeFolder url="file://$MODULE_DIR$/external" />
<excludeFolder url="file://$MODULE_DIR$/flashing-files" />
<excludeFolder url="file://$MODULE_DIR$/frameworks/base/docs" />
<excludeFolder url="file://$MODULE_DIR$/kernel" />
<excludeFolder url="file://$MODULE_DIR$/libcore" />
<excludeFolder url="file://$MODULE_DIR$/libnativehelper" />
<excludeFolder url="file://$MODULE_DIR$/out" />
<excludeFolder url="file://$MODULE_DIR$/pdk" />
<excludeFolder url="file://$MODULE_DIR$/platform_testing" />
<excludeFolder url="file://$MODULE_DIR$/prebuilt" />
<excludeFolder url="file://$MODULE_DIR$/prebuilts" />
<excludeFolder url="file://$MODULE_DIR$/shortcut-fe" />
<excludeFolder url="file://$MODULE_DIR$/test" />
<excludeFolder url="file://$MODULE_DIR$/toolchain" />
<excludeFolder url="file://$MODULE_DIR$/tools" />

使用Android Studio选中"android.ipr"打开项目即可,初次打开源码项目,加载时间就比较久,预计30-60分钟。

代码跳转问题解决

至此,源码已加载完成,但是打开某个类,点击跳转时,跳转到的是sdk编译好的class文件中,不是源码java文件,解决方法如下:
1,AndroidStudio加载代码完成后,打开SDK Manager,选择SDK Platforms中的Android11.0®版本,进行下载,sdk下载完成后,关闭SDK Manager
2,打开androidStudio界面上的ProjectStructure,选中Project项,选择Android API 30 Platform
3,选择ProjectStructure左侧Modules选项,选中中间的android列表(注意非Android),
然后切换到Dependencies卡片,删除除了和"Android API 30 Platform"的其他jar依赖,点击确定.

debug调试

1,例如想要调试sendBroadcase相关功能,可以在ActivityManagerService.java- public final int broadcastIntentWithFeature方法中添加断点
2,选择Run->Attach Debuger to Android Process->show all processes->system_process,点击ok
3,尝试写一个apk,在界面上点击按钮发送一个广播功能,当点击按钮时,断点调试触发,即可在androidStudio界面上进行单步调试。

你可能感兴趣的:(android,android)