ubuntu 16.04下编译android源码
一、android源码下载
google官方一直使用的是14.04,所以对14.04的坑早已经填的平平的,但是今年年初尝试16.04遇到一些坑,甚至有些人装了16.04后根本没法工作,很多公司还是用的14.04,毕竟要搞生产嘛,随后在虚拟机和真实机上都使用16.04编译,现将笔记整理一下发出来
由于googlesource无法直接下载,即便是有梯子网速也受限下载也比较空难,就使用国内的教育机构的镜像(清华大学的开源项目tuna)(https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/)
1.安装repo:
确保主目录下有一个 bin/ 目录,并且该目录包含在路径中:
mkdir ~/bin
PATH=~/bin:$PATH
使用tuna的git-repo镜像
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -o repo
chmod +x repo
2.下载源码
mkdir android_source
cd android_source
wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包 总共有30多个G 靠网速了
tar xf aosp-latest.tar #下载完成后解压
cd aosp
repo sync -l #从.repo中checkout出代码
repo的运行过程中会尝试访问官方的git源更新自己会导致如下失败
Fetching project platform/developers/samples/android
fatal: unable to access 'https://gerrit.googlesource.com/git-repo/': Failed to connect to gerrit.googlesource.com port 443: 连接超时
fatal: unable to access 'https://gerrit.googlesource.com/git-repo/': Failed to connect to gerrit.googlesource.com port 443: 连接超时
error: Cannot fetch repo
error: Exited sync due to fetch errors
避免以上错误将如下内容复制到你的~/.bashrc里
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
重启终端
repo sync # 更新代码,建议睡觉前更新
二、编译环境搭建
1.安装使用openjdk
sudo apt-get update
sudo apt install openjdk-8-jdk
2.ubuntu下特定依赖包安装
sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386
sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib
sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev
sudo apt-get install git-core gnupg flex bison gperf build-essential
sudo apt-get install zip curl zlib1g-dev gcc-multilib g++-multilib
sudo apt-get install libc6-dev-i386
sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev
sudo apt-get install lib32z-dev ccache
sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4
三、编译
在aosp更目录下执行编译命令
. build/envsetup.sh
![](https://img.haomeiwen.com/i1315592/16c8753ef3448354.png)
执行命令lunch 会出现一些目标选项,输入要编译目标前的数字。
![](http://upload-images.jianshu.io/upload_images/1315592-ea910b60bda2b048.png)
或者直接lunch full-eng,lunch 后面的参数由目标设备和编译场景两部分,full代表为模拟器全编译,eng代表用于开发场景
![](http://upload-images.jianshu.io/upload_images/1315592-e946ef50afe90299.png)
开始编译代码:在源码根目录下键入命令make -j8 ,“j8”表示用4个线程数进行编译,线程数不是越多越好,我一般给电脑cpu进程数的2倍。编译一次需要的时间取决于电脑的性能,在公司编译使用服务器需要10分钟,本地电脑编译i7 16g的配置需要2小时,家里的笔记本需要8小时。一般都选择晚上下班,开始编译。以为白天要写代码,编译会让你的电脑卡到不能动,公司有好几套不同平台的代码都要编译那就交给编译服务器,虽然编译服务器会很快,但是公司各部门各模块都可能要编译白天的修改,服务器也经常会忙不过来出现排队
四、16.04上的编译失败解决
make: *** [out/host/linux-x86/obj/EXECUTABLES/aidl_intermediates/aidl_language_y.cpp]
解决:sudo apt-get install bison
clang: error: linker command failed with exit code 1 (use -v to see invocation)
build/core/host_shared_library_internal.mk:51: recipe for target 'out/host/linux-x86/obj32/lib/libart.so' failed
make: *** [out/host/linux-x86/obj32/lib/libart.so] Error 1
解决:
cd art/build/
vim Android.common_build.mk ++75 大概75行
ifneq ($(WITHOUT_HOST_CLANG),true) 改为 ifneq ($(WITHOUT_HOST_CLANG),false)
ART_HOST_CLANG :=true 改为 ART_HOST_CLANG :=false
![](http://upload-images.jianshu.io/upload_images/1315592-8e1f590a74d5aae3.png)
如果编译成功会有提示 ,在最后一行显示#### make completed successfully(编译耗时)####
五、源码阅读修改
虽然工具有很多,现介绍android开发人员比较熟悉的android studio
1、生成studio可识别的配置文件,编译成功以后键入命令:
make idegen && development/tools/idegen/idegen.sh
完成以后会在源码根目录下面生成:android.ipr android.iml
2、导入至studio
由于源码比较大导入消耗内存,所以要修改一下配置文件studio/bin/studio.vmoptions
默认是几百兆,我给了4个g。如下图
![](http://upload-images.jianshu.io/upload_images/1315592-f4a315b77600b87f.png)
android.ipr可直接在stdio中打开 file ---->open 选择android.ipr就可以将源码导进studio了 第一次需要一些时间