跳转至

Android源码编译

编译Android系统的时候遇到了环境不同,稳定性不同的问题,选择docker解决问题

以编译Google Pixel 3,lineageOS 21.0为例

下载源码

第一步,安装repo

1
2
3
4
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

第二步,配置git

1
2
git config --global user.email "[email protected]"
git config --global user.name "Your Name"

由于其大小,一些存储库配置为 lfs 或大文件存储,需要安装git-lfs:

1
git lfs install # apt install git-lfs

第三步,初始化LineageOS存储库

1
repo init -u https://github.com/LineageOS/android.git -b lineage-21.0 --git-lfs

第四步,同步源码并准备,这里可以先参考清华lineageOS 源代码镜像使用帮助,先使用清华源同步,但是最后还是需要切回github同步一下,不然指定机型编译后会报错

1
2
3
4
5
repo sync

# after finished
source build/envsetup.sh
breakfast blueline

第五步,同步设备特定固件代码

方法一,先刷机再执行device/google/blueline目录下的./extract-files.sh

方法二,从OTA包中获取固件,此处可以参考 Extracting proprietary blobs from LineageOS zip files

定制镜像

Dockerfile文件如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
FROM ubuntu:22.04

# Modify the sources.list for improving download speed 
RUN sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list

# Create environment
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get -qq update
RUN apt-get -y install bc bison build-essential ccache cpio curl flex g++-multilib gcc-multilib 
RUN apt-get -y install git git-lfs gnupg gperf imagemagick libc6-dev libelf-dev libgl1-mesa-dev liblz4-tool
RUN apt-get -y install libncurses5 libncurses5-dev libsdl1.2-dev libssl-dev libx11-dev libxml2 libxml2-utils 
RUN apt-get -y install lzop lzip m4 make ncurses-dev patch pngcrush python3 python3-pip rsync schedtool 
RUN apt-get -y install squashfs-tools unzip x11proto-core-dev xsltproc zip zlib1g-dev openjdk-11-jdk
RUN ln -s /usr/bin/python3 /usr/bin/python

# Install repo
RUN curl https://storage.googleapis.com/git-repo-downloads/repo > /usr/bin/repo

# Turn on caching
ENV USE_CCACHE 1
ENV CCACHE_EXEC /usr/bin/ccache
ENV CCACHE_DIR=/ccache
RUN ccache -M 50G

# Mount source code directory
VOLUME /source
ENV WORKDIR /source
WORKDIR $WORKDIR

docker-compose.yml配置如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
version: "3"
services:
  android_builder:
    build: .
    command: /bin/bash
    tty: true
    stdin_open: true
    volumes:
      - /home/${USER}/android/lineage/:/source # SourceCode Directory
      - /home/${USER}/.ccache:/ccache # ccache directory

在执行前先配置好ccache的目录和源码目录,再使用`docker compose run --rm android_builder bash启动

整理了一个项目 dockers

执行编译

1
2
3
4
source build/envsetup.sh
breakfast blueline # 若编译user系统,则执行 breakfast blueline user,下同

brunch blueline

参考文献


最后更新: 2024-05-27
创建日期: 2024-04-19

评论