Android 源码编译之旅

前言

背景

工作需要,想着刚好有个趁手的兵器(Mac),虽然做好充足的功课,但是这场编译之旅还是十分曲折

本文主要记录本人使用macOs( 10.14) 在本地 checkout 出 AOSP ,并进行编译,导入 Android Studio 的完整经历。

安装软件

  1. XCode : 编译源码需要
  2. MacOSX-SDK (10.13) : 编译需要。本机是10.14 版本,有问题,详细见碰到的问题二,
  3. JDK : 一般都有,没有要装下
  4. MacPorts : 下载工具,用来下载编译源码需要的软件

正文

Mac 分区

为何分区:

  1. Linux 文件名大小写敏感,
  2. mac 默认文件系统大小写不敏感,编译系统会报错

如何分区:

  1. 磁盘工具(Disk Utility)-> 分区 -> 二次确认分区 -> 点击加号,选择大小和格式 -> 点击应用开始分区
  2. 新的分区在 /Volimes 目录下
  3. 因为 android-7.1.1_r6 22.12G, 编译完成占用 67.4G, 因此选择 80G
  4. 判断当前shell : echo $0 -> -bash

移动硬盘分区

  1. 购入移动硬盘,抹掉数据,改成 ExFat 格式
  2. 使用命令行进行 250G 分区
hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 250g ~/android.dmg
  1. 双击新加 android.img.sparseimage ,将其挂载

Repo 下载源码

  1. repo: 方便 checkout Android 源码这种众多 git 项目汇聚的大项目
  2. 命令
//下载repo
$ mkdir ~/bin
$ PATH=~/bin:$PATH
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo

//初始化 repo,使用清华镜像
$ repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-9.0.0_r3

// 下载源码,开启4个并发
$ repo sync -j4
  1. 也可替换成国内镜像,比如清华、中科
  2. 下载时间长,使用国内镜像还是蛮快的,应该是4,5个小时吧
  3. repo 下载报超时。repo 是对 git 的封装,给 git 设置代理
  4. 可以修改 mac 节能设置,防止休眠。系统设置 -> 节能 -> 勾选“防止进入睡眠”

编译

  1. 设置文件描述符限制
$ ulimit -S -n 1024
  1. 编译个系统浏览器 apk
// 清除缓存
make clobber
// 初始化环境
source build/envsetup.sh 
// 打包浏览器
mmm packages/apps/Browser2/

// or 自动生成依赖
mmma packages/apps/Browser2/
  1. 编译源码
//根目录下执行,环境设置命令 
source build/envsetup.sh 
// 选择编译的源码类型
lunch
// 可以选择1. aosp_arm-eng 或2. aosp_arm64-eng
1
//查看本机CPU 核数, machdep.cpu.thread_count
sysctl machdep.cpu
//编译,使用核数的两倍, 这个耗时也就,差不多6个小时
make -j16
//启动虚拟机
emulator

源码导入 Android Studio 查看

  1. 编译 idegen
source build/envsetup.sh // 将执行文件设置为临时变量
mmm development/tools/idegen/  //生成idegen.jar文件

// 结果:
[100% 5/5] Install: out/host/darwin-x86/framework/idegen.jar

#### build completed successfully (07:31 (mm:ss)) ####
  1. 执行脚本 idegen.sh
development/tools/idegen/idegen.sh

//结果
Read excludes: 83ms
Traversed tree: 1886971ms
  1. 编译完成,在AOSP 根目录下可以看到生成的 android.ipr 和 android.iml 文件
- android.ipr : 保存工程相关的设置
- android.iml : 用来描述 modules 
  1. 导入。Android Studio -> File -> Open 选择 android.ipr 文件
  2. 精简导入
