当前位置: 代码迷 >> Android >> ubuntu上载编译android源码(二)
  详细解决方案

ubuntu上载编译android源码(二)

热度:77   发布时间:2016-05-01 16:24:31.0
ubuntu下载编译android源码(二)
原帖:http://mmmyddd.freeshell.net/wiki/index.html
Contents [Hide]
1 Android Source Code Download
1.1 Website
1.2 Create Directory
1.3 Donwload Tool Chain
1.4 下载Android平台源代码
2 编译Android平台以及SDK
2.1 完全编译
2.2 模块编译
2.3 增量编译的步骤
Android Source Code Download
Website
android的最新源代码下载的官方网站是:http://source.android.com
源代码的下载说明页面是:http://source.android.com/download (现在貌似用还用不了)
现在的网站是http://git.android.com

Create Directory
在HOME(/home/justin)目录下创建如下目录结构:

  /home/justin/android:
  总计 36
  drwxr-xr-x 7 justin justin 4096 01-15 10:34 .
  drwxr-xr-x 58 justin justin 12288 01-15 10:39 ..
  drwxr-xr-x 2 justin justin 4096 01-15 10:34 applications
  drwxr-xr-x 2 justin justin 4096 01-15 10:33 downloads
  drwxr-xr-x 2 justin justin 4096 01-15 12:34 bin
  drwxr-xr-x 2 justin justin 4096 01-15 10:33 sdk
  drwxr-xr-x 2 justin justin 4096 01-15 10:33 src
  drwxr-xr-x 2 justin justin 4096 01-15 10:34 tools

将该目录添加到PATH环境变量:

export PATH=$HOME/android/bin:$PATH
如果需要反复使用,需要将这一行加入~/.bashrc启动脚本中

Donwload Tool Chain
Git
Git的版本在1.5.4之上

sudo apt-get install git-core
Proxy
安装代理软件:

apt-get install connect-proxy
Repo
repo是包装了git命令的python脚本:

curl http://android.git.kernel.org/repo > ~/android/bin/repo
网关内部需要设置curl代理:

假设网段的(http)代理为:wwwgate.freeshell.net:8080

curl --proxy wwwgate.freeshell.net:8080 http://android.git.kernel.org/repo > ~/android/bin/repo
加上可执行权限

chmod a+x ~/android/bin/repo
Python
Python的版本在2.4之上

sudo apt-get install python
JDK 1.6
在下列地址下载: http://java.sun.com/javase/downloads 解压得到如下文件: ~/android/downloads/jdk-6u11-linux-i586.bin

cd ~/android/downloads
chmode a+x jdk-6u11-linux-i586.bin
运行这个可执行文件, 将生成的目录jdk1.6.0_11拷贝到/usr/local/下

将下列环境变量加入~/.bashrc:

export PATH=/usr/local/jdk1.6.0_11/bin:$PATH
export JAVA_HOME=/usr/local/jdk1.6.0_11
export ANDROID_JAVA_HOME=$JAVA_HOME
Other tools
sudo apt-get install gcc g++
sudo apt-get install flex bison gperf libsdl1.2-dev libesd0-dev libwxgtk2.6-dev \
     build-essential zip curl libncurses5-dev zlib1g-dev valgrind libreadline5-dev
如果因为缺少X11/Xatom.h和X11/Xlib.h导致的build失败, 可以安装如下包:

sudo apt-get install x11proto-core-dev  # provides Xatom.h
sudo apt-get install libx11-dev         # provides Xlib.h
交叉编译环境
android emulator 默认的目标代码是arm7的代码。如果需要将目标代码移植到其他 版本的arm平台上去,需要重新配置相应的交叉编译环境。

下载Android平台源代码
1. 初始化要下载的文件列表:

cd ~/android/src
repo init -u git://android.git.kernel.org/platform/manifest.git
如果想检出除master外其他分支上的代码可以用-b选项:

