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

  1. 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="/opt/local/bin:/opt/local/sbin:$PATH"`
  1. 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 [default]
	https://distfiles.macports.org/ports.tar.gz [default]

三、下載源碼

  • 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/的教程,寫的很詳細。

    1. 下載AOSP月更新的初始化包,https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar

    2. 解壓。將下載得到的aosp-latest.tar壓縮文件直接拷貝到前面創建的xxx.dmg鏡像文件裝載的磁盤目錄,執行$ tar xf aosp-latest.tar,稍等解壓完畢后,當前目錄會出現一個AOSP文件夾,該文件夾下有一個隱藏文件夾.repo

    3. 同步代碼樹。執行$ 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

  1. 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}
    
  2. Set up environment

     $ source build/envsetup.sh
    
  3. Choose a target

     $ lunch aosp_arm-eng
    
    ildtype Use
    er limited access; suited for production
    erdebug like "user" but with root access and debuggability; preferred for debugging
    g development configuration with additional debugging tools

    可選的有aosp_arm-eng、 aosp_arm64-eng、aosp_mips-eng、aosp_mips64-eng、aosp_x86-eng......

  4. Build the code

     $ make -jN
    

    N為數字,一般為cpu線程數1到2倍。我的本本是mbp2015年中,使用$ make -j8,很多warning,不管即可,用了一個多小時,最后輸出結果#### make completed successfully (01:06:05 (hh:mm:ss)) ####

  5. 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!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM