Android源码编译、下载、导入Android Studio过程记录(MacOS 10.12)

本文详细介绍如何在MacOS环境下搭建适合编译Android 6.0源码的开发环境,包括创建大小写敏感的磁盘分区、安装必要的软件、下载源码、编译过程中的兼容性处理等步骤。

1. 摘要

Google官方的Android源码下载及编译文档,请参考https://source.android.com/source/initializing.html[1]

本文主要参考了《Android源码到下载、编译与导入到Android Studio》[2],MacOS 10.11可以直接去看它,也可以借鉴本篇的一些其它参考资料。

除此之外,遇到的其它问题,主要参考《Mac上下载编译Android 6.0源码详细记录》[3]



2. 环境配置

参考setting-up-a-mac-os-x-build-environment[4]


2.1 创建分区

Mac默认使用的文件系统是大小写不敏感的,这在编译Android源码的过程中,会出现一些问题。所以需要创建一块大小写敏感的磁盘分区。

可以使用Disk Utility(磁盘工具)进行分区,选择格式是:Mac OS Extended (Case-sensitive, Journaled) ,即Mac OS X扩展(区分大小写,日志式),建议大小为80G(下载源码需要36G,编译完后一共占用75G)。


Google官方推荐的命令行:

$ hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 80g ~/android.dmg

该命令会产生这两个文件:~/android.img和~/android.img.sparseimage


如果在下载编译过程中需要将分区扩容,可以使用如下命令:

$ hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage

2.2 安装jdk1.7

Oracle官网已不提供jdk1.7的下载,百度搜索后,在csdn中发现jdk-7u71-macosx-x64.dmg,经验证可用。


为了方便jdk的切换,可以在~/.bash_profile文件中做了如下配置:

export JAVA_6_HOME=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
export JAVA_7_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
export JAVA_8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home

alias jdk8='export JAVA_HOME=$JAVA_8_HOME'
alias jdk7='export JAVA_HOME=$JAVA_7_HOME'
alias jdk6='export JAVA_HOME=$JAVA_6_HOME'
export JAVA_HOME=$JAVA_8_HOME
这样,只需要在shell中敲入jdk6、jdk7、jdk8即可切换当前shell窗口的jdk环境


2.3 安装其它软件

需要安装MacPorts,安装完成之后,调用下面的命令:

$ POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg


除此之外,还需要安装Xcode。因为电脑本身已安装有Xocde,Version 8.1 (8B62),没遇到相关问题。

3. 源码下载

3.1 下载Repo

新建目录,并确保新建目录在$PATH下:

$ mkdir ~/bin
$ PATH=~/bin:$PATH


下载Repo工具,并设置其为可执行文件:

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

 

未翻墙用户,可使用 清华大学Tuna镜像源[5]。使用方法为:

编辑~/bin/repo文件,更改REPO_URL为:

REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
或者直接在~/.bash_profile环境下加入:

export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'


3.2 下载源码

现在,进入到我们创建的大小写敏感分区,创建一个文件夹作为源码的根文件,执行repo init:

$ repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-6.0.1_r17
完成之后,调用下面的命令来下载源码:

$ repo sync


为了避免链接断开时,需要手动输入repo sync,可使用以下shell脚本:

#!/bin/bash 
   #FileName  get_android.sh
   PATH=~/bin:$PATH 
   repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-6.0.1_r17
   repo sync 
   while [ $? = 1 ]; do 
       echo "================sync failed, re-sync again =====" 
       sleep 3 
       repo sync 
   done

将上面的脚本保存成get_android.sh,放在下载源码的根目录下,输入下面命令,即可开始下载:

$ chmod a+x ./get_android.sh && ./get_android.sh
完成后可以删除.repo/文件夹及其文件:

$ rm -rf .repo/



4. 源码编译

4.1 兼容处理

打开build/core/combo/mac_version.mk文件,在mac_sdk_versions_supported变量后添加版本号10.11:

mac_sdk_versions_supported :=  10.6 10.7 10.8 10.9 10.11


注意:重要的事情说三遍,一定是10.11,一定是10.11,一定是10.11。虽然,我的Mac OS是10.12。


4.2 MacOS SDK下载

下载地址:https://github.com/phracker/MacOSX-SDKs[6]

通过git clone把它们下载到/Applications/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/目录下:(至少需要把MacOSX10.11.sdk下载到该目录下)

为了方便大家使用,我把MacOSX10.11.sdk.tar.xz上传到csdn下载资源了。

下载后,可以使用如下命令完成操作:(假设MacOSX10.11.sdk.tar.xz下载到~/Downloadws/目录下)

$ sudo cp ~/Downloads/MacOSX10.11.sdk.tar.xz /Applications/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
$ sudo tar xvzf /Applications/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk.tar.xz
$ sudo rm /Applications/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk.tar.xz

4.3 编译

在源码根目录输入命令:

$ source build/envsetup.sh
选择设备:

$ lunch 


我选择了6. aosp_x86_64-eng。


具体选择含义,可参考Google官网提供的这个表格:

BuildtypeUse
userlimited access; suited for production
userdebuglike "user" but with root access and debuggability; preferred for debugging
engdevelopment configuration with additional debugging tools






编译源码

$ make -j4

运行模拟器

$ emulator



5. 导入Android Studio

5.1 编译idegen模块

(这一步也可以直接去网上下载idegen.jar并将其复制到out/host/darwin-x86/framework/目录下)

$ mmm development/tools/idegen/

5.2 生成android.ipr和android.iml

$ development/tools/idegen/idegen.sh


5.3 导入

使用Android Studio,File->Open,打开根目录下的android.ipr文件夹,等待index完成

在这一过程中,遇到一些奇怪问题,可尝试这样解决:File->Invalidate Caches / Restart...

源码与项目的关联在Eclipse下叫Link with Editor,而在Android Studio中为AutoScroll from Source和AutoScroll to Source



6. 参考资料

[1] https://source.android.com/source/initializing.html

[2] 《Android源码到下载、编译与导入到Android Studio》

[3] 《Mac上下载编译Android 6.0源码详细记录》

[4] setting-up-a-mac-os-x-build-environment

[5] 清华大学Tuna镜像源

[6] https://github.com/phracker/MacOSX-SDKs


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值