Mac OS X下用Eclipse浏览和编译Android源代码
<font face="Courier New">为了能够同时开发</font><span style="font-family:'Times New Roman', serif;">iPhone</span>
<font face="Courier New">应用程序和</font><span style="font-family:'Times New Roman', serif;">Android</span>
<font face="Courier New">应用程序,我将所有的开发环境都集成到了自己的</font><span style="font-family:'Times New Roman', serif;">Macbook</span>
<font face="Courier New">上,虽然用</font><span style="font-family:'Times New Roman', serif;">Macbook</span>
<font face="Courier New">开发普通的</font><span style="font-family:'Times New Roman', serif;">Android</span>
<font face="Courier New">应用程序没什么问题,但是你要是想用</font><span style="font-family:'Times New Roman', serif;">Eclipse</span>
<font face="Courier New">浏览和编译</font><span style="font-family:'Times New Roman', serif;">Android</span>
<font face="Courier New">源代码并且对其</font><span style="font-family:'Times New Roman', serif;">SDK</span>
<font face="Courier New">进行调试,就是很麻烦的事情了。通过</font><span style="font-family:'Times New Roman', serif;">Google</span>
<font face="Courier New">发现绝大部分</font><span style="font-family:'Times New Roman', serif;">Android</span>
<font face="Courier New">源代码的编译和开发环境都是在</font><span style="font-family:'Times New Roman', serif;">Ubantu</span>
<font face="Courier New">系统上,虽然</font><span style="font-family:'Times New Roman', serif;">Android</span>
<font face="Courier New">官网提供了在</font><span style="font-family:'Times New Roman', serif;">MacOS X</span>
<font face="Courier New">上编译源码的方式,但是尝试了多次总是发现有问题。</font>
<font face="Courier New">刚好这段时间</font><span style="font-family:'Times New Roman', serif;">Android</span>
<font face="Courier New">源码网站被黑,终于找了一个方法能够下载到完整的代码,于是就借此机会再次尝试一下编译源码,同时在</font><span style="font-family:'Times New Roman', serif;">Eclipse</span>
<font face="Courier New">里面调试成功,记录下来整个过程方便以后查阅。</font>
<font face="Courier New">一</font><span style="font-family:'Times New Roman', serif;">.</span>
<font face="Courier New">设置</font><span style="font-family:'Times New Roman', serif;">MacOS X</span>
<font face="Courier New">编译环境</font>
<font face="Courier New">这一点是根据</font><span style="font-family:'Times New Roman', serif;">Android</span>
<a target="_blank" href="https://www.ancii.com/link/v1/Ymn-a-Ko6yGc3JTN79ENKi1VmOxfG4DDe6D1iXVsvsdb6ntCOjajXs-ini-F7WzPlFOvZA2DWH1wewuodBUCiA/" rel="nofollow" title="<font face=Courier New>官方网站</font>"><font face="Courier New">官方网站</font></a><font face="Courier New">提供的方法来设置的,我这里只做了一些翻译和解释工作。我自己的</font><span style="font-family:'Times New Roman', serif;">Macbook</span>
<font face="Courier New">已经升级到了</font><span style="font-family:'Times New Roman', serif;">Lion</span>
<font face="Courier New">,对于操作系统配置什么就不用去担心了。直接上吧。</font>
1.
创建一个大小写敏感的磁盘镜像文件
<font face="Courier New">由于编译</font><span style="font-family:'Times New Roman', serif;">Android</span>
<font face="Courier New">源代码需要将代码存放在大小写敏感的磁盘当中,所以我们需要首先创建一个大小写敏感的磁盘镜像文件(如果你的磁盘已经是大小写敏感的,则省略这个步骤,直接进入步骤</font><span style="font-family:'Times New Roman', serif;">2</span>
<font face="Courier New">)</font><span style="font-family:'Times New Roman', serif;">.</span>
<font face="Courier New">进入应用程序</font><span style="font-family:'Times New Roman', serif;">/</span>
<font face="Courier New">实用工具</font><span style="font-family:'Times New Roman', serif;">/</span>
<font face="Courier New">磁盘工具,选择文件</font><span style="font-family:'Times New Roman', serif;">-></span>
<font face="Courier New">新建</font><span style="font-family:'Times New Roman', serif;">-></span>
<font face="Courier New">新建空白映像:</font>
<font face="Courier New"><img src="https://www.linuxidc.com/upload/2011_10/111016211585771.gif" title="Mac OS X下用Eclipse浏览和编译Android源代码" alt="Mac OS X下用Eclipse浏览和编译Android源代码"><br /></font>
<font face="Courier New">其中名称可以随意,大小可以选择自定义</font><span style="font-family:'Times New Roman', serif;">30G</span>
<font face="Courier New">,格式选用</font><span style="font-family:'Times New Roman', serif;">MacOs</span>
<font face="Courier New">扩展(区分大小写),映像格式选择稀疏磁盘映像,这样文件的大小会随着文件的多少而增长,不用一上来就搞</font><span style="font-family:'Times New Roman', serif;">30G</span>
<font face="Courier New">那么大,其它都是默认选项。</font>
<span style="font-weight:normal;"><font face="Courier New">同时,你还可以通过命令行方式生成该磁盘镜像:</font></span>
<span style="font-weight:normal;">$hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 30g /</span>
<span style="color:#0000ff;"><em><span style="font-weight:normal;">path-to-image</span></em></span>
<span style="font-weight:normal;">/AndroidDisk.sparseimage</span>
<font face="Courier New"><span style="font-weight:normal;">生成的硬盘镜像文件名就是</span><span style="font-family:'Times New Roman', serif;"><span style="font-weight:normal;"></span></span></font>
AndroidDisk.sparseimage<span style="font-weight:normal;"><font face="Courier New">了。似乎创建完以后</font></span><span style="font-family:'Times New Roman', serif;"><span style="font-weight:normal;">OS</span></span>
<span style="font-weight:normal;"><font face="Courier New">会自动把这个镜像加载上去,挂载点是</font></span><span style="font-family:'Times New Roman', serif;"><span style="font-weight:normal;">/Volumes/android</span></span>
<span style="font-weight:normal;"><font face="Courier New">,其中</font></span><span style="font-family:'Times New Roman', serif;"><span style="font-weight:normal;">android</span></span>
<span style="font-weight:normal;">就是上面我们为磁盘镜像设置的名称</span>
。
<font face="Courier New">另外也可以将如下一行代码加入到</font><span style="font-family:'Times New Roman', serif;">~/.profile</span>
<font face="Courier New">当中,</font><span style="font-family:'Times New Roman', serif;">function mountAndroid { hdiutil attach /</span>
<span style="font-family:'Times New Roman', serif;"><span style="color:#0000ff;"><em>path-to-image</em></span></span>
<span style="font-family:'Times New Roman', serif;">/AndroidDisk.sparseimage -mountpoint /Volumes/android; }</span>
<font face="Courier New">这样以后每次用</font><span style="font-family:'Times New Roman', serif;">mountAndroid</span>
<font face="Courier New">命令就行了。</font>
2.
安装所需要的包
<font face="Courier New">1</font>
<font face="Courier New">)首先你要安装的就是</font><span style="font-family:'Times New Roman', serif;"><a target="_blank" href="https://www.ancii.com/link/v1/pbsrIclBJAbef2_zzYFq9qlqoVeCP-4MVG5YmGjkuR4/" rel="nofollow" title="Xcode">Xcode</a></span>
<font face="Courier New">,由于我的机器本身就是用来开发</font><span style="font-family:'Times New Roman', serif;">iPhone</span>
<font face="Courier New">应用程序的,所以我的</font><span style="font-family:'Times New Roman', serif;">Xcode</span>
<font face="Courier New">是最新的版本,如果你没有</font><span style="font-family:'Times New Roman', serif;">Xcode</span>
<font face="Courier New">,可以到苹果的官网注册一个</font><span style="font-family:'Times New Roman', serif;">appleid</span>
<font face="Courier New">,然后可以免费下载最新的</font><span style="font-family:'Times New Roman', serif;">Xcode</span>
<font face="Courier New">工具和</font><span style="font-family:'Times New Roman', serif;">MacOS SDK</span>
<font face="Courier New">。</font>
<font face="Courier New">2</font>
<font face="Courier New">)从</font><span style="font-family:'Times New Roman', serif;"><a target="_blank" href="https://www.ancii.com/link/v1/mqFIwMa3Y5mqfsoOv7rzAY_FdSiczCqxh8PTeu-1lb70E9I3vApN4cUeP8Ep6nFq/" rel="nofollow" title="macports.org">macports.org</a></span>
<font face="Courier New">安装</font><span style="font-family:'Times New Roman', serif;">MacPorts</span>
<font face="Courier New">工具</font>
<font face="Courier New">安装</font><span style="font-family:'Times New Roman', serif;">MacPorts</span>
<font face="Courier New">之前需要确认环境变量</font><span style="font-family:'Times New Roman', serif;">PATH</span>
<font face="Courier New">中,</font><span style="font-family:'Times New Roman', serif;">/opt/local/bin</span>
<font face="Courier New">出现在</font><span style="font-family:'Times New Roman', serif;">/usr/bin</span>
<font face="Courier New">之前,可以运行一下这行来确认:</font>
<font face="Courier New">$echo $PATH</font>
<font face="Courier New">否则,就将如下代码加入</font><span style="font-family:'Times New Roman', serif;">~/.profile</span>
<font face="Courier New">当中。</font>
<font face="Courier New">export PATH=/opt/local/bin:$PATH</font>
<font face="Courier New">3</font>
<font face="Courier New">)从</font><span style="font-family:'Times New Roman', serif;">MacPorts</span>
<font face="Courier New">安装</font><span style="font-family:'Times New Roman', serif;">make,git, and GPG packages</span>
<font face="Courier New">:</font>
<font face="Courier New">$POSIXLY_CORRECT=1 sudo port install gmake libsdl git-core gnupg</font>
<font face="Courier New">4</font>
<font face="Courier New">)将</font><span style="font-family:'Times New Roman', serif;">gmake3.82</span>
<font face="Courier New">还原到</font><span style="font-family:'Times New Roman', serif;">3.81</span>
<font face="Courier New">gmake3.82</font>
<font face="Courier New">版本存在一个</font><span style="font-family:'Times New Roman', serif;">Bug</span>
<font face="Courier New">,所以我们要将其还原为</font><span style="font-family:'Times New Roman', serif;">3.81</span>
<font face="Courier New">版本。</font>
<font face="Courier New">$sudo vim /opt/local/etc/macports/sources.conf</font>
<font face="Courier New">然后在</font><span style="font-family:'Times New Roman', serif;">rsync</span>
<font face="Courier New">那行的上面一行加上:</font>
<font face="Courier New">file:///Users/Shared/dports</font>
<font face="Courier New">然后按照下面的步骤依次执行:</font>
<font face="Courier New">$mkdir /Users/Shared/dports</font>
<font face="Courier New">$cd /Users/Shared/dports/</font>
<font face="Courier New">$svn co --revision 50980 http://svn.macports.org/repository/macports/trunk/dports/devel/gmake/devel/gmake/</font>
<font face="Courier New">$portindex /Users/Shared/dports</font>
$ sudo port install gmake @3.81
<font face="Courier New">执行完成后,检查一下</font><span style="font-family:'Times New Roman', serif;">gmake</span>
<font face="Courier New">的版本:</font>
<font face="Courier New">$gmake –version</font>
<font face="Courier New">GNUMake 3.81</font>
5
)修改文件标识符限制
<font face="Courier New">于是最后下载</font><span style="font-family:'Times New Roman', serif;">Android</span>
<font face="Courier New">源码前的最后一步就是,修改文件标识符限制:</font><span style="font-family:'Times New Roman', serif;"><br /></span>
<font face="Courier New">由于</font><span style="font-family:'Times New Roman', serif;">MacOS</span>
<font face="Courier New">默认将同时打开的文件标识符数量限制得太小,并行编译处理时可能会超出这个限制。所以在 </font><span style="font-family:'Times New Roman', serif;">~/.profile</span>
<font face="Courier New">里加上这么一段:</font>
<font face="Courier New">#set the number of open files to be 1024</font>
<font face="Courier New">ulimit -S -n 1024</font>
<font face="Courier New">二</font><span style="font-family:'Times New Roman', serif;">.</span>
<font face="Courier New">下载</font><span style="font-family:'Times New Roman', serif;">gingerbread</span>
<font face="Courier New">源代码</font>
<font face="Courier New">Android</font>
<font face="Courier New">源代码网站</font><span style="font-family:'Times New Roman', serif;">android.git.kernel.org</span>
<font face="Courier New">已经被黑了很长一段时间,后来在网上另外找到一网站提供源代码下载。</font>
<font face="Courier New">1.</font>
<font face="Courier New">创建</font><span style="font-family:'Times New Roman', serif;">~/bin/</span>
<font face="Courier New">目录</font>
<font face="Courier New">$mkdir ~/bin</font>
<font face="Courier New">$PATH=~/bin:$PATH</font>
<font face="Courier New">2.</font>
<font face="Courier New">安装</font><span style="font-family:'Times New Roman', serif;">repo</span>
<font face="Courier New">$curl http://git-repo.googlecode.com/files/repo-1.12 > ~/bin/repo</font>
<font face="Courier New">3.</font>
<font face="Courier New">设置</font><span style="font-family:'Times New Roman', serif;">repo</span>
<font face="Courier New">可执行权限</font>
<font face="Courier New">$chmoda +x ~/bin/repo</font>
<font face="Courier New">4.</font>
<font face="Courier New">修改</font><span style="font-family:'Times New Roman', serif;">~/bin/repo</span>
<font face="Courier New">文件将第五行</font>
<font face="Courier New">REPO_URL='https://code.google.com/p/git-repo/'</font>
<font face="Courier New">改为</font>
<font face="Courier New">REPO_URL='http://code.google.com/p/git-repo/'</font>
<font face="Courier New">5.</font>
<font face="Courier New">新建目录</font><span style="font-family:'Times New Roman', serif;">gingerbread</span>
<font face="Courier New">,并进入该目录</font>
<font face="Courier New">$mkdir android_gingerbread_src</font>
<font face="Courier New">$cd android_gingerbread_src</font>
<font face="Courier New">6.</font>
<font face="Courier New">执行</font><span style="font-family:'Times New Roman', serif;">repoinit</span>
<font face="Courier New">$repo init -u git://Android.git.linaro.org/platform/manifest.git -bgingerbread</font>
<font face="Courier New">7.</font>
<font face="Courier New">找到</font><span style="font-family:'Times New Roman', serif;">android_gingerbread_src</span>
<font face="Courier New">目录中的</font><span style="font-family:'Times New Roman', serif;">.repo</span>
<font face="Courier New">下面的</font><span style="font-family:'Times New Roman', serif;">manifest.xml</span>
<font face="Courier New">文件,该文件只是一个链接,实际上是</font><span style="font-family:'Times New Roman', serif;">manifest</span>
<font face="Courier New">目录下面的</font><span style="font-family:'Times New Roman', serif;">default.xml</span>
<font face="Courier New">文件,将</font>
<span style="color:#ff0000;"><font face="Courier New">fetch="git://Android.git.kernel.org/"</font></span>
<font face="Courier New">改为</font>
<span style="color:#ff0000;"><font face="Courier New">fetch="git://Android.git.linaro.org/"</font></span>
<font face="Courier New">8.</font>
<font face="Courier New">回到</font><span style="font-family:'Times New Roman', serif;">android_gingerbread_src</span>
<font face="Courier New">目录下,执行</font><span style="font-family:'Times New Roman', serif;">reposync</span>
<font face="Courier New">$repo sync</font>
<font face="Courier New">这一步需要很长时间,喝杯茶等吧。</font>