记录一次Android源码的下载和编译

准备

  1. 确保安装了Git工具,最好使用代理
$ sudo apt-get install git

git socks5 代理设置 (https 代理设置,将命令中的socks5换成https)

$ git config --global http.proxy socks5://127.0.0.1:1080
$ git config --global https.proxy socks5://127.0.0.1:1080  

下载 repo 工具

  1. 确保在当前用户的home目录下有一个bin/目录,并且bin/目录在PATH环境变量中
$ mkdir ~/bin 
$ PATH=$HOME/bin:$PATH 
  1. 下载repo工具,确保它可以运行
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo 

下载AOSP源码

  1. 创建工作目录(名字合法即可)
$ mkdir WORKING_DIRECTORY 
$ cd WORKING_DIRECTORY 
  1. 下载清华大学初始化包
    https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar
$ wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar #下载初始化包 
$ tar xf aosp-latest.tar #解压 

20G大家慢慢下吧。下完后再回来继续…

$ cd aosp 
$ repo sync # 同步完整目录 

也可以只同步指定项目目录

$ repo sync platform/frameworks/base 

填坑

教程总是美好的,现实总是残酷的!同步提示git文件变更,让先提交更新。

... 
    default.xml 
error: .repo/manifests/: contains uncommitted changes 

搜索一下发现很多朋友遇见这个问题,也有比较统一的解决办法:

$ cd .repo/manifests 
$ git stash 
$ git clean -d -f 
$ cd - 
$ repo sync 

真解决了,也就不用在这里记录,一万个人的就会有一万种环境吧。直接去git仓库目录 ,查看远程仓库的地址。

$ git remote -v 
origin  https://aosp.tuna.tsinghua.edu.cn/platform/manifest (fetch) 
origin  https://aosp.tuna.tsinghua.edu.cn/platform/manifest (push) 

这个清华大学镜像仓库居然用浏览器打不开,直接换成googlesource的地址(如果你访问不了的话,原因你知道的…)

$ git remote rm origin 
$ git remote add origin https://android.googlesource.com/platform/manifest 
$ repo sync 
error: Your local changes to the following files would be overwritten by checkout:
    default.xml
Please, commit your changes or stash them before you can switch branches.
...

还是失败了,default.xml文件问题,再用上面的命令恢复文件….还是不行。不行,直接放大招。把manifests目录删除,从googlesource重新clone,再repo sync成功了。
(注意:这里的目录名问 manifests,clone 下来名为 manifest,记得加 s。)

《记录一次Android源码的下载和编译》 repo_sync.png

同步过程再次遇见git文件版本问题

error: .repo/repo/: contains uncommitted changes

同上方法解决,https://gerrit.googlesource.com/git-repo,重新 sync。

完成 repo sync

《记录一次Android源码的下载和编译》 2016-10-29 09-01-08屏幕截图.png

到这,源码算是下下来了。

编译

官方教程

  1. 需要先安装Openjdk
$ sudo add-apt-repository ppa:openjdk-r/ppa
$ sudo apt-get update
$ sudo apt-get install openjdk-8-jdk
$ sudo update-alternatives --config java
  1. 编译
$ make clobber #清除旧的编译文件
$ . build/envsetup.sh  #执行编译环境脚本
$ lunch aosp_arm-eng #选择编译的版本
$ make -j4  #开始编译 [-j]参数 --jobs[=N] 同时允许 N 个任务;无参数表明允许无限个任务。

编译过程中或许也会遇见一些问题了。类似这样:

《记录一次Android源码的下载和编译》 2016-10-29 12-15-18屏幕截图.png

注意高亮的部分,这里提示了错误的信息,我的环境没有安装billion,所以安装一下就可以继续了

...
FAILED: /bin/bash -c "prebuilts/misc/linux-x86/bison/bison -d  --defines=out/host/linux-x86/obj/STATIC_LIBRARIES/libaidl-common_intermediates/aidl_language_y.h -o out/host/linux-x86/obj/STATIC_LIBRARIES/libaidl-common_intermediates/aidl_language_y.cpp system/tools/aidl/aidl_language_y.yy"
prebuilts/misc/linux-x86/bison/bison: m4 子进程失败
...
$ sudo apt-get install bison 
$ make -j4

《记录一次Android源码的下载和编译》 2016-10-29 12-19-49屏幕截图.png

最后 耐心等待,祝大家顺利!

参考

Google官方教程
Gityuan博客
清华大学开源软件镜像站教程

    原文作者:Eirture
    原文地址: https://www.jianshu.com/p/1f0db4d071f2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