MacBook Pro android 7.0 编译环境搭建

1 导言

想在自己的电脑搭建一个android源码环境,没事时学习下(会有没事时候?)。这其中有挺多波折,搞了差不多一个星期时间(本人是加班狗),基本上是晚上解决错误,开始编译,然后就睡觉,接着晚上再回来看有没有错。

2 Mac book基本配置

软件版本: OS X 10.11.6
内存:8G
硬盘:256 (128G够吗,感觉不够,现在android编译完后占用86G)

3 环境配置

官方教程:https://source.android.com/source/initializing.html,基本上是抄写了一遍。

3.1 Creating a case-sensitive disk image

mac os 文件系统是对大小写不区分的,不清楚基于什么原因。而git是不支持这种不区分大小写的文件系统的,所以我们要创建一个对大小写敏感的image,用于存放android 源码。

$ hdiutil create -type SPARSE -fs ‘Case-sensitive Journaled HFS+’ -size 100g ~/android.dmg

size我给了100G,不过不用怕小了,是可以继续增加的:

# hdiutil resize -size g ~/android.dmg.sparseimage

android.dmg.sparseimage就是我们刚才创建的image。
image创建了,需要mount到文件系统中,为了方便起见,加上两个command,增加到~/.bash_profile中:
mount command

# mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; }

umount command

# unmount the android file image
function umountAndroid() { hdiutil detach /Volumes/android; }

当运行mountAndroid命令后,df结果如下:

/dev/disk2s2 100Gi 86Gi 14Gi 87% 22529002 3601418 86% /Volumes/android

路径就在/Volumes/android

3.2 安装JDK

Mac OS - jdk 8u45 or newer
mac上可以用oracle的jdk8,记得linux应该已经用openjdk了。jdk下载路径:http://www.oracle.com/technetwork/java/javase/downloads/index.html ,请选择mac版的jdk 8u45 or newer

3.3 安装xcode和Xcode command line tools

这个无比蛋疼,折腾了好几次。按照Google推荐的版本是Xcode 4.5.2 ,但安装时mac os提示不兼容;最新版本目前是8.2,但编译时会报错。

  • install Xcode command line tools

    $ xcode-select –install

  • install xcode
    经过实验,xcode 7.1 可以顺利build过。xcode下载地址:https://developer.apple.com , develop -> xcode -> download (这是最新的xcode) -> 网页最下面,see more download,所有版本都可以找到,下载xcode 7.1。按照普通app安装就好了,安装完了之后,要运行一遍,license授权。

3.4 获取 macports

https://www.macports.org/install.php
macport下载,和普通软件一样安装。安装完成后,检查~/.bash_profile PATH 环境变量中opt/local/bin路径是否出现在/usr/bin之前。如果没有,请手动加上:

export PATH=/opt/local/bin:$PATH

3.5 获取make, git, and GPG packages from MacPorts

$ POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg

很简单,直接等待安装结果就好了。

3.6 Reverting from make 3.82

这个如果编译Android 4.0.x包括之前的code,make3.82会有bug,所以要revert到3.81版本,但我编译的是7.0,所以就没有必要了。如果有需要,可以参考source.android.com如何做的。

3.7 Setting a file descriptor limit

~/.bash_profile:

# set the number of open files to be 1024
ulimit -S -n 1024

4 Android source code下载

大家都清楚,国内是访问不了google任何网站的,要么,要么用国内镜像替换。这次下载使用国内镜像网站( 清华大学开源软件镜像站)。但如果想从google直接获取,请参考google源码官网下载介绍。

4.1 Installing Repo

建立一个存放的地方,这个随便。

$ mkdir ~/bin
$ PATH=~/bin:$PATH

下载repo

$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo

如果上面也没有办法下,那就使用清华大学开源软件镜像站提供的repo(Git Repo 镜像使用帮助)

curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -o repo
chmod +x repo

修改repo更新源,下载source code之前会update repo,如果指向google,可能会update不成功。所以建议修改为:

export REPO_URL=’https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/’

4.2 开始download source code

建立工作目录:

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY

初始化仓库:

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.0.0_r29

sync Android Source Tree:

repo sync

这是一个漫长的等待,清华大学这个开源网站的速度还行。

checkout 想要的branch或者tag
下载完成之后,源码中是没有本地分支的,如果后期要修改的话,最好新建一个本地分支。

repo forall -c git checkout -b android-7.0.0_r29_test android-7.0.0_r29

建立本地分支android-7.0.0_r29_test,并且checkout tag为android-7.0.0_r29的快照到本地分支。

5 配置jack server

记得从6.0开始,就使用这个jack 来编译android code。看下官方解释: “Jack is a new Android toolchain that compiles Java source into Android dex bytecode. ” , 它就是一个编译的toolchain。

一般上不需要配置,但在mac book上编译时遇到了out of mem问题,如下:

FAILED: /bin/bash out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/with-local/classes.dex.rsp
GC overhead limit exceeded
Try increasing heap size with java option ‘-Xmx’
Warning: This may have produced partial or corrupted output.
ninja: build stopped: subcommand failed.
make: * [ninja_wrapper] Error 1

解决方法,在build/envsetup.sh 最后加上(放在这里方便我偷懒,只要我要编译android,就会执行该脚本):

export JACK_SERVER_VM_ARGUMENTS=”-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m”
./prebuilts/sdk/tools/jack-admin kill-server
./prebuilts/sdk/tools/jack-admin start-server

其实google和百度有其他解法,说修改$HOME/.jack 配置(SERVER_NB_COMPILE=1 改为1),但是这个对我来说不起作用,一样报错。由于现在jack server版本应该是1.3 ,SERVER_NB_COMPILE已经不存在了,现在配置是:

## $HOME/.jack-server/config.properties 文件中
jack.server.max-service=4 (对应就版本SERVER_NB_COMPILE)

更多详细配置说明请看:$ADROID_ROOT/prebuilts/sdk/tools/README-jack-server.md

6 编译Android source code

终于到了这一步,不过不要高兴太早,就我经验而言,大部分时间都花在这里,~~~解决编译错误~~~

cd $WORK_ANDROID_DIR
source build/envsetup.sh
lunch aosp_arm-eng (用模拟器运行,这个就行)
make -j4

编译完成的提示为:

#### make completed successfully (02:39:06 (hh:mm:ss)) ####

查看out目录,可以看到已经生成image:

$ ls -l out/target/product/generic/
-rw-r–r– 1 staff 1265409 2 18 20:49 ramdisk.img
-rw-r–r– 1 staff 1879048192 2 18 20:59 system.img
-rw-r–r– 1 staff 576716800 2 19 15:26 userdata.img

7 Run After build

真机没有,所以尝试使用emulator 运行看看。

emulator -system out/target/product/generic/system.img -ramdisk out/target/product/generic/ramdisk.img -data out/target/product/generic/userdata.img

这样运行,过了十多分钟才启动到桌面,不清楚是慢,还是image有问题,看到system process anr了,可能是image编译有问题。

编译android code,目的是为了能够学习,必须的让自己编译的系统跑起来,不然意义就不大了。如果只是为了了解,下载源码就好了,也没有必要编译。

这块后续补充,还没有完善 ……

7 注意事项

回想这个过程,编译错误主要是因为xcode 版本和 jack server 的配置,也没有遇到其他诡异的错误。

你可能感兴趣的:(android编译)