Nexus 搞机之路 —— aosp源码的获取与编译

本文介绍如何使用中科大镜像下载并编译Nexus5x的AOSP源码,包括安装repo工具、下载内核源码、编译过程及解决内存不足等问题。

Nexus5x 源码的下载与编译

由于国内的墙的关系,即使使用了梯子,在repo 源码的时候也会出现失败的现象,辛亏国内有中科大和清华大学aosp的源,加快了repo 的速度

国内镜像的说明

AOSP镜像的一些说明文档,可以根据说明进行下载
https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
https://lug.ustc.edu.cn/wiki/mirrors/help/aosp

1.下载安装repo

  1. Make sure you have a bin/ directory in your home directory and that it is included in your path:
    mkdir /bin PATH=~/bin:$PATH
  2. Download the Repo tool and ensure that it is executable:
    $ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    $ chmod a+x ~/bin/repo

2.下载aosp源码

2-1.可以先下载他们提供的aosp.tar包,在通过repo sync同步到最新
2-2.如果需要切换到其他版本
repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-7.1.1_r11
repo sync
注:

已有仓库如何改用科大源
如果您已经从官方同步了 AOSP 仓库,现在希望使用科大的 AOSP 仓库,请修改 .repo/manifests.git/config,将
url = https://android.googlesource.com/platform/manifest
修改成
url = git://mirrors.ustc.edu.cn/aosp/platform/manifest

3.下载kernel

Download the source for the kernel you want to build using the appropriate git clone command:
$ 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

由于我们使用的是nexus5所以下载msm.git
git clone https://mirrors.ustc.edu.cn/aosp/kernel/msm.git

4.编译

make clobber
sourcebuild/envsetup.shor . build/envsetup.sh

$ lunch aosp_bullhead-userdebug
or lunch之后选择需要编译的项目

make bootimage编译boot.img

fastboot boot boot.img 不会刷入flash 中,
fastboot flash boot boot.img 刷入内存中

全编译的时候应该注意设置java虚拟机的堆栈大小git clone https://android.googlesource.com/kernel/msm.git
export JACK_SERVER_VM_ARGUMENTS=”-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m”
1. out/host/linux-x86/bin/jack-admin kill-server
2. out/host/linux-x86/bin/jack-admin start-server


!!!编译的时候会出现JAVA虚拟机内存不足的情况

  1. 提示:increasing heap size。就是Java虚拟机的heap size不够了,添加环境变量:
    export _JAVA_OPTIONS=-Xmx8192m

赋值大小看自己机器了。
然而,即使这样设置了,有时候也不起作用,因为可能机器本身硬件内存就不足了,根本就没有8G那么多好吗。由于自己的机器是8G的,而且运行在虚拟机上,只分配到4G的空间,所以build的过程中,内存不足的问题卡了自己很久很久,而且官方说,最好是有16g ram以上,几乎要放弃了。最后想了个办法,增加swap内存。
至于如何增加swap内存,参考这里。

自己直接swap增加了16G,由于本来是8G,所以最好是24G了。
感叹一下,UNIX的“Everything is file”的伟大,硬件不够,软件来补,呵呵~~~
解决这个内存问题后,就顺利的build出来了

增加swap分区
mkdir swap
cd swap
sudo dd if=/dev/zero of=swapfile bs=1024 count=10000000
sudo mkswap swapfile
sudo swapon swapfile

free -m查看swap 分区是否已经调整

bootimage编译方法:

git clone xxxxxxxxxxxxxxxxxxx/msm.git
在msm.git中解压的文件夹中进行编译
makeARCH=arm64bullheaddefconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-android- -j41212

在aosp目录中编译

google中的方法
Building the kernel

