Android Open Source Project中文网站
源码来源:清华大学开源软件镜像站——AOSP
不要一看到googlexxx就被唬到了,这个链接完全可以访问。
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
这一步可选,为了防止后面运行repo init
出现无法连接到gerrit.googlesource.com错误,切换到清华的git源
echo "export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'" >> ~/.bashrc
. ~/.bashrc
mkdir SOURCE_DIR
cd SOURCE_DIR
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
初始化主代码仓库:
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
初始化某个分支版本代码仓库(代号、标记和细分版本号列表):
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-7.1.1_r4
这里并发数不能太大,按清华开源镜像站建议,设为4。
repo sync -j4
这一步会耗时较久,如果中途失败了,可以重新运行repo sync
继续。
包括android 7.x在内的,凡是使用了jack编译工具的源码版本,在编译之前需要修改一个地方:源码目录/prebuilts/sdk/tools/jack-admin文件,找到JACK_SERVER_COMMAND
声明的地方,将原来的:
#...
JACK_SERVER_COMMAND="java -XX:MaxJavaStackTraceDepth=-1 -Djava.io.tmpdir=$TMPDIR $JACK_SERVER_VM_ARGUMENTS -cp $LAUNCHER_JAR $LAUNCHER_NAME"
#...
修改为:
#...
JACK_SERVER_COMMAND="java -XX:MaxJavaStackTraceDepth=-1 -Djava.io.tmpdir=$TMPDIR $JACK_SERVER_VM_ARGUMENTS -Xmx4096m -cp $LAUNCHER_JAR $LAUNCHER_NAME"
#...
添加了一个-Xmx4096m参数,可以防止编译期间出现 Out of memory error...GC overhead limit exceeded.
的问题。
如果编译中途遇到才修改,建议重启先手动重启jack-server,再继续make。
./prebuilts/sdk/tools/jack-admin stop-server
./prebuilts/sdk/tools/jack-admin start-server
. build/envsetup.sh
lunch
make -j8
经测试,Android 8.1 源码目录的/build/envsetup.sh已经不再是一个真的shell脚本,而是变成了一个链接文件:
ls -l build/envsetup.sh
lrwxrwxrwx 1 sy sy 16 Apr 13 01:24 build/envsetup.sh -> make/envsetup.sh
如果执行. build/envsetup.sh
没有效果,则执行它链接到的真实shell脚本:
. build/make/envsetup.sh
lunch
make -j8
编译输出目录在:SOURCE_DIR/out/target/product/[PRODUCT_NAME]
,如:android-7.1.1_r14/out/target/product/bullhead
。
这一步一般几个小时,视电脑性能而定。完成时截图:
. build/envsetup.sh # android 8.1 执行 . build/make/envsetup.sh
lunch sdk-eng
make sdk
编译输出目录在:SOURCE_DIR/out/host/linux-x86/sdk
完整操作:
. build/envsetup.sh # android 8.1 执行 . build/make/envsetup.sh
lunch
emulator
如果是在同一个终端里编译完sdk后立即运行模拟器,由于前面已经执行过.及lunch命令了,就只需要执行:
emulator
手机型号:Nexus 5X
配置电脑对手机的usb访问权限,可以参考我的另一篇博客:Linux配置Android设备USB访问权限。
如何进入fastboot模式并解锁设备,详细步骤可以参考官方指导:Running Builds。
主要用到的命令是:
$ adb reboot bootloader #重启手机进入fastboot
$ fastboot oem unlock #重新加锁为:fastboot oem lock
$ fastboot flashall -w #将编译的镜像写入手机flash
如果是在新的终端里面cd到编译目录,直接运行fastboot flashall -w
,会遇到error: could not load boot.img
的问题:
$ cd android-7.1.1_r14/out/target/product/bullhead
$ fastboot flashall -w
error: could not load boot.img
经过我的摸索,得到的解决方法是:先运行一次lunch
命令,选择对应的编译版本和类型,再烧写镜像就好了。
$ cd android-7.1.1_r14
$ lunch # 选择设备对应的类型,我这里选择:21. aosp_bullhead-userdebug
$ cd out/target/product/bullhead
$ fastboot flashall -w
target reported max download size of 536870912 bytes
wiping userdata...
Creating filesystem with parameters:
Size: 11773390848
Block size: 4096
Blocks per group: 32768
Inodes per group: 8176
Inode size: 256
Journal blocks: 32768
Label:
Blocks: 2874363
Block groups: 88
Reserved block group size: 703
Created filesystem with 11/719488 inodes and 84965/2874363 blocks
wiping cache...
Creating filesystem with parameters:
Size: 100663296
Block size: 4096
Blocks per group: 32768
Inodes per group: 6144
Inode size: 256
Journal blocks: 1024
Label:
Blocks: 24576
Block groups: 1
Reserved block group size: 7
Created filesystem with 11/6144 inodes and 1422/24576 blocks
--------------------------------------------
Bootloader Version...: BHZ21c
Baseband Version.....: M8994F-2.6.39.3.03
Serial Number........: 01bf27deae6532f8
--------------------------------------------
checking product...
OKAY [ 0.020s]
sending 'boot' (12033 KB)...
OKAY [ 0.370s]
writing 'boot'...
OKAY [ 0.209s]
sending 'recovery' (16165 KB)...
OKAY [ 0.455s]
writing 'recovery'...
OKAY [ 0.284s]
erasing 'system'...
OKAY [ 0.431s]
sending sparse 'system' 1/2 (503150 KB)...
OKAY [ 12.371s]
writing 'system' 1/2...
OKAY [ 9.437s]
sending sparse 'system' 2/2 (484987 KB)...
OKAY [ 12.276s]
writing 'system' 2/2...
OKAY [ 8.659s]
erasing 'vendor'...
OKAY [ 0.099s]
sending 'vendor' (199062 KB)...
OKAY [ 4.913s]
writing 'vendor'...
OKAY [ 3.757s]
erasing 'userdata'...
OKAY [ 1.206s]
sending 'userdata' (136726 KB)...
OKAY [ 3.362s]
writing 'userdata'...
OKAY [ 2.378s]
erasing 'cache'...
OKAY [ 0.071s]
sending 'cache' (5688 KB)...
OKAY [ 0.232s]
writing 'cache'...
OKAY [ 0.116s]
rebooting...
finished. total time: 60.783s