cd ~/android/src
repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake
2. 配置git帐户

git config --global user.email "[email protected]"
git config --global user.name "xxxxxx"
3. 同步文件列表:

repo sync
在第一次下载全部代码完成后,可以按模块更新子项目的代码:

repo sync project-path
其中的project-path可以在src/.repo/manifests/default.xml中找到:

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <remote  name="korg"
           fetch="git://android.git.kernel.org/"
           review="review.source.android.com" />
  <default revision="master"
           remote="korg" />

  <project path="build" name="platform/build">
    <copyfile src="core/root.mk" dest="Makefile" />
  </project>

  <project path="kernel"
           name="kernel/common"
           revision="refs/heads/android-2.6.27" />

  <project path="bionic" name="platform/bionic" />

  <project path="bootable/bootloader/legacy" name="platform/bootable/bootloader/legacy" />
  <project path="bootable/diskinstaller" name="platform/bootable/diskinstaller" />
  <project path="bootable/recovery" name="platform/bootable/recovery" />
  ... ...
在网关内部使用需要设置代理:

新建如下文件:

#!/bin/sh
# /home/justin/android/bin/socks-gw.sh
# This script connects to a HTTP proxy using connect.c
connect -H wwwgate.freeshell.net:8080 $@
#!/bin/sh
# /home/justin/android/bin/socks-ssh.sh
ssh -o ProxyCommand="/home/justin/android/bin/socks-gw.sh %h %p" $@
增加这两个文件的可执行权限:

chmod a+x /home/justin/android/bin/socks-ssh.sh
chmod a+x /home/justin/android/bin/socks-gw.sh
可以将所有代理的环境变量放在:

#!/bin/sh
# /home/justin/proxy.sh

# http proxy setting
export HTTP_PROXY=http://wwwgate.freeshell.net:8080
export http_proxy=$HTTP_PROXY

# set git to use ssh over http proxy
export GIT_SSH="/home/justin/android/bin/socks-ssh.sh"
export GIT_PROXY_COMMAND="/home/justin/android/bin/socks-gw.sh"
需要的时候运行:

. ~/proxy.sh
编译Android平台以及SDK
完全编译
编译映像
cd ~/android/src
make
映像编译成功后会在目录 ~/android/src/out/target/product/generic 下产生一些image文件

ramdisk.img system.img userdata.img android-info.txt

验证,运行这些模块:

export ANDROID_PRODUCT_OUT=/home/justin/android/src/out/target/product/generic
cd out/host/linux-x86/bin
./emulator
SDK编译
在做完一次完全编译后,就可以build SDK了。

make sdk
注意:如果需要build SDK,需要安装sun-java5-jdk, 而不是sun-java6-jdk,否则会出现如下错误:

build/core/product_config.mk:207: WARNING: adding test OTA key
============================================
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=
============================================
Combining NOTICE files: out/target/product/generic/obj/NOTICE.txt
Finding NOTICE files: out/host/linux-x86/obj/NOTICE_FILES/hash-timestamp
Combining NOTICE files: out/host/linux-x86/obj/NOTICE.txt
Package: out/target/product/generic/generic-img-eng.justin.zip
SDK buildinfo: out/target/product/generic/sdk/sdk-build.prop
Docs droiddoc: out/target/common/docs/dx
javadoc: 错误 - 在 doclet 类 DroidDoc 中,方法 start 已抛出异常 java.lang.reflect.InvocationTargetException
com.sun.tools.javac.code.Symbol$CompletionFailure: 未找到 sun.util.resources.OpenListResourceBundle 的类文件
此时,可以考虑重新安装sun jdk5, 或者直接从http://java.sun.com/javase/downloads 下载到~/android/downloads/jdk-1_5_0_17-linux-i586.bin

sudo apt-get install sun-java5-jdk
并设置相应的.bashrc命令。

