nexus 5 + kitcat aosp + kernel 编译 2019-01-21 10:02:05 Steven Xeldax 安卓系统编译的过程是极其复杂繁琐,故该过程记录如下: ## AOSP 完整编译(无kernel编译,driver闭源) ### 前期的准备 由于安卓编译特别需要消耗内存和cpu资源所以工作站最好达到一下的需求: 1. 高版本需要使用 64 位linux环境。如果是较低的版本,则可以在 32 位系统中进行编译。 2. 如果要检出代码,至少需要 250GB 可用磁盘空间;如果要进行编译,则还需要 150GB。如果要进行多次编译,则需要更多空间 3. 物理机内存最好8g以上,如果在虚拟机中运行 Linux,则至少需要 16GB 的 RAM/交换空间。 此外软件需求为: 1. 主要的软件包,如python,gun make,gcc等,具体会在搭建环境中说明。 2. 设备二进制,由于安卓是开放项目,大部分芯片厂商的驱动是闭源的,所以需要了解自己的手机型号,下载对应的Blob。 3. 编译工具包 操作系统需求为: GNU/Linux Android 6.0 (Marshmallow) - AOSP master:Ubuntu 14.04 (Trusty) Android 2.3.x (Gingerbread) - Android 5.x (Lollipop):Ubuntu 12.04 (Precise) Android 1.5 (Cupcake) - Android 2.2.x (Froyo):Ubuntu 10.04 (Lucid) 事实上android 4.4以上大部分只要用ubuntu 14.04或14.04以上就足够了。 ### 搭建环境 安装依赖,以下可能有重复的包,继续安装即可。 ``` sudo apt-get update sudo apt-get install openjdk-8-jdk sudo apt-get install git gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386 sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev libgl1-mesa-dev libxml2-utils xsltproc unzip sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386 sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386 sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev sudo apt-get install git-core gnupg flex bison gperf build-essential sudo apt-get install zip curl zlib1g-dev gcc-multilib g++-multilib sudo apt-get install libc6-dev-i386 sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev sudo apt-get install lib32z-dev ccache sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4 ``` ### 下载源代码 #### 准备 Repo 1. 确保主目录下有一个 bin/ 目录,并且该目录包含在路径中: ``` mkdir ~/bin PATH=~/bin:$PATH ``` 2. 下载 Repo 工具,并确保它可执行: ``` curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo chmod a+x ~/bin/repo ``` #### 使用Repo > repo init -u https://android.googlesource.com/platform/manifest > repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1 同步代码 > repo sync 需要设备ss代理 ``` export HTTP_PROXY=http://<proxy_user_id>:<proxy_password>@<proxy_server>:<proxy_port> export HTTPS_PROXY=http://<proxy_user_id>:<proxy_password>@<proxy_server>:<proxy_port> ``` 准备二进制: https://developers.google.com/android/drivers eg ``` Nexus 5 (GSM/LTE) binaries for Android 4.4 (KRT16M) Hardware Component Company Download SHA-256 Checksum NFC, Bluetooth, Wi-Fi Broadcom Link f9c2fd83bbedbe522d562737a2d9e7a4a3acb8fee6b47458b60da381ac73f958 Camera, Sensors, Audio LG Link 5ce50d1c03b6a6aeefb433e181ceca1eb16528e3ed7560eb9311ec5696007074 Graphics, GSM, Camera, GPS, Sensors, Media, DSP, USB Qualcomm Link 7051a80ec58cf4d626048c333b6a0f12d1db82194fab3850acc9cd6c091ecb8e ``` 例如hammerhead,只要把下载好的文件放到项目目录,然后运行解压就行,程序会自动将驱动文件释放到vendor中。 ### 开始编译 设置环境 ``` source build/envsetup.sh ``` 选择目标 ``` lunch aosp_arm-eng ``` eg > lunch aosp_hammerhead-userdebug ``` make -j4 ``` ### 刷机 成功后img在out/target/product/xxxx/下 adb reboot bootloader fastboot -w flashall 整个过程中没有涉及到内核编译,以下为kernel编译的过程。 ## android 内核编译 ### 前期的准备 确定内核版本 ``` 设备 二进制文件所在的位置 源代码所在的位置 编译配置 blueline device/google/crosshatch-kernel 说明 b1c1_defconfig crosshatch device/google/crosshatch-kernel 说明 b1c1_defconfig taimen device/google/wahoo-kernel kernel/msm wahoo_defconfig walleye device/google/wahoo-kernel kernel/msm wahoo_defconfig easel N/A kernel/arm64 mnh_prod_defconfig marlin device/google/marlin-kernel kernel/msm marlin_defconfig sailfish device/google/marlin-kernel kernel/msm marlin_defconfig hikey device/linaro/hikey-kernel kernel/hikey-linaro hikey_defconfig angler device/huawei/angler-kernel kernel/msm angler_defconfig bullhead device/lge/bullhead-kernel kernel/msm bullhead_defconfig shamu device/moto/shamu-kernel kernel/msm shamu_defconfig fugu device/asus/fugu-kernel kernel/x86_64 fugu_defconfig volantis device/htc/flounder-kernel kernel/tegra flounder_defconfig hammerhead device/lge/hammerhead-kernel kernel/msm hammerhead_defconfig flo device/asus/flo-kernel/kernel kernel/msm flo_defconfig deb device/asus/flo-kernel/kernel kernel/msm flo_defconfig manta device/samsung/manta/kernel kernel/exynos manta_defconfig mako device/lge/mako-kernel/kernel kernel/msm mako_defconfig grouper device/asus/grouper/kernel kernel/tegra tegra3_android_defconfig tilapia device/asus/grouper/kernel kernel/tegra tegra3_android_defconfig maguro device/samsung/tuna/kernel kernel/omap tuna_defconfig toro device/samsung/tuna/kernel kernel/omap tuna_defconfig panda device/ti/panda/kernel kernel/omap panda_defconfig stingray device/moto/wingray/kernel kernel/tegra stingray_defconfig wingray device/moto/wingray/kernel kernel/tegra stingray_defconfig crespo device/samsung/crespo/kernel kernel/samsung herring_defconfig crespo4g device/samsung/crespo/kernel kernel/samsung herring_defconfig ``` ### 环境搭建 需要一个交叉编译的环境 > git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/ > git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/ ### 下载源代码 ``` $ git clone https://android.googlesource.com/kernel/common.git $ git clone https://android.googlesource.com/kernel/hikey-linaro $ git clone https://android.googlesource.com/kernel/x86_64.git $ git clone https://android.googlesource.com/kernel/exynos.git $ git clone https://android.googlesource.com/kernel/goldfish.git $ git clone https://android.googlesource.com/kernel/msm.git $ git clone https://android.googlesource.com/kernel/omap.git $ git clone https://android.googlesource.com/kernel/samsung.git $ git clone https://android.googlesource.com/kernel/tegra.git git checkout -b android-msm-hammerhead-3.4-kitkat-mr2 ``` ### 开始编译 ``` export ARCH=arm64 export CROSS_COMPILE=aarch64-linux-android- cd hikey-linaro git checkout -b android-hikey-linaro-4.1 origin/android-hikey-linaro-4.1 make hikey_defconfig make ``` ### 打包编译 使用unmkbootimg ``` git clone https://github.com/pbatard/bootimg-tools.git make ``` unmkbootimg -i boot.img 用unmkbootimg 将之前编译生成的 zImage-dtb 替换kernel 进行重打包 ``` $ mkbootimg --base 0 --pagesize 2048 --kernel_offset 0x00008000 --ramdisk_offset 0x02900000 --second_offset 0x00f00000 --tags_offset 0x02700000 --cmdline 'console=ttyHSL0,115200,n8 androidboot.hardware=hammerhead user_debug=31 maxcpus=2 msm_watchdog_v2.enable=1' --kernel zImage-dtb --ramdisk ramdisk.cpio.gz -o bootnew.img ``` 刷机 ``` fastboot flash boot bootnew.img ``` ### 采坑 hammerhead编译过程中会遇到perl历史遗留问题,按照下面改就行了: http://sunyongfeng.com/201701/programmer/linux/kernel_compile_fail.html ## 参考资料 https://source.android.google.cn/setup/build/building https://source.android.com/setup/build/building-kernels#building https://forum.xda-developers.com/android/software-hacking/reference-how-to-compile-android-kernel-t3627297 https://developer.sony.com/develop/open-devices/guides/kernel-compilation-guides/how-to-build-and-flash-a-linux-kernel-for-aosp-supported-devices#tutorial-step-5