bobbai 发表于 2017-7-6 07:25:39

Mac下载并编译Google安卓AOSP项目代码

Mac下载并编译Google安卓AOSP项目代码
  参考 https://source.android.com/source/index.html
  这两天用Mac下载安卓AOSP源码,且把遇到的问题记下来。当然作为一个菜鸟,难免会有错误或者描述不对的地方,欢迎各路大神小神批评指正。转载请注明出处http://www.cnblogs.com/ryanchi/p/5682186.html。

一、准备环境
  (请提前安装好xcode或command line tools)


[*]  Installing the JDK
  
The master branch of Android in the Android Open Source Project (AOSP) requires Java 8

[*]  Creating a case-sensitive disk image
  
执行下面命令,将会在/Users/当前用户/目录下创建android.dmg文件,当然也可以自己定义文件位置。
  
$ hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g ~/android.dmg
  
This will create a .dmg (or possibly a .dmg.sparseimage) file.

  官方文档推荐40GB,后来发现完全不够用,这里我直接上100g,并且文件位置直接放在桌面,下次开机时直接双击打开,虽然MBP经常不关机。。。
  
$ hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 100g ~/Desktop/android.dmg



[*]  如果你镜像创建完毕,想改变镜像大小,可以执行下面代码:
  
$ hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage

[*]  你可以为bash添加function,以便快速挂载和卸载android.dmg文件。但如果和我一样偷懒镜像文件直接放桌面,则可以省略这一步,直接从桌面装载镜像就行。
  

# mount the android file image  
function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; }
  
# unmount the android file image
  
function umountAndroid() { hdiutil detach /Volumes/android; }


二、Installing required packages


[*]  Install MacPorts from macports.org
  
直接下载MacPorts_xxx.pkg文件安装即可(xxx为版本号)。

  Note: Make sure that /opt/local/bin appears in your path before /usr/bin。可以通过$ echo $PATH命令查看。如果不是,在~/.bash_profile文件中添加下面代码,添加完毕后直接执行$ source ~/.bash_profile命令。
  

export PATH=/opt/local/bin:$PATH      



[*]我的情况是MacPorts_xxx.pkg安装完毕后,其自动在~/.bash_profile文件里添加了export PATH=&quot;/opt/local/bin:/opt/local/sbin:$PATH&quot;

[*]  Get make, git, and GPG packages from MacPorts:
  
$ POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg

  报错:
  

Error: Port gmake not found  

  执行$ sudo port -d sync即可。
  
若执行上述命令继续报如下错误:
  

receiving file list ... rsync: read error: Operation timed out (60)  
Error: Synchronization of the local ports tree failed doing rsync
  
port sync failed: Synchronization of 1 source(s) failed
  

  解决办法:
  
修改 /opt/local/etc/macports/sources.conf,将原来最后一行修改成如下,然后执行$ sudo prot -v selfupdate即可。
  

#rsync://rsync.macports.org/release/tarballs/ports.tar   
https://distfiles.macports.org/ports.tar.gz
  


三、下载源码


[*]  Installing Repo
  

$ mkdir ~/bin       # 在当前用户目录新建bin\文件夹  
$ PATH=~/bin:$PATH      # 将bin目录添加到PATH
  

  
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
  
$ chmod a+x ~/bin/repo

[*]Downloading the Android Source Tree  
考虑到国内网络问题,这一步骤可以参考清华大学镜像资源站https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/的教程,写的很详细。


[*]下载AOSP月更新的初始化包,https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar。
[*]解压。将下载得到的aosp-latest.tar压缩文件直接拷贝到前面创建的xxx.dmg镜像文件装载的磁盘目录,执行$ tar xf aosp-latest.tar,稍等解压完毕后,当前目录会出现一个AOSP文件夹,该文件夹下有一个隐藏文件夹.repo。
[*]  同步代码树。执行$ repo sync。具体可以参考下面步骤:
  

wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包  
tar xf aosp-latest.tar
  
cd AOSP   # 解压得到的 AOSP 工程目录
  
# 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录
  
repo sync # 正常同步一遍即可得到完整目录
  
# 或 repo sync -l 仅checkout代码


四、Build


[*]  Clean up
  
To ensure the newly installed binaries are properly taken into account after being extracted, delete the existing output of any previous build using:
  

$ make clobber  

  报错
  

Error: could not find jdk tools.jar at /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/../lib/tools.jar, please check if your JDK was installed correctly.Stop.  

  解决办法:参考关于完整Android源码的阅读在~/.bash_profile文件中指定ANDROID_JAVA_HOME为JDK路径即可。
  

export ANDROID_JAVA_HOME=${JAVA_HOME}  


[*]  Set up environment
  

$ source build/envsetup.sh
[*]  Choose a target
  

$ lunch aosp_arm-eng  

Buildtype
Use
user
limited access; suited for production
userdebug
like &quot;user&quot; but with root access and debuggability; preferred for debugging
eng
development configuration with additional debugging tools
可选的有aosp_arm-eng、 aosp_arm64-eng、aosp_mips-eng、aosp_mips64-eng、aosp_x86-eng......
[*]  Build the code
  

$ make -jN  

  N为数字,一般为cpu线程数1到2倍。我的本本是mbp2015年中,使用$ make -j8,很多warning,不管即可,用了一个多小时,最后输出结果#### make completed successfully (01:06:05 (hh:mm:ss)) ####

[*]  Emulate an Android Device
  
运行模拟器
  

$ emulator
五、Building Kernels
  到了这里,略坑。由于需要克隆谷歌git仓库到本地,所以配置完git的http代理以后,到AOSP项目中,cd进入kernel文件夹,直接
  

$ git clone https://android.googlesource.com/kernel/goldfish.git  

  等了一晚,结果如下:
  

remote: Sending approximately 982.87 MiB ...  
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
  
fatal: The remote end hung up unexpectedly
  
fatal: 过早的文件结束符(EOF)
  
fatal: index-pack failed
  

  由于使用的梯子不给力,试了好几次,都是clone了一部分后直接挂掉。。。所以现在正琢磨自己搭建ss服务器,同时也希望有好的梯子童鞋可以私聊推荐一下。内核这部分内容可以参考谷歌https://source.android.com/source/building-kernels.html或者罗升阳老师的博客 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)

THE END!
页: [1]
查看完整版本: Mac下载并编译Google安卓AOSP项目代码