0 备注

  • 源码40G左右
  • 编译完152G左右
  • 编译时间:60分钟(24核),30分钟(48核心)
    由于步骤较多,建议紧跟右侧目录

1 环境

需要 python2+jdk

apt-get update 

# 先安装 apt-fast
add-apt-repository ppa:apt-fast/stable
apt-get update
apt-get -y install apt-fast

# 升级一下系统组件
# 不同的安卓版本对java的版本需求也是不一样的

# 最新版本的jabva
apt install -y default-jdk
# 使用比较广泛的 java8
apt install -y openjdk-8-jdk-headless

# 其他组件
apt-fast install -y python  make git curl

# 其他依赖 lineageos 提供
apt-fast install -y \
bc bison build-essential ccache \
curl flex g++-multilib gcc-multilib \
git gnupg gperf imagemagick lib32ncurses5-dev \
lib32readline-dev lib32z1-dev liblz4-tool \
libncurses5-dev libsdl1.2-dev libssl-dev \
libwxgtk3.0-dev libxml2 libxml2-utils lzop \
pngcrush rsync schedtool squashfs-tools \
xsltproc zip zlib1g-dev

2 配置repo

curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > /bin/repo
chmod a+x /bin/repo
git config --global user.email "you@example.com"
git config --global user.name "Your Name"

3 选择分支

# 查看所有分支
https://android.googlesource.com/platform/manifest/+refs

mkdir src
cd src

# 指定分支版本
repo init -u https://android.googlesource.com/platform/manifest -b android-8.1.0_r68
# 清华大学加速节点
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-8.1.0_r68

4 同步分支

repo sync -j16  --force-sync --no-clone-bundle

5 设置环境变量

source build/envsetup.sh

6 清理上一次的编译输出

make clobber

7 选择编译的版本

  • 无论是第三方rom还是aosp官方的源码,都没有集成设备的驱动
  • 编译rom之前需要自己去寻找 设备需要的驱动
  • 驱动一般由 设备官方提供或者由 第三方开发者提供

7.1 下载驱动(可选)

  • 谷歌的设备是有官方驱动的,直接下载即可
  • 下载驱动

解压后在根目录 运行脚本

bash extract-qcom-bullhead.sh

7.2 选择分支

命令 demo

# 指定编译版本
# 编译为debug
lunch aosp_x86_64-eng   
# 编译公式
lunch aosp_设备代号-目标类型   
# 查看配置demo
lunch

google官方配置表

设备代号编译配置
Pixel XLmarlinaosp_marlin-userdebug
Pixelsailfishaosp_sailfish-userdebug
HiKeyhikeyhikey-userdebug
Nexus 6Pangleraosp_angler-userdebug
Nexus 5Xbullheadaosp_bullhead-userdebug
Nexus 6shamuaosp_shamu-userdebug
Nexus Playerfuguaosp_fugu-userdebug
Nexus 9volantis (flounder)aosp_flounder-userdebug
Nexus 5 (GSM/LTE)hammerheadaosp_hammerhead-userdebug
Nexus 7 (WLAN)razor (flo)aosp_flo-userdebug
Nexus 7(移动版)razorg (deb)aosp_deb-userdebug
Nexus 10mantaray (manta)full_manta-userdebug
Nexus 4occam (mako)full_mako-userdebug
Nexus 7 (WLAN)nakasi (grouper)full_grouper-userdebug
Nexus 7(移动版)nakasig (tilapia)full_tilapia-userdebug
Galaxy Nexus (GSM/HSPA+)yakju (maguro)full_maguro-userdebug
Galaxy Nexus (Verizon)mysid (toro)aosp_toro-userdebug
Galaxy Nexus(试验版)mysidspr (toroplus)aosp_toroplus-userdebug
Motorola Xoom(美国 WLAN 版)wingrayfull_wingray-userdebug
Nexus Ssoju (crespo)full_crespo-userdebug
Nexus S 4Gsojus (crespo4g)full_crespo4g-userdebug
编译类型使用情况
user权限受限;适用于生产环境
userdebug与“user”类似,但具有 root 权限和调试功能;是进行调试时的首选编译类型
eng具有额外调试工具的开发配置

然后会生成如下环境变量

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=8.1.0
TARGET_PRODUCT=aosp_x86_64
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_PLATFORM_VERSION=OPM1
TARGET_BUILD_APPS=
TARGET_ARCH=x86_64
TARGET_ARCH_VARIANT=x86_64
TARGET_CPU_VARIANT=
TARGET_2ND_ARCH=x86
TARGET_2ND_ARCH_VARIANT=x86_64
TARGET_2ND_CPU_VARIANT=
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.0-52-generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=OPM6.171019.030.B1
OUT_DIR=out
AUX_OS_VARIANT_LIST=
============================================

选择分之后建议重新同步一下

8 编译

48表示线程数量,根据自己的实际情况进行修改

make -j 48

其他编译选项

##制作系统专用的 sdk 工具包
make sdk  

##制作系统专用的 ndk 工具包
make ndk  

##重新打包
make snod 

##编译所有的模块 
m
##编译当前目录下的模块,当前目录下要有Android.mk文件 
mm
##编译指定路径下的模块,指定路径下要有Android.mk文件 
mmm

