前言
Git管理单独的模块,而repo管理所有的Git库,将多个Git库组合在一起,形成一个完整的系统 使用 repo init 获取 manifests 和 repo 库,使用 repo sync 获取 manifests 中的模块 如果报错可运行 repo sync –trace 获取出错的那几个文件属于哪个模块 然后到 .repo/manifest/default.xml 中将对应的模块删掉就好了,但可能编译错误
配置环境
安装编译所需依赖
1 2 3 4 5 6 sudo apt install git curl gnupg zip unzip ccache xsltproc gettext repo \gcc-multilib g++-multilib make flex m4 bc bison gperf dosfstools mtools \ libelf-dev libssl-dev libncurses5 libxml2-utils zlib1g-dev openjdk-8-jdk \ x11proto-core-dev libx11-dev libgl1-mesa-dev python-enum34 python-mako \ syslinux-utils lib32stdc++6 libc6-dev-i386 lib32ncurses-dev lib32z-dev
若找不到repo包请添加contrib源或直接从谷歌下载
1 2 3 4 5 6 7 8 9 10 11 sudo curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > /usr/local/bin/reposudo chmod a+x /usr/local/bin/reposudo apt install python3sudo rm /usr/bin/pythonsudo ln -s /usr/bin/python3 /usr/bin/pythonexport REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
从其他源安装OpenJDK
1 2 3 4 5 6 7 8 wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add - echo "deb http://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/deb buster main" >> /etc/apt/sources.listapt update && apt install adoptopenjdk-8-hotspot
获取源码
设置变量
1 2 3 4 git config --global user.email "username@email.com" git config --global user.name "username"
拉取仓库
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 mkdir android-source && cd android-sourcerepo init -u http://scm.osdn.net/gitroot/android-x86/manifest \ -b pie-x86 -m android-x86-9.0-r2.xml \ --repo-url=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/ sed -i 's|android.googlesource.com|mirrors.tuna.tsinghua.edu.cn/git/AOSP|g' \ .repo/manifests/android-x86-9.0-r2.xml -u -m -b --depth=1 --mirror --repo-url --repo-branch --no-repo-verify https://mirrors.ustc.edu.cn/aosp https://mirrors.tuna.tsinghua.edu.cn/git/AOSP https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/
同步源码
1 2 3 4 5 6 7 8 9 10 11 12 13 repo sync -j4 --no-tags --no-clone-bundle --current-branch --no-tags --no-clone-bundle --current-branch --force-sync wget -c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar tar -xvf aosp-latest.tar && cd aosp repo sync -l
编译源码
编译目标系统
1 2 3 4 5 cd android-sourcesource build/envsetup.sh lunch android_x86_64-userdebug m -j12 iso_img
编译后的文件
1 2 3 4 内核将在 out/target/product/x86_64/kernel 模块放在 out/target/product/x86_64/system/lib/modules/ 镜像文件 out/target/product/x86_64/android_x86_64.iso
指定配置文件
1 2 3 4 make iso_img TARGET_PRODUCT=android_x86_64
编译后的清理
1 2 3 4 1. 在源码目录的根目录下 make clean 2. 进到源码的 kernel/ 目录下,执行 make mrproper 3. 再退回到根目录下执行 source build/envsetup.sh ; lunch ; make
内核定制
自定义内核配置
1 2 3 4 5 6 7 8 9 10 11 12 cd android-sourcesource build/envsetup.shlunch android_x86_64-userdebug cp kernel/arch/x86/configs/android-x86_64_defconfig kernel/.configcd kernelmake O=kernel/ ARCH=x86_64 menuconfig
仅构建内核用于其他的Android X86系统
1 2 3 4 5 6 7 8 9 source build/envsetup.shlunch android_x86_64-userdebug make kernel
构建包含定制内核的镜像
1 2 3 4 5 6 7 8 9 10 11 12 1. 如你已有合适的内核配置文件my_defconfig,可使用你的配置文件来构建包含定制内核的镜像ISO 2. 不能直接使用Linux发行版的内核配置来定制,因为Android需要内核开启特定的功能支持 3. 在kernel/configs/p/android-4.14/android-base.cfg中包含了Android P所需内核支持的选项,但是它删除了android-x86的arm特定选项如PMEM 3. 如你已有合适的内核配置文件,可以直接指定配置文件路径来构建ISO make iso_img TARGET_PRODUCT=android_x86_64 TARGET_KERNEL_CONFIG=kernel/arch/x86/configs/my_defconfig 5. 如你已有经编译好的内核,可以直接指定内核位置来使用它生成ISO make iso_img TARGET_PRODUCT=android_x86_64 TARGET_PREBUILT_KERNEL=out/target/product/x86_64/kernel
使用其他版本内核构建系统
1 2 3 4 5 6 cd android-source/kernelgit fetch x86 kernel-4.9-p git checkout -t x86/kernel-4.9-p cd ..
注意事项
Win10中WSL环境下编译会通不过,envsetup.sh脚本目前仅支持bash或zsh,其他自测。
编译Android 710使用JDK8,56使用JDK7,2.34.4使用JDK6,1.52.2使用JDK5
使用12线程编译ISO历时3个小时左右,16G内存最高时占用12G,需要的磁盘空间超过150G
使用CCACHE来提高编译速度
1 2 3 4 5 6 7 export USE_CCACHE=1 export CCACHE_DIR=~/.ccache prebuilts/misc/linux-x86/ccache/ccache -M 50G watch -n1 -d prebuilts/misc/linux-x86/ccache/ccache -s
获取源码时的错误解决
1 2 3 4 5 6 7 8 9 10 错误:error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with unexpected length was received. 解决:git config -l git config --global http.sslVerify false git config --global http.postBuffer 1048576000 git config --global core.compression -1 错误:fatal: unable to access 'https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/packages/apps/StorageManager/' : Failed to connect to mirrors.tuna.tsinghua.edu.cn port 443: Connection refused 把这:fetch="https://aosp.tuna.tsinghua.edu.cn/" 换成:fetch="https://mirrors.ustc.edu.cn/aosp/"
编译系统时的错误解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14 export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m" prebuilts/sdk/tools/jack-admin kill-server prebuilts/sdk/tools/jack-admin start-server 解决:vi android-source/bootable/newinstaller/Android.mk 找到:-e boot/grub/efi.img 改为:-eltorito-boot boot/grub/efi.img 解决:修改grub引导添加 nomodeset xforcevesa
编译Anbox镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 cd anboxrepo init -u https://github.com/anbox/platform_manifests.git -b anbox \ --repo-url=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/ --depth=1 repo sync -j4 -c --no-tags --no-clone-bundle --force-sync source build/envsetup.shlunch anbox_x86_64-userdebug make -j8 cd anbox/vendor/anboxscripts/create-package.sh \ out/target/product/x86_64/ramdisk.img \ out/target/product/x86_64/system.img