android源码编译网上很多教程,但所有的教程,发源地都是官网,所以,请在看到这篇文章的朋友,可以先移步官网。
关于源码下载和编译:https://source.android.com/source/index.html
前序:
我们知道,android是基于Linux内核的系统,但Linux内核代码是区分大小写的(猜测当时是基于一个区分大小写的文件系统开发的),所以存储Android源码的磁盘所在的文件系统一定是需要区分大小写,即大小写敏感的,才可以进行编译构建。
Android简介:
http://zh.wikipedia.org/wiki/Android
使用Mac的朋友应该都知道,OS X的文件系统是大小写不敏感的,即不区分大小写。Mac早期是基于Mach内核,新的
OS X系统是基于BSD Unix和内核,新的OS X结合BSD Unix、OpenStep和Mac OS 9的元素。它的最底层建基于Unix基础,其代码被称为Darwin
2011年以前苹果称自己的OS为“Mac OS X”,发布Mac OS X v10.7后,苹果称其为“OS X Lion”,其后的系统均改名为“OS X”
OS X简介:
http://zh.wikipedia.org/wiki/Mac_OS
言归正传:
简介:
1、源码tags以及tag所支持的硬件设备说明
https://source.android.com/source/build-numbers.html
初始化OS X Android源码Build环境
https://source.android.com/source/initializing.html
1、创建一个支持区分大小写的磁盘镜像
# hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g ~/android.dmg
2、磁盘镜像扩容(如果编译源码时,磁盘空间不够则需要使用到)
# hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage
3、挂载该大小写敏感的磁盘镜像到目录/Volumes/android(OS X挂载该镜像后,才支持在OS X中操作该磁盘)
hdiutil attach ~/android.dmg -mountpoint /Volumes/android
注意:如果系统使用hdiutil create创建镜像时,生成的镜像文件为android.dmg.sparsefile(不是android.dmg),则需要使用~/android.dmg.sparsefile替换~/android.dmg
4、卸载挂载到目录/Volumes/android的大小写敏感的磁盘镜像文件~/android.dmg
hdiutil detach /Volumes/android
注:为了方便挂载和卸载磁盘镜像文件,你可以写两个方法命令,将这两个方法命令加如到~/.bash_profile,例如:
命令行中执行mountAndroid挂载磁盘镜像 ~/android.dmg到目录/Volumes/android:
# mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; }
命令行中执行umountAndroid卸载挂载到目录/Volumes/android的磁盘镜像~/android.dmg:
# unmount the android file image
function umountAndroid() { hdiutil detach /Volumes/android; }
5、jdk安装
据官网描述,master和5.0.x需要Java 7,在Mac OS上使用jdk-7u71-macosx-x64.dmg
Android Gingerbread (2.3 - 2.3.2)到 KitKat(4.4 - 4.4.4),使用Java 6版本的Java JDK
注:Oracle仅仅在Java7时才开始提供基于Mac OS的JDK,所以,Java6时,并未提供基于Mac OS的JDK1.6版本
所以,Apple给开发者提供了基于Java6的JDK1.6版本的开发包,下载地址:
https://support.apple.com/kb/dl1572?locale=zh_CN
后续步骤,引用官方:
Master branch
To build the latest source in a Mac OS environment, you will need an Intel/x86 machine running Mac OS X v10.8 (Mountain Lion) or later, along with Xcode 4.5.2 or later including the Command Line Tools.
Branch 5.0.x and earlier branches
To build 5.0.x and earlier source in a Mac OS environment, you will need an Intel/x86 machine running Mac OS X v10.8 (Mountain Lion), along with Xcode 4.5.2 and Command Line Tools.
Branch 4.4.x and earlier branches
To build 4.2.x and earlier source in a Mac OS environment, you will need an Intel/x86 machine running Mac OS X v10.6 (Snow Leopard) or Mac OS X v10.7 (Lion), along with Xcode 4.2 (Apple's Developer Tools). Although Lion does not come with a JDK, it should install automatically when you attempt to build the source.
The remaining sections for Mac OS apply only to those who wish to build earlier branches.
Branch 4.0.x and all earlier branches
To build android-4.0.x and earlier branches in a Mac OS environment, you need an Intel/x86 machine running Mac OS X v10.5 (Leopard) or Mac OS X v10.6 (Snow Leopard). You will need the Mac OS X v10.5 SDK.
注:我使用的Xcode版本是Xcode5.1.1
Installing required packages
-
Install Xcode from the Apple developer site. We recommend version 3.1.4 or newer (e.g., gcc 4.2). Version 4.x could cause difficulties. If you are not already registered as an Apple developer, you will have to create an Apple ID in order to download.
-
Install MacPorts from macports.org.
Note: Make sure that
/opt/local/bin
appears in your path BEFORE/usr/bin
. If not, addexport PATH=/opt/local/bin:$PATH
to your
~/.bash_profile
.Note: If you do not have a
.bash_profile
file in your home directory, create one. -
Get make, git, and GPG packages from MacPorts:
$ POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg
If using Mac OS X v10.4, also install bison:
$ POSIXLY_CORRECT=1 sudo port install bison
Reverting from make 3.82
For versions of Android before ICS, there is a bug in gmake 3.82 that prevents android from building. You can install version 3.81 using MacPorts by taking the following steps:
-
Edit
/opt/local/etc/macports/sources.conf
and add a line that saysfile:///Users/Shared/dports
above the rsync line. Then create this directory:
$ mkdir /Users/Shared/dports
-
In the new
dports
directory, run$ svn co --revision 50980 http://svn.macports.org/repository/macports/trunk/dports/devel/gmake/ devel/gmake/
-
Create a port index for your new local repository:
$ portindex /Users/Shared/dports
-
Finally, install the old version of gmake with
$ sudo port install gmake @3.81
Setting a file descriptor limit
On Mac OS, the default limit on the number of simultaneous file descriptors open is too low and a highly parallel build process may exceed this limit.
To increase the cap, add the following lines to your ~/.bash_profile
:
# set the number of open files to be 1024
ulimit -S -n 1024
Optimizing a build environment (optional)
Setting up ccache
You can optionally tell the build to use the ccache compilation tool. Ccache acts as a compiler cache that can be used to speed up rebuilds. This works very well if you use make clean
often, or if you frequently switch between different build products.
Put the following in your .bashrc
(or equivalent):
export USE_CCACHE=1
By default the cache will be stored in ~/.ccache
. If your home directory is on NFS or some other non-local filesystem, you will want to specify the directory in your .bashrc
file as well:
export CCACHE_DIR=<path-to-your-cache-directory>
The suggested cache size is 50-100GB. You will need to run the following command once you have downloaded the source code:
prebuilts/misc/linux-x86/ccache/ccache -M 50G
On Mac OS, you should replace linux-x86
with darwin-x86
:
prebuilts/misc/darwin-x86/ccache/ccache -M 50G
When building Ice Cream Sandwich (4.0.x) or older, ccache is in a different location:
prebuilt/linux-x86/ccache/ccache -M 50G
This setting is stored in the CCACHE_DIR and is persistent.
Next: Download the source
Your build environment is good to go! Proceed to downloading the source.
Android源码下载:
1、下载指定tag的android 源码
https://source.android.com/source/downloading.html
编译构建:
https://source.android.com/source/building-running.html
遇到的问题:
问题一
接着可以开始编译了,但是没过多久又开始报错了。你会发现它提示:
SyntaxError: Unable to find any JNI methods for org/chromium/ui/Clipboard.
这个也是Xcode版本是高版本的问题。在github有个错误的解决方案:传送门
提醒一下,路径为:
external/chromium_org/base/android/jni_generator/jni_generator.py
问题解决后,开始编译构建:
构建成功图:
参考链接:
Mac 10.10 编译android 4.4.4 for nexus
注意:我在按如上的参考链接下,始终未能成功编译过,只是借鉴了里面的遇到的问题的解决办法,后面回归Xcode5.1.1编译通过,一开始用的Xcode6.3.1,始终未能编译过