学习了Android的版本管理工具Git和Repo之后,已经迫不及待的要下载Android的源代码、编译并运行了。(注:本文是基于Ubuntu11.04)
初始化编译环境
- 下载Android全部代码大概需要2G的磁盘空间,编译Android源码则至少需要10G的磁盘空间。所以,在开始之间最好先检查下系统的可用空间
- $ df -h
-
Filesystem Size Used Avail Use% Mounted on
-
/dev/sda8 37G 23G 13G 65% /
- 安装JDK
Ubuntu默认的包管理配置文件中不包含SUN JDK的地址,可以采用如下方法将其包仓库的地址通知给APT,并下载安装:
- $ sudo add-apt-repository "deb lucid partner"
-
$ sudo add-apt-repository "deb-src ubuntu lucid partner"
-
$ sudo apt-get update
-
$ sudo apt-get install sun-java6-jdk
- 安装Android编译所必须的包
- $ sudo apt-get install git-core gnupg flex bison gperf build-essential \
-
zip curl zlib1g-dev libc6-dev libncurses5-dev \
-
x11proto-core-dev libx11-dev libreadline5-dev \
-
libgl1-mesa-dev g++-multilib mingw32 tofrodos
(注:和Android官网提供的安装手册不一致的是,在Ubuntu11.04中,lib32ncurses5-dev和lib32readline5-dev包名字有所改变,ia32-libs和lib32z-dev已经不存在了,但是不影响编译运行)
- 配置USB访问权限
在Ubuntu系统中,普通用户默认情况下是不能直接访问USB设备的。但是,可以通过创建文件/etc/udev/rules.d/51-android.rule,并添加如下内容来配置用户可以访问USB设备:
- # adb protocol on passion (Nexus One)
-
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e12", MODE="0600", OWNER=""
-
# fastboot protocol on passion (Nexus One)
-
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0fff", MODE="0600", OWNER=""
-
# adb protocol on crespo (Nexus S)
-
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e22", MODE="0600", OWNER=""
-
# fastboot protocol on crespo (Nexus S)
-
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e20", MODE="0600", OWNER=""
下载Android源代码- 采用Android源码学习(2)使用Git和Repo进行版本管理所述的方法下载源代码
- 如果之前没有指定分支,可以通过如下方法指定分支,并更新代码
- $ repo init -b gingerbread
-
$ repo sync
- repo sync命令的作用就是下载整个Android源码到本地,这个操作时间较长,具体时间和网速有关(我整整下载了一宿)。
源码编译- 初始化环境变量
- $ source build/envsetup.sh
- including device/htc/passion/vendorsetup.sh
including device/samsung/crespo4g/vendorsetup.sh
including device/samsung/crespo/vendorsetup.sh
- 配置编译目标
- $ lunch
- You're building on Linux
-
-
Lunch menu... pick a combo:
-
1. generic-eng
-
2. simulator
-
3. full_passion-userdebug
-
4. full_crespo4g-userdebug
-
5. full_crespo-userdebug
-
-
Which would you like? [generic-eng]
-
-
============================================
-
PLATFORM_VERSION_CODENAME=REL
-
PLATFORM_VERSION=2.3.5
-
TARGET_PRODUCT=generic
-
TARGET_BUILD_VARIANT=eng
-
TARGET_SIMULATOR=false
-
TARGET_BUILD_TYPE=release
-
TARGET_BUILD_APPS=
-
TARGET_ARCH=arm
-
HOST_ARCH=x86
-
HOST_OS=linux
-
HOST_BUILD_TYPE=release
-
BUILD_ID=GINGERBREAD
-
============================================
- 编译源码
运行模拟器编译成功后,生成的全部目标文件都在源码所在目录的out目录下。在运行模拟器,体验Android之前,需要添加如下环境变量到你的.profile文件中:
- export ANDROID_SRC=${HOME}/android/source
-
export ANDROID_OUT=${ANDROID_SRC}/out
-
export PATH=${PATH}:${ANDROID_OUT}/host/linux-x86/bin
-
export ANDROID_PRODUCT_OUT=${ANDROID_OUT}/target/product/generic
然后,重新开启新终端,使刚才的设置生效,在命令行运行如下命令
终于看到了Android桌面了
编译错误解决方法备注
- 如果在Ubuntu11.10上编译Android,遇到如下错误
- host C++: libutils <= frameworks/base/libs/utils/RefBase.cpp
-
frameworks/base/libs/utils/RefBase.cpp: In member function ‘void android::RefBase::weakref_type::trackMe(bool, bool)’:
-
frameworks/base/libs/utils/RefBase.cpp:507:67: error: passing ‘const android::RefBase::weakref_impl’ as ‘this’ argument of ‘void android::RefBase::weakref_impl::trackMe(bool, bool)’ discards qualifiers [-fpermissive]
-
make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/libutils_intermediates/RefBase.o] Bd 1
解决方法:
- cd frameworks/base/
-
git cherry-pick c4fbbe06
- 如果在Ubuntu11.10上编译Android,遇到如下错误
- host C++: obbtool <= frameworks/base/tools/obbtool/Main.cpp
-
:0:0: error: "_FORTIFY_SOURCE" redefined [-Werror]
-
:0:0: note: this is the location of the previous definition
-
cc1plus: all warnings being treated as errors
-
make: *** [out/host/linux-x86/obj/EXECUTABLES/obbtool_intermediates/
-
Main.o] Error 1
解决方法:
Ubuntu11.10默认提供的gcc和g++版本为4.6,安装版本为4.4的gcc和g++就可以解决此编译问题。
阅读(2903) | 评论(1) | 转发(0) |