sdk编译成功后会在~/android/src/out/host/linux-x86/sdk/ 生成sdk的文件目录和压缩包:

android-sdk_eng.justin_linux-x86
android-sdk_eng.justin_linux-x86.zip

并在~/android/src/out/target/product/generic(generic是默认的产品名)下打包所有的映像文件:

generic-img-eng.justin.zip

生成的SDK目录结构为:

  /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86:
  总计 32
  drwxrwx---  6 justin justin 4096 02-13 17:06 .
  drwxr-x---  3 justin justin 4096 02-13 17:09 ..
  drwxrwx---  2 justin justin 4096 02-13 17:06 add-ons
  drwxrwx--- 14 justin justin 4096 02-13 17:06 docs
  -rw-rw----  1 justin justin  172 02-13 17:08 documentation.html
  drwxrwx---  3 justin justin 4096 02-13 17:06 platforms
  -rw-rw----  1 justin justin  225 02-13 17:08 RELEASE_NOTES.txt
  drwxrwx---  3 justin justin 4096 02-13 17:08 tools
安装生成的SDK只需要在.bashrc中增加:

export PATH=$PATH:/home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools
为了使用方便,将生成的SDK目录链结至~/android/sdk:

ln -sf /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools \
       ~/android/sdk
模块编译
在src目录执行:

cd ~/android/src
. build/envsetup.sh
envsetup.sh 提供了一些的bash函数定义,当运行了envsetup.sh后就可以使用help 命令来查看:

help
得到这些命令的帮助信息:

Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- croot:   Changes directory to the top of the tree.
- m:       Makes from the top of the tree.
- mm:      Builds all of the modules in the current directory.
- mmm:     Builds all of the modules in the supplied directories.
- cgrep:   Greps on all local C/C++ files.
- jgrep:   Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.

Look at the source to view more functions. The complete list is:
add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct choosetype choosevariant croot findmakefile gd
bclient get_abs_build_var getbugreports get_build_var getprebuilt gettop help isviewserverstarted jgrep lunch m mm mmm pid
printconfig print_lunch_menu resgrep runhat runtest setpaths set_sequence_number set_stuff_for_environment settitle smoke
test startviewserver stopviewserver tapas tracedmdump
其中对模块的编译有帮助的是tapas、m、mm、mmm这几个命令。

1.tapas - 以交互方式设置build环境,以下是运行效果:

tapas
第一步,选择目标设备:

Build for the simulator or the device?
     1. Device
     2. Simulator

Which would you like? [1]
第二步,选择目标代码格式:

Build type choices are:
     1. release
     2. debug

Which would you like? [1]
第三步,选择产品平台:

Product choices are:
     1. emulator
     2. generic
     3. sim
You can also type the name of a product if you know it.
Which would you like? [generic]
第四步,在选用参数下构建平台。

2. 独立模块的构件命令

m: Makes from the top of the tree.
mm: Builds all of the modules in the current directory.
mmm: Builds all of the modules in the supplied directories.
其中mmm后面要跟模块的根目录,不是所有的目录下都有子模块,那些含有Android.mk文件目录才是 模块的根目录,模块名可以从Android.mk的LOCAL_MODULE或者LOCAL_PACKAGE_NAME变量中得到。

单独编译某模块,需要在mmm后面指定模块路径,例如编译application中的Contacts:

mmm packages/apps/Contacts/
或者在src目录下直接运行make module name:

cd ~/android/src
make Contacts
增量编译的步骤
1. 修改代码

2. 编译所修改的代码所在模块,例如:

cd ~/android/src
mmm packages/apps/Contacts
3. 在~/android/src中运行:

cd ~/android/src
make snod
该命令生成一个新的系统映像system.img

4.将这个系统映像拷贝至sdk下:

cd ~/android/src
cp out/target/product/generic/system.img \
   out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/lib/images/
5. 删除程序遗留的数据:

out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/emulator -wipe-data