Android源码编译ubuntu 15.10

一、准备工作
编译环境

官方要求:https://source.android.com/source/initializing.html;
我的环境:ubuntun 15.10 64位,openjdk 1.7, python 2.7.

These instructions apply to all branches, including master.

The Android build is routinely tested in house on recent versions of
Ubuntu LTS (14.04), but most distributions should have the required
build tools available. Reports of successes or failures on other
distributions are welcome.

For Gingerbread (2.3.x) and newer versions, including the master
branch, a 64-bit environment is required. Older versions can be
compiled on 32-bit systems.

Note: See the Requirements for the complete list of hardware and
software requirements. Then follow the detailed instructions for
Ubuntu and Mac OS below.

Installing the JDK

The master branch of Android in the Android Open Source Project (AOSP)
requires Java 8. On Ubuntu, use OpenJDK.

Java 8: For the latest version of Android

For Ubuntu >= 15.04

$ sudo apt-get update
$ sudo apt-get install openjdk-8-jdk

针对不同的android版本,编译的jdk版本也有要求,具体如下:

To develop older versions of Android, download and install the corresponding version of the Java JDK:
Java 7: for Lollipop through Marshmallow
Java 6: for Gingerbread through KitKat
Java 5: for Cupcake through Froyo

以下是我的安装步骤:
1. 首先安装open-jdk-7,执行以下命令:

$ sudo apt-get update
$ sudo apt-get install openjdk-7-jdk

安装完成后配置下java的环境变量:

$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac

在终端输入java -version和javac -version看下版本是否是open-jdk-1.7

  1. 安装一些依赖,否则我们在编译的时候会报一些错误,执行以下命令:
sudo apt-get install git-core gnupg flex bison gperf build-essential \
  zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 \
  lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache \
  libgl1-mesa-dev libxml2-utils xsltproc unzip
至此,我们的编译环境已经准备好了,开始下载源码吧。
二、下载源码

首先我们要下载一个repo工具,这个工具是由google提供的,打开终端,在用户的根目录下创建一个目录用来存放repo库,同时我们要把目录加入到环境变量中,这样我才能在终端中执行repo命令,执行以下命令:

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

接下来我们在repo_dir目录下面执行curl命令下载repo脚本,repo是一个用python写的脚本,当然这里要翻墙的,后面使用到的同步都需要使用翻墙工才行,我使用的是Proxychians + shadowsocks:

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

按装完成后,我们要创建一个目录用来存放我们下载的android源码,执行以下命令:

$ make AndroidSource
$ cd AndroidSource

接下来我们要配置git帐号(这里使用google的账户即可,没有的还需要去注册一个https://www.google.com/accounts),因为Android源码是放在google云仓库中,,如果没有安装Git的请自行安装,执行以下命令:

$ git config --global user.name "王洋"
$ git config --global user.email "wangyangbayern@gmail.com"

配置好之后,我们要初始化以下repo脚本,执行命令:

 repo init -u https://android.googlesource.com/platform/manifest

这里初始化的就是等下我们下载的android源码的配置文件,这里我们可以设置下载的android源码版本,上面的命令默认下载的是最新的android源码。下面这条命令就可以指定不同的Android版本,命令如下:

proxychains repo init -u https://android.googlesource.com/platform/manifest -b android-5.1.1_r30

其中 -b 表示branch,后面就是对应的版本,如我这里选择的是==android-5.1.1_r30==
具体大家可以在这里查看:https://source.android.com/source/build-numbers.html#source-code-tags-and-builds大家可根据上需要选择自己要下载的版本。
执行以上步骤以后我们就在目录下面发现一个.*repo的文件,最后一步我们就可以开始执行下载命令了。*

二、下载源码

我们可以执行下面的命令进行下载,当然这里也是要翻墙的:

$ proxychains repo sync

但是这里会经常断掉,同步出错,我们只需要再次执行repo sync命令即可。为了方便我编写了一个脚本,可以循环执行下载命令,省去大家不停执行repo sync,脚本如下:

proxychains repo sync  -j16
while [ $? = 1 ]; do  
        echo “======sync failed, re-sync again======”  
        sleep 3  
        proxychains repo sync  -j16
done    

等待下载吧。我是下载了2晚上就好了。

三、编译源码

如果你按照官方的步骤做,一般不会出现什么大问题,我在编译中遇到了一点小问题。具体步骤如下:
1. 在AndroidSource,即你的源码下载目录执行如下命令,这条命令是执行编译环境的脚本:

$ source build/envsetup.sh
  1. 然后选择你要编译的系统版本一般有如下几种版本,默认会编译arm版本的,下面是对应的系统版本
DeviceCode nameBuild configuration
HiKeyhikeyhikey-userdebug
Nexus 6Pangleraosp_angler-userdebug
Nexus 5Xbullheadaosp_bullhead-userdebug
Nexus 6shamuaosp_shamu-userdebug
Nexus Playerfuguaosp_fugu-userdebug
Nexus 9volantis (flounder)aosp_flounder-userdebug
Nexus 5 (GSM/LTE)hammerheadaosp_hammerhead-userdebug
Nexus 7 (Wi-Fi)razor (flo)aosp_flo-userdebug
Nexus 7 (Mobile)razorg (deb)aosp_deb-userdebug
Nexus 10mantaray (manta)full_manta-userdebug
Nexus 4occam (mako)full_mako-userdebug
Nexus 7 (Wi-Fi)nakasi (grouper)full_grouper-userdebug
Nexus 7 (Mobile)nakasig (tilapia)full_tilapia-userdebug
Galaxy Nexus (GSM/HSPA+)yakju (maguro)full_maguro-userdebug
Galaxy Nexus (Verizon)mysid (toro)aosp_toro-userdebug
Galaxy Nexus (Experimental)mysidspr (toroplus)aosp_toroplus-userdebug
PandaBoard (Archived)pandaaosp_panda-userdebug
Motorola Xoom (U.S. Wi-Fi)wingrayfull_wingray-userdebug
Nexus Ssoju (crespo)full_crespo-userdebug
Nexus S 4Gsojus (crespo4g)full_crespo4g-userdebug

执行如下命令:

$ lunch

然后弹出选择版本的确认框,大家根据自己的需求选择就可以了。
3. 首先建议大家先执行如下命令,否则编译过程中可能会报错。

$ make update-api
#然后在执行编译命令
$ nake -j4

大家等待吧,我是编译了差不多2个多小时才编译完成的。

四、配置环境变量

最后一步,我们需要把编译输出目录里对应的adb、emulator命令所在目录加入环境变量,否则无法使用adb fastboot、emulator等命令哦-_-。两种办法:
- 通过再次执行 source build/envsetup.sh 命令,重新帮我们配置临时环境变量,但是这种关闭终端后就不行了(so 放弃了)
- 通过修改bashrc文件,一劳永逸,这种就简单了,执行如下命令:

$ vim .bashrc

在最后一行加上下面几句:

export PATH=$PATH:~/AndroidSource/out/host/linux-x86/bin
export PATH=$PATH:~/AndroidSource/prebuilts/android-emulator/linux-x86_64/

其中
>/AndroidSource/out/host/linux-x86/bin
>/AndroidSource/prebuilts/android-emulator/l>inux-x86_64/

分别对应的是adb、fastboot和emulator所在的目录,好了我们执行adb、emulator看下是否可以执行以下两条命令:

$ adb
$ emulator -v

adb
emulator
到此为止,终于完成了源码的编译,这个搞了我好久。总结下,对着官方文档做就行了,有问题找Google。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值