9 CCACHE加速编译

  • 使用 ccache 可以加速编译,代价是使用更多的磁盘
  • 建议ssd打开该选项
export USE_CCACHE=1
ccache -M 50G

# 清理所有缓存
# ccache -C

10 提取镜像

  1. 编译完成后整个 aosp源码目录大小为 150G左右
  2. 编译后的 文件在 $OUT(out/target/product/)目录下
  3. 主要是提取 *.imgkernel文件
//tar zcf aosp.tar.gz *.img kernel-ranchu
kernel-ranchu  
cache.img  
ramdisk.img  
system.img   
system-qemu.img  
vendor.img
vendor-qemu.img
encryptionkey.img  
userdata.img

11 刷真机

  1. 这一步主要是给大家介绍如何刷机
  2. 谷歌提供的adb驱动中包含了 fastboot组件,这个组件就是负责刷机的

进入 fb

adb reboot bootloader

解锁

# 2015年开始的设备 (Nexus 5X, Nexus 6P, Pixel, Pixel XL, Pixel 2 or Pixel 2 XL device)
fastboot flashing unlock

# 2015年之前的设备
fastboot oem unlock

# 针对 Pixel 2 XL 的额外操作
fastboot flashing unlock_critical

刷机

## 完整的镜相包
bash flash-all

## 自定义包,-w 表示清除数据
## 使用 环境变量`$ANDROID_PRODUCT_OUT`作为 镜像目录
## 需要提取所有 img文件,kernel文件,以及 android-info.txt 文件
fastboot -w flashall

手动刷

fastboot flash bootloader bootloader-xxx-xxx.img
fastboot flash radio radio-xxx-xxx-xxx.img
fastboot reboot-bootloader
fastboot flash recovery recovery.img
fastboot flash boot boot.img
fastboot flash system system.img
fastboot flash vendor vendor.img

## 清除数据
fastboot flash cache cache.img
fastboot flash userdata userdata.img

12 internal problem错误

  1. **There's an internal problem with your device. Contact your manufacturer for details.**
  2. 出现这个错误是因为 系统镜像中的 驱动版本号 和 真正使用 驱动版本号不一样

    vendor/build.prop <--> /system/build.prop

两种解决方案:

方案1:

修改源文件

/frameworks/base/core/java/android/os/Build.java.

定位方法:

public static boolean isBuildConsistent()

永远返回真

return true;

方案2:

adb sideload xxx.zip

13 模拟器启动 镜像

  1. 老版本的 emulator可以直接指定 镜像位置来指定
  2. 新版本的 emulator必须基于 avd配置来操作

13.1 旧版 emulator

全参数

老版本模拟器需要 指定 所有镜像的 绝对路径

emulator -sysdir out/target/product/generic/ -system out/target/product/generic/system.img -ramdisk out/target/product/generic/ramdisk.img -data out/target/product/generic/userdata.img -kernel prebuilt/android-arm/kernel/kernel-qemu -sdcard sdcard.img -skindir sdk/emulator/skins -skin WVGA800 -scale 0.7 -memory 512 -wipe-data -partition-size 200

13.2 新版 emulator

13.3 推荐

打包整个编译出来的目录

设置环境变量指向编译出的 目录

set ANDROID_PRODUCT_OUT=C:\usr\Android\sdk\system-images\android-16\default\armeabi-v7a

直接运行模拟器,自动设置路径

emulator

13.4 不推荐

  1. 首先利用 android studio创建架构和 自定义 rom一样的虚拟机
  2. 打开虚拟机文件夹将 自定义虚拟机的 文件全部复制进去
  3. 只需要覆盖 *.imgkernel文件即可
  4. 正常启动虚拟机

14 错误修复

14.1 语言环境错误

出现这类问题说明控制台环境与编译器冲突

Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed.

解决办法:
make前用下面这行代码 统一环境

export LC_ALL=C

14.2 内存过小

出现这类错误在设置

Out of memory error (version 1.2-rc4 'Carnac' (298900 f95d7bdecfceb327f9d201a1348397ed8a843843 by android-jack-team@google.com)).
GC overhead limit exceeded.

方案1:设置环境变量

export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4G"
out/host/linux-x86/bin/jack-admin kill-server
out/host/linux-x86/bin/jack-admin start-server

方案2:修改文件

/prebuilts/sdk/tools/jack-admin

关键字段:JACK_SERVER_COMMAND

14.3 fingerprint 错误

详细错误信息 fingerprint cannot exceed 91 bytes

编辑文件 build/tools/post_process_props.py. Change lines as follows:

//原始
PROP_VALUE_MAX = 91
//修改为
PROP_VALUE_MAX = 128

编辑文件 bionic/libc/include/sys/system_properties.h. Change lines as follows:

//原始
define PROP_VALUE_MAX   32
//修改为
define PROP_VALUE_MAX  128

Do

make clean
make

14.4 installd.cpp:43:1

错误

installd.cpp:43:1: error: static_assert failed "Size mismatch."

修改文件 frameworks/native/cmds/installd/installd_deps.h

//原始
constexpr size_t kPropertyValueMax = 92u;
//修改为
constexpr size_t kPropertyValueMax = 128u;
Last modification:November 19, 2020
如果觉得我的文章对你有用,请随意赞赏