根据Android官网提供的方法设置编译环境,编译源码
对于Andrid无法下载的问题,可以移步清华下载AOSP,具体链接:https://mirrors.tuna.tsinghua.edu.cn/help/#AOSP
一. Mac OS X(10.9.2)编译环境设置
1. 创建大小写敏感的磁盘镜像
可以通过磁盘管理工具进行设置,也可以通过以下命令生成镜像文件
hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g ~/android.dmg.sparsefile
生成分区之后,可以在~/.bash_profile中添加以下命令来加载分区
# mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg.sparsefile -mountpoint /Volumes/android; }
在terminal中输入mountAndroid即可加载分区
2. 需要安装的工具
(1) 安装Xcode,目前虽然有5.1.0版本,但是需要安装5.0.2版本,在编译源码的时候无法正确通过。
(2) 安装MacPort
注:确定/opt/local/bin在/usr/bin之前,如果没有,执行以下命令,或者修改.bash_profile
export PATH=/opt/local/bin:$PATH
(3) 通过MacPort安装make,git,GPG, 如果本地已经安装,或者版本已经比较新了,无需安装
sudo port install gmake libsdl git-core gnupg
(4) 关于make 3.82版本
make 3.82版本有bug,需要退回3.81,通过以下命令确认版本
make -v
GNU Make 3.81
Copyright (C) 2006 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.
This program built for i386-apple-darwin11.0
因为我的机器上已经是3.81版本所以没用操作这个过程,具体可以查阅Android 官网
(5) 修改文件描述符限制
在~/.bash_profile中添加以下内容
# set the number of open files to be 1024
ulimit -S -n 1024
二. 下载源码
1. 创建目录~/bin
mkdir ~/bin
添加以下命令到~/.bash_profile
export PATH=~/bin:$PATH
2. 安装repo,这里没用安装官网文档来,具体原因你懂得
$ curl https://git-repo.googlecode.com/files/repo-1.19 > ~/bin/repo
然后修改repo为可执行权限
chmod a+x ~/bin/repo
3. 为了方便下载,修改repo文件,如下
#REPO_URL = 'http://gerrit.googlesource.com/git-repo'
REPO_URL='http://code.google.com/p/git-repo/'
4. 创建个源码目录mkdir android-source准备下载
cd android-source
5. 执行repo init, git库和官网的不一样,原因不解释
从master 下载源码
repo init -u git://Android.git.linaro.org/platform/manifest.git
或者 分支上下载
repo init -u git://Android.git.linaro.org/platform/manifest.git -b “branch tag”
我选择的是比较新的android-4.4.2_r1
repo init -u git://Android.git.linaro.org/platform/manifest.git -b android-4.4.2_r1
可以在官网查看你想要下载的源码对应的tag,
执行完之后,在本地会有个目录.repo
然后在android-source目录下执行repo sync下载代码
下载代码需要很长时间,如果感觉没用继续下载,可以ctrl+c杀掉,继续执行repo sync 直到下载完成。
下载完成后,android-source下一共27G,.repo 17G, 源码10G左右
三. 可以编译代码
将代码从android-source目录拷贝到挂载的之前创建的分区/Volumes/android,然后在这个分区下可以删除.repo目录,这个目录在编译代码时,没用并且占去很多空间
1. 初始化编译环境
source build/envsetup.sh
2. 选择编译目标,具体查阅官网
lunch full-eng
3. 编译代码
make
或者
make -j4
我在编译过程出现两次错误。
1. build/core/base_rules.mk:130: *** external/webrtc/src/system_wrappers/source: MODULE.TARGET.STATIC_LIBRARIES.libwebrtc_system_wrappers already defined by external/webrtc/src/system_wrappers/source. Stop.
这应该是个bug,需要在编译代码前,在terminal中取消NDK_ROOT设置,可以参阅
http://stackoverflow.com/questions/18039555/module-already-defined-when-compiling-android-4-3-source-code
unset NDK_ROOT
2. 这个错误就是Xcode版本的问题,出错log如下
target thumb C++: libstagefright_amrnbenc <= frameworks/av/media/libstagefright/codecs/amrnb/enc/src/spstproc.cpp target thumb C++: libstagefright_amrnbenc <= frameworks/av/media/libstagefright/codecs/amrnb/enc/src/ton_stab.cpp Gyp timestamp: /Users/lo/Documents/10-repos/02-cyanogenmod/new/android/system/out/target/product/mako/obj/GYP/net_net_resources_gyp_intermediates/net_resources.stamp Gyp timestamp: /Users/lo/Documents/10-repos/02-cyanogenmod/new/android/system/out/target/product/mako/obj/GYP/webkit_webkit_resources_gyp_intermediates/webkit_resources.stamp Gyp timestamp: /Users/lo/Documents/10-repos/02-cyanogenmod/new/android/system/out/target/product/mako/obj/GYP/skia_skia_library_gyp_intermediates/skia_library.stamp Gyp action: Generating icui18n shim headers. (/Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/shim_headers/icui18n/target/third_party/icu/source/i18n/unicode/basictz.h) Gyp action: Generating icuuc shim headers. (/Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/shim_headers/icuuc/target/third_party/icu/source/common/unicode/brkiter.h) Gyp timestamp: /Users/myuser/android/system/out/target/product/mako/obj/GYP/ui_base_strings_ui_strings_gyp_intermediates/ui_strings.stamp mkdir -p /Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/ui/jni; cd external/chromium_org/ui; ../base/android/jni_generator/jni_generator.py --input_file android/java/src/org/chromium/ui/Clipboard.java --output_dir "/Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/ui/jni" --optimize_generation 0 --jarjar ../android_webview/build/jarjar-rules.txt mkdir -p /Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/ui/jni; cd external/chromium_org/ui; ../base/android/jni_generator/jni_generator.py --input_file android/java/src/org/chromium/ui/gfx/BitmapHelper.java --output_dir "/Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/ui/jni" --optimize_generation 0 --jarjar ../android_webview/build/jarjar-rules.txt mkdir -p /Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/ui/jni; cd external/chromium_org/ui; ../base/android/jni_generator/jni_generator.py --input_file android/java/src/org/chromium/ui/gfx/DeviceDisplayInfo.java --output_dir "/Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/ui/jni" --optimize_generation 0 --jarjar ../android_webview/build/jarjar-rules.txt Traceback (most recent call last): File "../base/android/jni_generator/jni_generator.py", line 1103, inTraceback (most recent call last): File "../base/android/jni_generator/jni_generator.py", line 1103, in Traceback (most recent call last): File "../base/android/jni_generator/jni_generator.py", line 1103, in sys.exit(main(sys.argv)) File "../base/android/jni_generator/jni_generator.py", line 1099, in main sys.exit(main(sys.argv)) File "../base/android/jni_generator/jni_generator.py", line 1099, in main sys.exit(main(sys.argv)) File "../base/android/jni_generator/jni_generator.py", line 1099, in main GenerateJNIHeader(input_file, output_file, options) File "../base/android/jni_generator/jni_generator.py", line 1016, in GenerateJNIHeader GenerateJNIHeader(input_file, output_file, options) File "../base/android/jni_generator/jni_generator.py", line 1016, in GenerateJNIHeader input_file, options) File "../base/android/jni_generator/jni_generator.py", line 570, in CreateFromFile input_file, options) File "../base/android/jni_generator/jni_generator.py", line 570, in CreateFromFile GenerateJNIHeader(input_file, output_file, options) File "../base/android/jni_generator/jni_generator.py", line 1016, in GenerateJNIHeader return JNIFromJavaSource(contents, fully_qualified_class, options) File "../base/android/jni_generator/jni_generator.py", line 538, in __init__ return JNIFromJavaSource(contents, fully_qualified_class, options) File "../base/android/jni_generator/jni_generator.py", line 538, in __init__ fully_qualified_class) SyntaxError: Unable to find any JNI methods for org/chromium/ui/gfx/BitmapHelper. input_file, options) File "../base/android/jni_generator/jni_generator.py", line 570, in CreateFromFile fully_qualified_class) SyntaxError: Unable to find any JNI methods for org/chromium/ui/gfx/DeviceDisplayInfo. return JNIFromJavaSource(contents, fully_qualified_class, options) File "../base/android/jni_generator/jni_generator.py", line 538, in __init__ fully_qualified_class) SyntaxError: Unable to find any JNI methods for org/chromium/ui/Clipboard. make: *** [/Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/ui/jni/DeviceDisplayInfo_jni.h] Error 1 make: *** Waiting for unfinished jobs.... make: *** [/Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/ui/jni/BitmapHelper_jni.h] Error 1 make: *** [/Users/myuser/android/system/out/target/product/mako/obj/GYP/shared_intermediates/ui/jni/Clipboard_jni.h] Error 1
重新安装Xcode 5.0.2就可以编译通过,chromium has trouble building with 5.1
编译完成后,40G的分区被占用了30G。
在目录
/Volumes/android/out/target/product/generic
ls
android-info.txt fake_packages root
cache installed-files.txtsymbols
cache.img obj system
clean_steps.mk previous_build_config.mksystem.img
data ramdisk.imguserdata.img