0. 下载 Android 源码
我用的是在 AOSP 基础上修改过的代码,不过套路都是一样的。
1. Android 源码全编译
$ cd android/source/code/folder
$ source build/envsetup.sh
$ lunch your_favorite_target
$ make
该步骤耗时较长,建议夜间编译,早上起来看结果。
2. 生成导入用的文件
Android 源码很庞大,我们可以修改 development/tools/idegen/excluded-paths
这个文件,以过滤掉不关心的内容。
$ mmm development/tools/idegen/
$ ./development/tools/idegen/idegen.sh
该步骤会生成 android.ipr 和 android.iml,同时可以查看使用说明:
$ cat development/tools/idegen/README
此时也可以通过编辑 android.iml 中的 exclude 条目来调整导入内容。比如:
<excludeFolder url="file://$MODULE_DIR$/.repo" />
<excludeFolder url="file://$MODULE_DIR$/AMSS" />
<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$/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$/frameworks/base/docs" />
<excludeFolder url="file://$MODULE_DIR$/gen" />
<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$/packages/apps" />
<excludeFolder url="file://$MODULE_DIR$/packages/experimental" />
<excludeFolder url="file://$MODULE_DIR$/packages/inputmethods" />
<excludeFolder url="file://$MODULE_DIR$/packages/screensavers" />
<excludeFolder url="file://$MODULE_DIR$/packages/screensavers" />
<excludeFolder url="file://$MODULE_DIR$/packages/services/BuiltInPrintService" />
<excludeFolder url="file://$MODULE_DIR$/packages/services/Mms" />
<excludeFolder url="file://$MODULE_DIR$/packages/services/Telecomm" />
<excludeFolder url="file://$MODULE_DIR$/packages/services/Telephony" />
<excludeFolder url="file://$MODULE_DIR$/packages/wallpapers/Mms" />
<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$/sdk" />
<excludeFolder url="file://$MODULE_DIR$/shortcut-fe" />
<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$/vendor" />
3. 调整 AndroidStudio 参数
// 修改 your/android/studio/bin/studio.vmoptions
// 和 your/android/studio/bin/studio64.vmoptions
// 我的机器 32G 内存,下面是我的数值配置:
-Xms2048m
-Xmx4096m
-XX:ReservedCodeCacheSize=1024m
-XX:SoftRefLRUPolicyMSPerMB=256
// 修改 your/android/studio/bin/idea.properties
idea.max.intellisense.filesize=5000
我的数值不一定是合适的,请根据自己机器的实际情况来配置适当的数值。
4. 导入 AndroidStudio
启动 Android Studio,然后点 Open an existing Android Studio project,在打开的窗口里选择之前生成的 android.ipr。
文件索引生成后,可以通过 Android Studio 界面来调整导入内容。点 File -> Project Structure,在窗口左侧选 Module, 在窗口右侧选 Sources,在文件夹上右键就可以 Excluded 了。
为了更好的使用体验,点击 File -> Project Structure -> SDKs 来配置 JDK,把所有 jar 包清掉就好。接着点 Project Settings -> Modules,找到 out/target/common/R
,点右键,选 Sources
,最后 Apply
。