// 项目 全部项目导入,一两个小时还完成不了,因此精简下
// 修改 android.iml 的excludeFolder, 只保留 frameworks 和 package 包
<excludeFolder url="file://$MODULE_DIR$/prebuilt" />
      <excludeFolder url="file://$MODULE_DIR$/.repo"/>
      <excludeFolder url="file://$MODULE_DIR$/frameworks/base/docs"/>
      <excludeFolder url="file://$MODULE_DIR$/art"/>
      <excludeFolder url="file://$MODULE_DIR$/bionic"/>
      <excludeFolder url="file://$MODULE_DIR$/bootable"/>
      <excludeFolder url="file://$MODULE_DIR$/build"/>
      <excludeFolder url="file://$MODULE_DIR$/compatibility"/>
      <excludeFolder url="file://$MODULE_DIR$/cts"/>
      <excludeFolder url="file://$MODULE_DIR$/dalvik"/>
      <excludeFolder url="file://$MODULE_DIR$/developers"/>
      <excludeFolder url="file://$MODULE_DIR$/development"/>
      <excludeFolder url="file://$MODULE_DIR$/device"/>
      <excludeFolder url="file://$MODULE_DIR$/external"/>
      <excludeFolder url="file://$MODULE_DIR$/hardware"/>
      <excludeFolder url="file://$MODULE_DIR$/kernel"/>
      <excludeFolder url="file://$MODULE_DIR$/libcore"/>
      <excludeFolder url="file://$MODULE_DIR$/libnativehelper"/>
      <excludeFolder url="file://$MODULE_DIR$/ndk"/>
      <excludeFolder url="file://$MODULE_DIR$/out"/>
      <excludeFolder url="file://$MODULE_DIR$/pdk"/>
      <excludeFolder url="file://$MODULE_DIR$/platform_testing"/>
      <excludeFolder url="file://$MODULE_DIR$/prebuilts"/>
      <excludeFolder url="file://$MODULE_DIR$/projectFilesBackup"/>
      <excludeFolder url="file://$MODULE_DIR$/sdk"/>
      <excludeFolder url="file://$MODULE_DIR$/system"/>
      <excludeFolder url="file://$MODULE_DIR$/test"/>
      <excludeFolder url="file://$MODULE_DIR$/toolchain"/>
      <excludeFolder url="file://$MODULE_DIR$/tools"/>

碰到的问题

Could not find a supported mac sdk

问题:
本机 mac 10.14.3, 执行编译报 "Could not find a supported mac sdk: [“10.10” “10.11” “10.12” “10.13”] "

网上的解决之道:
x86_darwin_host.go 文件新增 "10.14"系统,让其支持

找到
build/soong/cc/config/x86_darwin_host.go
修改
darwinSupportedSdkVersions = []string{
        "10.10",
        "10.11",
        "10.12",
        "10.13",
        "10.14",

后遗症:
这么改完后,不报这个问题了,但是问题转移了,成为下面那个问题了。

Undefined symbols for architecture i386

本机 mac 10.14, 其实是不支持 i386 架构,强行修改不行,搜索千百度,终于在 Google 论坛的 Android Building 组里面看到一些有用信息

I have solved this issue.You can download MacOSX10.13.sdk on https://github.com/phracker/MacOSX-SDKs/releases and copy it to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs

简述之:

  1. https://github.com/phracker/MacOSX-SDKs/releases 下载 MacOsx 10.13 sdk
  2. 将该sdk 拷贝到 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs, 我这里的 Xcode.app 是 Xcode-beta
  3. x86_darwin_host.go 文件新增 "10.14"系统去掉,让他使用 10.13 编译

结语

整个工程耗时耗力,也算是自己的一场磨难。

致谢

  1. macOS(Sierra 10.12)上Android源码(AOSP)的下载、编译与导入到Android Studio (最详细)
  2. Android源码:2、下载详解(二)(支持移动硬盘、mac)—亲测成功 (移动硬盘分区)
  3. 自己动手编译Android源码(超详细) (模块编译)
  4. 搭建编译环境 (android 官方文档)
  5. Getting the following error while building aosp using mmm command. (解决困扰我许久的 i386 问题)

猜你喜欢

转载自www.cnblogs.com/haichao/p/10583900.html