一、Android源码下载和编译
下载和编译源码参见下面
macOS High Sierra编译android9.0源码-https://blog.csdn.net/wangkaishou/article/details/89855766
二、生成可被AndroidStudio导入的配置文件
2.1 单独编译idegen模块生成idegen.jar
在源码的根目录下,执行下面命令编译出idegen.jar文件
source build/envsetup.sh
mmm development/tools/idegen/
命令执行完成后会输出idegen.jar的路径:
out/host/darwin-x86/framework/idegen.jar
...
============================================
ninja: no work to do.
[1/1] out/soong/.bootstrap/bin/soong_build out/soong/build.ninja
out/build-aosp_x86_64-_development_tools_idegen_Android.mk-cleanspec.ninja is missing, regenerating...
out/build-aosp_x86_64-_development_tools_idegen_Android.mk.ninja is missing, regenerating...
[ 99% 228/229] glob toolchain/pgo-profiles/hwui/hwui.profdata.2018-03-20
[ 20% 1/5] //development/tools/idegen:idegen javac [darwin common]
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
[100% 5/5] Install: out/host/darwin-x86/framework/idegen.jar
2.2 生成AndroidStudio工程配置文件-android.ipr和android.iml
成功编译完成idegen模块生成idegen.jar文件后,在根目录继续执行下面命令,就可以生成了AndroidStuido源码工程的配置文件了
development/tools/idegen/idegen.sh
大约一两分钟后命令执行完成后就会生成android.ipr和android.iml 这两个文件位于在源码的根目录下
.
├── Android.bp -> build/soong/root.bp
├── Makefile
├── android.iml
├── android.ipr
├── art
├── bionic
├── bootable
├── bootstrap.bash -> build/soong/bootstrap.bash
├── build
├── compatibility
├── cts
├── dalvik
├── developers
├── development
├── device
├── external
├── frameworks
├── hardware
├── kernel
├── libcore
├── libnativehelper
├── out
├── packages
├── pdk
├── platform_testing
├── prebuilts
├── sdk
├── sync.sh
├── system
├── test
├── toolchain
└── tools
三、导入源码
打开Android Studio,通过File->open找到源码目录下的android.ipr文件就可以直接导入工程了.
第一次导入将会非常漫长.
3.1 加快导入
默认会把所有android源码导入到AndroidStudio中,如果只关注Framework层源码,可以在android.iml文件中添加下面的文件,排除掉不需要导入的目录
<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$/cts" />
<excludeFolder url="file://$MODULE_DIR$/dalvik" />
<excludeFolder url="file://$MODULE_DIR$/developers" />
<excludeFolder url="file://$MODULE_DIR$/development" />
<excludeFolder url="file://$MODULE_DIR$/device" />
<excludeFolder url="file://$MODULE_DIR$/docs" />
<excludeFolder url="file://$MODULE_DIR$/external" />
<excludeFolder url="file://$MODULE_DIR$/hardware" />
<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$/prebuilts" />
<excludeFolder url="file://$MODULE_DIR$/sdk" />
<excludeFolder url="file://$MODULE_DIR$/system" />
<excludeFolder url="file://$MODULE_DIR$/test" />
<excludeFolder url="file://$MODULE_DIR$/toolchain" />
<excludeFolder url="file://$MODULE_DIR$/tools" />
<excludeFolder url="file://$MODULE_DIR$/.repo" />
四、AndroidStudio中配置源码
通过AndroidStudio导入源码后, 需要为项目配置SDK和JDK,否则阅读源码时代码会跳转到jar包中的类
4.1 新建和配置SDK
-
右键打开项目配置窗口-Project Structure,选中SDKs中新建SDK,命名为jdk(framework),什么名字都可以,然后将其Classpath和Sourcepath删除掉
-
右键打开项目配置窗口-
Project Structure
,选中SDKs
,选择Android API 28 Platform
将Java SDK切换为上一步刚创建的jdk(framework)
4.2 为工程配置SDK
右键打开项目配置窗口-Project Structure
,选中Project
,将Project SDK
切换为选择的 Android API 28 Platform
4.3 为工程配置依赖
右键打开项目配置窗口-Project Structure
,选择Modules
将Dependencies中除了Android API 28 Platform以外的全部删除掉,然后将frameworks和external
添加进来最终如下图所示.
五、调试源码
5.1 打开通过源码编译的模拟器
source build/envsetup.sh
lunch
emulator
5.2 attach进程
attach到模拟器中进程上,打上断点就可以调试了.