目录
源码下载
1 镜像地址
清华大学AOSP(Android Open Source Project)
https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/12
2 过程摘录
下载Repo 工具
mkdir ~/bin
PATH=~/bin:$PATH
curl https://aosp.tuna.tsinghua.edu.cn/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo1234
替换已有的 AOSP 源代码的 remote
如果你之前已经通过某种途径获得了 AOSP 的源码(或者你只是 init 这一步完成后), 你希望以后通过 TUNA 同步 AOSP 部分的代码,只需要将.repo/manifest.xml
把其中的 aosp 这个 remote 的 fetch 从 https://android.googlesource.com
改为 https://aosp.tuna.tsinghua.edu.cn/
。
<manifest>
<remote name="aosp"
- fetch="https://android.googlesource.com"
+ fetch="https://aosp.tuna.tsinghua.edu.cn"
review="android-review.googlesource.com" />
<remote name="github"
同时,修改 .repo/manifests.git/config
,将
url = https://android.googlesource.com/platform/manifest
更改为
url = https://aosp.tuna.tsinghua.edu.cn/platform/manifest
然后在 .repo/manifests
目录下,执行
git add .
git commit -m "change repo mirror"
使用每月更新的初始化包
由于首次同步需要下载 24GB 数据,过程中任何网络故障都可能造成同步失败,我们强烈建议您使用初始化包进行初始化。下载 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar,下载完成后记得根据 checksum.txt 的内容校验一下。由于所有代码都是从隐藏的 .repo
目录中 checkout 出来的,所以我们只保留了 .repo
目录,下载后解压 再 repo sync
一遍即可得到完整的目录。
使用方法如下:
wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包 # 实践证明这个也需要改源代码的remote
tar xf aosp-latest.tar
cd AOSP # 解压得到的 AOSP 工程目录
# 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录
repo sync # 正常同步一遍即可得到完整目录 #需跨越长城
# 或 repo sync -l 仅checkout代码
此后,每次只需运行 repo sync
即可保持同步。 我们强烈建议您保持每天同步,并尽量选择凌晨等低峰时间
3 解决repo sync下载代码时提示error: Exited sync due to fetch errors
再次执行 repo sync
就可以了
4 其他
- 初始化包大小21.4G,我的网速5-6M/s,下载时间基本一个小时左右。
- Android7.0,aosp目录编译完之后的大小是54.6G,安装Ubuntu时要注意留够足够的空间,推荐100G+以上。
源码编译
1 搭建编译环境
- openJdk is needed !我自己平时开发用的是JDK1.7,这里就要安装一下openjdk,并且更改环境变量。
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-7-jdk
但是如果你是刚安装完Ubuntu,那么就可以跳过此步骤,Ubuntu自带openjdk
- 写入JAVA_HOME(openjdk的安装路径)给系统配置文件 .bashrc;
echo export JAVA_HOME="/usr/lib/jvm/java-7-openjdk-amd64/" >> ~/.bashrc
source ~/.bashrc
重新输入echo $JAVA_HOME
命令,就可以看到刚刚设置好的配置。
- required package
sudo apt-get install bison g++-multilib git gperf libxml2-utils make python-networkx zlib1g-dev:i386 zip1
- 因为源码包含C 和 C++代码,gcc为必须
sudo apt-get install gcc1
- Setting up ccache , 使用ccache
ccache是一个编译器ccache用于C和C++。可以让构建更加快速。在源代码的根目录处,做如下操作:
$export USE_CCACHE=1
$export CCACHE_DIR=/<path_of_your_choice>/.ccache
$prebuilts/misc/linux-x86/ccache/ccache -M 50G</path_of_your_choice>123
建议的cache大小为50-100G
可以使用如下操作来查看使用的ccache大小
$watch -n1 -d prebuilts/misc/linux-x86/ccache/ccache -s1
若使用Ice Cream Sandwich(4.0.x)或更老的版本,需要用prebuilts来代替prebuilts/misc
2 开始编译 Building the System
- 1.Set up environment
$ source build/envsetup.sh
or
$ . build/envsetup.sh1
- 2.Choose a Target
$ lunch
You're building on Linux
Lunch menu... pick a combo:
1. aosp_arm-eng
2. aosp_arm64-eng
3. aosp_mips-eng
4. aosp_mips64-eng
5. aosp_x86-eng
6. aosp_x86_64-eng
7. aosp_manta-userdebug
8. aosp_flo-userdebug
9. aosp_deb-userdebug
10. full_fugu-userdebug
11. aosp_fugu-userdebug
12. aosp_tilapia-userdebug
13. aosp_grouper-userdebug
14. aosp_mako-userdebug
15. aosp_hammerhead-userdebug
16. aosp_flounder-userdebug
17. aosp_shamu-userdebug
18. mini_emulator_x86-userdebug
19. mini_emulator_arm64-userdebug
20. mini_emulator_x86_64-userdebug
21. mini_emulator_mips-userdebug
22. m_e_arm-userdebug
Which would you like? [aosp_arm-eng]
- 3.build
使用make构建,GNU make可以通过使用-jN参数来处理并行任务。此处的N,根据所用的计算机的硬件的核数来指定,一般N指定为CPU核心的1到2倍。例如我的CPU是i5-6500(CPU4个核,每个核2个线程),那么最优的构建是使用make -j8。
make -j8
3 build successfully
#### make completed successfully (01:04:27 (hh:mm:ss)) ####1
- 目录结构如下:
7 9月 24 00:36 android-info.txt
62 9月 24 00:22 build_fingerprint.txt
4.0K 9月 24 10:34 cache
66M 9月 24 12:26 cache.img
70K 9月 23 23:51 clean_steps.mk
38 9月 24 00:22 current_build_config.mk
4.0K 9月 24 10:41 data
4.0K 9月 24 09:51 dex_bootjars
4.0K 9月 24 01:33 gen
1.4K 9月 24 12:25 hardware-qemu.ini
70K 9月 24 11:00 installed-files.txt
1.1M 9月 24 00:30 module-info.json
4.0K 9月 24 11:00 obj
38 9月 23 23:51 previous_build_config.mk
1.5M 9月 24 10:40 ramdisk.img
4.0K 9月 24 10:39 recovery
4.0K 9月 24 10:39 root
4.0K 9月 24 10:39 symbols
4.0K 9月 24 10:50 system
1.8G 9月 24 11:01 system.img
550M 9月 24 10:58 userdata.img
550M 9月 24 12:41 userdata-qemu.img
~/aosp/out/target/product/generic$
4 编译中的问题解决
- 第一次编译,在编译到82%的时候失败,报错 : Increase Java heap size
including ./tools/external/fat32lib/Android.mk ...
Starting build with ninja
ninja: Entering directory `.'
[ 0% 1/21542] Ensure Jack server is installed and started
Jack server already installed in "/home/smile/.jack-server"
Server is already running
[ 0% 2/21542] Building with Jack: out/target/common/obj/JAVA_LIBRARIES/core-all_intermediates/with-local/classes.dex
FAILED: /bin/bash out/target/common/obj/JAVA_LIBRARIES/core-all_intermediates/with-local/classes.dex.rsp
Java heap space
Try increasing heap size with java option '-Xmx<size>'
Warning: This may have produced partial or corrupted output.
ninja: build stopped: subcommand failed.
build/core/ninja.mk:146: recipe for target 'ninja_wrapper' failed
make: *** [ninja_wrapper] Error 1
#### make failed to build some targets (08:25 (mm:ss)) #### 12345678910111213141516
stackoverflow上关于此问题的解决方法(详见这里),在make之前进行heap size配置
1. export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"
2. out/host/linux-x86/bin/jack-admin kill-server
3. out/host/linux-x86/bin/jack-admin start-server 123
文章评论