When you know the last commit message for a kernel and have successfully downloaded the kernel source and prebuilt gcc, you are ready to build the kernel. The following build commands use the hikey kernel:
exportARCH=arm64 export CROSS_COMPILE=aarch64-linux-android-
cdhikeylinaro git checkout -b android-hikey-linaro-4.1 origin/android-hikey-linaro-4.1
makehikeydefconfig make
To build a different kernel, simply replace hikey-linaro with the name of the kernel you want to build.
The image outputs to the arch/arm64/boot/Image directory; the kernel binary outputs to thearch/arm64/boot/dts/hisilicon/hi6220-hikey.dtb fle. Copy the Image directory and the hi6220-hikey.dtb file to the hikey-kernel directory.
Alternatively, you can include the TARGET_PREBUILT_KERNEL variable while using make bootimage (or any other make command line that builds a boot image). This variable is supported by all devices as it is set up via device/common/populate-new-device.sh. For example:
exportTARGETPREBUILTKERNEL= your_kernel_path/arch/arm/boot/zImage-dtb
Note: Kernel names differ by device. To locate the correct filename for your kernel, refer todevice// in the kernel source.

生成驱动目录

接下来需要在Android官网下载 Nexus5所需要的驱动:
Hardware Component
Company
Download
MD5 Checksum
SHA-1 Checksum
NFC, Bluetooth, Wi-Fi
Broadcom
Link
2c398994e37093df51b105d63f0eb611
991346159c95ae75f760014a6822b8b3e8667700
Camera, Sensors, Audio
LG
Link
74cf8235e6bb04da28b2ff738b13eee9
175dd5bae81bb54030d072cb0f0b4ec81eb3f71f
Graphics, GSM, Camera, GPS, Sensors, Media, DSP, USB
Qualcomm
Link
0a43395e175d3de3dc312d8abdcb4f20
007cf9d49f0409d5c703e7f2811fd153fee22353
下载完成后,解压出来是三个.sh文件,放到Android源码目录下面,然后执行.会将相关驱动放到vender目录下面.

编译AOSP源代码时,需要遵循一系列标准化的步骤,并使用特定的命令来初始化环境、选择目标设备以及执行实际的编译过程。以下是详细的说明: ### 初始化构建环境 首先,确保已经安装了所有必要的依赖项。对于Ubuntu 22.04系统,可以运行如下命令安装所需的库和工具: ```bash sudo apt-get install git gnupg flex bison build-essential zip curl zlib1g-dev libc6-dev-i386 x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig ``` 如果遇到缺少`libncurses5`的问题,可以创建符号链接以使用更高版本的库替代[^4]。 接下来,下载并安装`repo`工具,它用于管理多个Git仓库。可以通过以下方式获取`repo`: ```bash mkdir ~/bin PATH=~/bin:$PATH curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo chmod a+x ~/bin/repo ``` 然后,设置你的Git用户名和邮箱: ```bash git config --global user.name "Your Name" git config --global user.email "you@example.com" ``` 创建一个目录来存放AOSP源码,并进入该目录进行初始化: ```bash mkdir aosp cd aosp repo init -u https://android.googlesource.com/platform/manifest -b android-14.0.0_r50 ``` 这里的分支名`android-14.0.0_r50`可以根据需求更改为其他版本。 同步源代码树: ```bash repo sync ``` ### 配置编译选项 配置编译环境之前,需要加载环境变量脚本: ```bash source build/envsetup.sh ``` 之后,使用`lunch`命令选择要编译的目标产品和构建类型。例如,如果你有一台Nexus 6p真,可以选择如下配置: ```bash lunch aosp_angler-userdebug ``` 对于模拟器或通用设备,可以选择如下的通用配置: ```bash lunch aosp_x86_64-eng ``` 其中,`-eng`表示工程版本,适合开发用途;`-userdebug`则适用于调试用户版本;而`-user`则是最终发布的用户版本。 ### 执行编译 一旦选择了合适的产品和构建类型,就可以开始编译过程。通常情况下,使用`make`命令加上-j参数指定并行任务数(建议为CPU核心数目的两倍)来进行编译: ```bash make -j$(nproc) ``` 或者,在某些较新的Android版本中可能需要使用`m`命令代替传统的`make`: ```bash m -j$(nproc) ``` 这将启动整个AOSP系统的编译流程,包括Framework、应用和其他组件。 ### 编译后的操作 成功完成编译后,可以在`out/target/product/<product_name>`目录下找到生成的镜像文件。这些镜像可以通过Fastboot工具刷入到支持的设备上,以便测试新编译的ROM。 此外,如果你想将新编译的系统部署到模拟器中,可以直接运行: ```bash emulator ``` 这个命令会启动带有最新编译结果的Android Emulator。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值