一.初始化编译环境
google推荐的编译环境是在Ubuntu LTS(10.04),但是新的LTS版本12.04已经出来,没必要在旧版本上做文章了,很多行特性和驱动10.04上都没有(例如无线网卡驱动),所以果断选择12.04的LTS版本。
对于2.3.X和更新的版本,需要64位的系统,之前的老版本2.2以下(包括2.2)可以在32位版本上编译,另外不推荐虚拟机,因为官网上写的虚拟机配置过于变态,要16G的RAM/swap和30G以上的硬盘,速度上也不会很理想。
在Ubuntu12.04下面需要:
-
Python 2.5 -- 2.7, 下载地址 python.org.
-
GNU Make 3.81 -- 3.82, 下载地址 gnu.org,
-
JDK 6 :2.3以上的版本; JDK 5 2.2和2.2以下的版本. 下载地址http://www.oracle.com/technetwork/java/javase/downloads/index.html
-
Git 1.7 以上. 下载地址 git-scm.com.
安装JDK1.6.0(不支持jdk7.0)
1.下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jdk6-downloads-1637591.html
选择64位的linux版本,这里安装好以后,后面设置应用开发环境还用得上,
这时我下载的最新版本是jdk-6u33-linux-x64.bin.
2. 安装
修改文件的权限
chmod 777 jdk-6u33-linux-x64.bin
执行安装
./jdk-6u33-linux-x64.bin
3.配置环境变量
修改/etc/profile 在后面添加java文件夹的路经
- export JAVA_HOME=/home/gexueyun/jdk1.6.0_33
- export PATH=$PATH:$JAVA_HOME/bin:/home/gexueyuan/android-sdk-linux/platforms-tools:/home/gexueyuan/eclipse/:/home/gexueyuan/bin
export JAVA_HOME=/home/gexueyun/jdk1.6.0_33
export PATH=$PATH:$JAVA_HOME/bin:/home/gexueyuan/android-sdk-linux/platforms-tools:/home/gexueyuan/eclipse/:/home/gexueyuan/bin
此时输入:java -ersion可能还看不见版本,重启或者重新登录后就能看见java的版本
- java version "1.6.0_33"
- Java(TM) SE Runtime Environment (build 1.6.0_33-b04)
- Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03, mixed mode)
java version "1.6.0_33"
Java(TM) SE Runtime Environment (build 1.6.0_33-b04)
Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03, mixed mode)
系统中有时候会安装openjdk,但是我们编译android的时候要用jdk1.6.0,有三种方法解决这个问题
第一,删除openjdk,我怕以后系统里面还有什么用处,果断不敢删啊
第二种方法就是修改环境变量,让系统调用java javac jar的时候优先调用你安装的jdk,修改/etc/environment
- PATH="/home/gexueyuan/jdk1.6.0_33/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
PATH="/home/gexueyuan/jdk1.6.0_33/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
这样系统就会优先调用你下载的jdk
这时候输入命令
- update-alternatives --config jar
- update-alternatives --config javac
- update-alternatives --config java
update-alternatives --config jar
update-alternatives --config javac
update-alternatives --config java
看到的还是
/usr/lib/jvm/java-6-openjdk-amd64/bin/ 中的
但是系统调用,会优先调用你安装的jdk
第三种,
就是比较正式的安装jdk到系统目录
1.把下载的文件
- 移动到/usr/lib/jvm下面,没有jvm这个目录就手动创建一个
移动到/usr/lib/jvm下面,没有jvm这个目录就手动创建一个
2.执行安装,这要系统权限的
- sudo ./jdk-6u33-linux-x64.bin
sudo ./jdk-6u33-linux-x64.bin
3.配置jdk
sudo vim /etc/enviroment
添加:
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=".:$JAVA_HOME/bin"
JAVA_HOME="/usr/lib/jvm/jdk1.6.0_33"
安装java/javac/jar,执行如下命令:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java1.6.0_33/bin/java 300
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java1.6.0_33/bin/javac 300
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/java1.6.0_33/bin/jar 300
sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config jar
执行后三条命令的时候,可能会出现选择的提示,一定要选择jdk1.6.0,而不是openjdk
然后使用java -version查看版本是否为1.6.0
二.安装必要的编译包
12.04下的安装包命令为:
- $ sudo apt-get install git-core gnupg flex bison gperf build-essential \
- zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev \
- libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 \
- libgl1-mesa-dev g++-multilib mingw32 openjdk-6-jdk tofrodos \
- python-markdown libxml2-utils xsltproc zlib1g-dev:i386
- $ sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so
$ sudo apt-get install git-core gnupg flex bison gperf build-essential \
zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev \
libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 \
libgl1-mesa-dev g++-multilib mingw32 openjdk-6-jdk tofrodos \
python-markdown libxml2-utils xsltproc zlib1g-dev:i386
$ sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so
这里面可以选择不安装openjdk
安装usb驱动(可选)
由于ubuntu的系统设置问题,普通用户是没有权限获取usb设备的,所以需要创建下面这个文件
/etc/udev/rules.d/51-android.rules
内容是:
- # adb protocol on passion (Nexus One)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e12", MODE="0600", OWNER="<username>"
- # fastboot protocol on passion (Nexus One)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0fff", MODE="0600", OWNER="<username>"
- # adb protocol on crespo/crespo4g (Nexus S)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e22", MODE="0600", OWNER="<username>"
- # fastboot protocol on crespo/crespo4g (Nexus S)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e20", MODE="0600", OWNER="<username>"
- # adb protocol on stingray/wingray (Xoom)
- SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", ATTR{idProduct}=="70a9", MODE="0600", OWNER="<username>"
- # fastboot protocol on stingray/wingray (Xoom)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="708c", MODE="0600", OWNER="<username>"
- # adb protocol on maguro/toro (Galaxy Nexus)
- SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", ATTR{idProduct}=="6860", MODE="0600", OWNER="<username>"
- # fastboot protocol on maguro/toro (Galaxy Nexus)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e30", MODE="0600", OWNER="<username>"
- # adb protocol on panda (PandaBoard)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d101", MODE="0600", OWNER="<username>"
- # fastboot protocol on panda (PandaBoard)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d022", MODE="0600", OWNER="<username>"
- # usbboot protocol on panda (PandaBoard)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d00f", MODE="0600", OWNER="<username>"
- # usbboot protocol on panda (PandaBoard ES)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d010", MODE="0600", OWNER="<username>"
# adb protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e12", MODE="0600", OWNER="<username>"
# fastboot protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0fff", MODE="0600", OWNER="<username>"
# adb protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e22", MODE="0600", OWNER="<username>"
# fastboot protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e20", MODE="0600", OWNER="<username>"
# adb protocol on stingray/wingray (Xoom)
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", ATTR{idProduct}=="70a9", MODE="0600", OWNER="<username>"
# fastboot protocol on stingray/wingray (Xoom)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="708c", MODE="0600", OWNER="<username>"
# adb protocol on maguro/toro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", ATTR{idProduct}=="6860", MODE="0600", OWNER="<username>"
# fastboot protocol on maguro/toro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e30", MODE="0600", OWNER="<username>"
# adb protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d101", MODE="0600", OWNER="<username>"
# fastboot protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d022", MODE="0600", OWNER="<username>"
# usbboot protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d00f", MODE="0600", OWNER="<username>"
# usbboot protocol on panda (PandaBoard ES)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d010", MODE="0600", OWNER="<username>"
其中username是你需要获取权限的用户名
三.下载源代码
在home目录下建立bin文件夹:
- $ mkdir ~/bin
- $ PATH=~/bin:$PATH
$ mkdir ~/bin
$ PATH=~/bin:$PATH
下载repo并且更改权限
- $ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
- $ chmod a+x ~/bin/repo
$ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
$ chmod a+x ~/bin/repo
初始化Repo客户端
在你的工作目录下面创建一个存放源代码的文件夹并且进入文件夹,比如:
- $ mkdir WORKING_DIRECTORY
- $ cd WORKING_DIRECTORY
$ mkdir WORKING_DIRECTORY
$ cd WORKING_DIRECTORY
初始化Repo获得最新的源代码,即主分支源代码:
- $ repo init -u https://android.googlesource.com/platform/manifest
$ repo init -u https://android.googlesource.com/platform/manifest
如果想获得其他版本的分支源代码,要在命令后面用 -b参数指定 :
比如最新的代码是
android-4.1.1_r3
$ repo init -u https://android.googlesource.com/platform/manifest -b android-4.1.1_r1
可以指定获取
android-4.1.1_r1
初始化完成以后,输入
$ repo sync
获得你想要的源代码
下载源代码时候的一个错误:
<code style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; font-family: Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif; vertical-align: baseline;"><span style="background-color: rgb(238, 238, 238);"><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;">Syncing</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> work tree</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(128, 0, 0); vertical-align: baseline;">100</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">%</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">(</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(128, 0, 0); vertical-align: baseline;">171</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">/</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(128, 0, 0); vertical-align: baseline;">171</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">),</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline;">done</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> external</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">dbus</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">/:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> discarding </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(128, 0, 0); vertical-align: baseline;">43</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> commits error</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> build</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">/:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> platform</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">build checkout </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(128, 0, 0); vertical-align: baseline;">0683a77348f2a0a529a8e2bcfdf4b6dc6b4c5f5b</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> error</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> cts</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">/:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;"> platform</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline;">cts checkout a3779fa378345bfd8699e91de46b29563be4beb2 </span></span></code>在完成同步后,repo报错,其原因是我这里先下载了4.1.1_r1的源代码,没下载完成,后面又初始化了4.1.1_r3继续下载,所以版本库里面很多文件不一样,编译的时候也会报错,这里我们用git命令处理一下:
在报错的路径下面使用:git checkout -f 把源代码强制还原成为最原始的源代码,无更改
再次repo sync,不会报错。
四.编译源代码
初始化脚本
$ source build/envsetup.sh
$ lunch full-eng
第二个命令的参数可以参考官网,说明很详细,你想生成什么样的文件,就设定什么样的参数
http://source.android.com/source/building.html
编译代码:
$ make -jN
其中N参数代表你计算机的线程参数,数字越大,编译速度越快,但是要看你计算机支持到什么地步
事实证明,4.6的GCC也可以编译成功,不像网上说的那样只能用gcc4.4
我的gcc版本
- gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
- Copyright (C) 2011 Free Software Foundation, Inc.
- This is free software; see the source for copying conditions. There is NO
- warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
其实,编译android系统和host主机自带的gcc版本没有关系,用的是源代码文件夹prebuild中内置的交叉工具链,如果对GCC工具链
还是不放心,那么就安装一个4.4版本的gcc:
- sudo apt-get install gcc-4.4
- sudo apt-get install g++-4.4
sudo apt-get install gcc-4.4
sudo apt-get install g++-4.4
安装完成以后,进入/usr/bin目录
- $ ls -l gcc*
$ ls -l gcc*
- lrwxrwxrwx 1 root root 7 Sep 3 13:45 gcc -> gcc-4.6
- -rwxr-xr-x 1 root root 259232 Apr 16 2012 gcc-4.4
- -rwxr-xr-x 1 root root 353216 Apr 16 2012 gcc-4.6
lrwxrwxrwx 1 root root 7 Sep 3 13:45 gcc -> gcc-4.6
-rwxr-xr-x 1 root root 259232 Apr 16 2012 gcc-4.4
-rwxr-xr-x 1 root root 353216 Apr 16 2012 gcc-4.6
先备份一下原来的gcc软链接,然后新建软链接gcc指向gcc-4.6
- sudo mv gcc gcc.bak
- sudo ln -s gcc-4.4 gcc
sudo mv gcc gcc.bak
sudo ln -s gcc-4.4 gcc
同理,修改下g++。
然后查看gcc和g++版本号,就能发现都显示4.4.7的版本
编译错误:
如果报错如下
那么就是jdk的问题,是因为你使用了系统的openjdk,重新配置jdk即可解决。
五.运行emulator:
编译成功后,运行模拟器
$ emulator
如果找不到命令,那么重新输入:
$ source build/envsetup.sh
$ lunch full-eng
再运行
$ emulator
模拟器就运行起来了。
原站地址-- http://blog.csdn.net/gexueyuan/article/details/7772387