Article Directory
illustrate
Recently, due to the epidemic situation, I have been working from home, and I have some free time to set up the development environment. In fact, I had a hunch before, so I bought an unlocked Pixel4 (Flame) on Xianyu, and used it with a cloud computer (Ubuntu 18.04.1) to build an Android system development environment on the cloud computer.
This article mainly records the whole process of downloading, compiling and flashing the Android source code (Android 12).
Benefits of local compilation:
- Brushing the real device is convenient for developers to debug locally, and the code can be imported into Android Studio for debugging.
- Can compile the Userdebug version, root and remount, which is convenient for debugging the system and App. In Debug mode, you can see many problems that cannot be seen in the User version; at the same time, because you can see more information, it is also convenient for App competing products Analysis, App behavior analysis.
- It is more convenient to learn the Android source code. The local version can open a lot of system-level Debug Logs. You can also add logs and type function call stacks to analyze the operating logic of system services and interfaces, and you can also modify the operating process of the system by yourself. .
Hardware requirements:
- A host with sufficient configuration, installed with Ubuntu 18.04.1 and above. Here I strongly recommend Amway cloud computer products, such as Tianyi cloud computer and other products, you can choose super configuration and Ubuntu system. The biggest advantage is that no matter where you go, you can feel that the compilation server is by your side.
- A Google pro son mobile phone, such as Pixl3L, Pixl4 , there are many on Xianyu. Note: Android 12 only supports machines above Pixl3L.
1. Code download
Due to the use of Google’s official download site in China, there will be cases where the download does not move, and sometimes the .repo cannot be downloaded, so this tutorial uses the domestic mirror site as an example. Refer to the official tutorial https://source.android.google.cn/source/downloading
USTC AOSP mirror site address: https://mirrors.ustc.edu.cn/help/aosp.html
To download, you only need to follow the following steps. It is recommended that you use a Linux system such as Ubuntu to download, compile, and develop the code.
1.1 Download the Repo tool
mkdir ~/bin
PATH=~/bin:$PATH
curl -sSL 'https://gerrit-googlesource.proxy.ustclug.org/git-repo/+/master/repo?format=TEXT' |base64 -d > ~/bin/repo
chmod a+x ~/bin/repo
1.2 Install and configure Git
sudo apt install git
git config --global user.name "xxxx"
git config --global user.email "[email protected]"
After configuring git, you can check whether it is successful through git config --list , such as mine:
user.name=quexl1
user.email=quexl1@xxxx.cn
1.3 Create project directory
Create a working directory locally (the name is arbitrary, the machine I bought is Pixel4, the code name is flame, here is aosp_12.0_flame as an example)
mkdir aosp_12.0_flame
cd aosp_12.0_flame
1.4 Initialize the Repo warehouse
repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-12.1.0_r11
## 如果提示无法连接到 gerrit.googlesource.com,可以编辑 ~/bin/repo,把 REPO_URL 一行替换成下面的:
## REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'
If it is not followed by "-b", all codes will be downloaded by default, and the default branch is master.
I synchronize the latest 12.0 version here, so the branch used is: android-12.1.0_r11
It should be noted here that the address used by the default repo is REPO_URL = 'https://gerrit.googlesource.com/git-repo', here we need to modify REPO_URL, otherwise it will fail to download
- Method 1: In your rc file, add a configuration: REPO_URL="https://gerrit-googlesource.proxy.ustclug.org/git-repo"
- Method 2: Open ~/bin/repo directly, and replace the REPO_URL line with the following: REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'
After downloading the .repo, there will be the following information
➜ Android12 repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest
Downloading Repo source from https://gerrit-googlesource.proxy.ustclug.org/git-repo
... A new version of repo (2.17) is available.
... You should upgrade soon:
cp /home/gracker/Code/Android12/.repo/repo/repo /home/gracker/bin/repo
Downloading manifest from git://mirrors.ustc.edu.cn/aosp/platform/manifest
remote: Enumerating objects: 91965, done.
remote: Total 91965 (delta 0), reused 0 (delta 0)
Your identity is: quexl1 <quexl1@xxxx>
If you want to change this, please re-run 'repo init' with --config-name
repo has been initialized in /home/aosp_12.0_flame
1.5 Synchronization code
After the above steps initialize the .repo file, code synchronization needs to execute repo sync to download. Due to the limitations of the mirror site and the problems that may be encountered during the download process, it is recommended that you use -j4 to download
repo sync -j4
2. Driver download
After the code download is complete, we are not in a hurry to compile it. If we want to run it on the real machine, we need to download some closed-source driver files from the manufacturer, so that the subsequent compiled code can run on the real machine. Here is the corresponding official
2.1 Download the driver of the corresponding model
Since the code I synchronized is the android-12.1.0_r11 branch, the driver needs to be downloaded here https://developers.google.cn/android/drivers
Taking my pixel 4 as an example, the downloaded code is android-12.1.0_r11, and the driver BUILD ID corresponding to TAG can be checked here: https://source.android.google.cn/docs/setup/about/build- numbers , so the driver I need to download is
Click Link to download the two files, then unzip them to the root directory of the code, and then execute the sh script to release the driver to a suitable location. The binary files and their corresponding makefiles will be installed in the vendor/ hierarchy of the source tree.
2.2 Driver Extraction
After the downloaded content is decompressed, there are two sh files. Take my Pixel 4 as an example, execute it in the root directory of the code, use D to turn the page down, and finally manually enter I ACCEPT
# 解压缩 extract-google_devices-crosshatch.sh
./extract-google_devices-flame.sh
# 解压缩 ./extract-qcom-crosshatch.sh
./extract-qcom-flame.sh
3. Code compilation
After the code and driver are downloaded, you can start to compile the code. Since the new version no longer supports Mac compilation, it is recommended that you still use Linux for compilation, and Ubuntu is recommended.
3.1 Set up the compilation environment
Reference: https://source.android.google.cn/setup/build/initializing
Run directly on Ubuntu 18.04 or above:
sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig
Note that if apt-get cannot find the source, you can solve it by modifying the source of apt-get, and Baidu by yourself.
You can also use my source to replace sources.list under /etc/apt
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
sudo apt-get update update;
3.2 Initialize the compilation environment
Execute in the root directory of the project source code
source build/envsetup.sh
3.3 Select compilation target
lunch
A list of compilation targets will be output for selection.
You're building on Linux
Lunch menu .. Here are the common combinations:
1. aosp_arm-eng
2. aosp_arm64-eng
3. aosp_barbet-userdebug
4. aosp_bluejay-userdebug
5. aosp_bramble-userdebug
6. aosp_bramble_car-userdebug
7. aosp_car_arm-userdebug
8. aosp_car_arm64-userdebug
9. aosp_car_x86-userdebug
10. aosp_car_x86_64-userdebug
11. aosp_cf_arm64_auto-userdebug
12. aosp_cf_arm64_phone-userdebug
13. aosp_cf_x86_64_auto-userdebug
14. aosp_cf_x86_64_foldable-userdebug
15. aosp_cf_x86_64_pc-userdebug
16. aosp_cf_x86_64_phone-userdebug
17. aosp_cf_x86_64_tv-userdebug
18. aosp_cf_x86_phone-userdebug
19. aosp_cf_x86_tv-userdebug
20. aosp_cheetah-userdebug
21. aosp_cloudripper-userdebug
22. aosp_coral-userdebug
23. aosp_coral_car-userdebug
24. aosp_flame-userdebug
25. aosp_flame_car-userdebug
26. aosp_oriole-userdebug
Just choose the code name of your own machine. The machine code name of my Pixel 4 is flame , so choose 24.
You can check the corresponding codes of all models here: https://source.android.google.cn/setup/build/running#selecting-device-build
3.4 compile
Execute make compilation, you can choose 12 or 32 thread compilation.
make -j12
After the compilation is successful, there will be the following output
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
[100% 777/777] Target vbmeta image: out/target/product/flame/vbmeta.img
#### build completed successfully (03:57 (mm:ss)) ####
4. Flash
After compiling, start flashing, take my test machine Pixel 4 as an example, execute the following commands in sequence
adb reboot fastboot
# 等待手机进入 fastboot 界面之后
fastboot flashall -w
# 刷机完成之后,执行 fastboot reboot 长期系统即可
fastboot reboot
Note: If the fastboot flashall -w command is executed outside the project, the $ANDROID_PRODUCT_OUT directory environment must be configured
The screenshot of flashing is as follows:
After that, the phone will automatically restart, and then enter the main interface. At this point, our code download-compile-flashing part is over.
If you encounter problems during the flashing process, you can flash the official flashing package to save: https://developers.google.cn/android/images
5. Mount the system partition
DM-verity is an important part of Android system security, it can ensure that the contents of Android vendor and system image files are authentic and reliable. So if the DM-verity function is turned on, then if we modify the system partition or vendor partition of the android system through fastboot, an error will occur during DM-verity verification, so we need to unlock DM-verity before fastboot.
adb root
adb reboot bootloader
fastboot devices
fastboot flashing unlock
fastboot reboot (wait for the system to restart)
At this time, it is found that the oem option has turned gray.
adb root
adb disable-verity
adb reboot
adb root
adb remount can successfully remount /system in read-write mode
Note: fastboot needs to install the corresponding driver before it can be used.
Well, the above is the whole process of downloading, compiling and flashing the Android 12 source code.
6. Single compile Framework module
framework.jar single compilation and replacement:
first modify the code of the framework/base/core part, compile framework.jar by the following command
make framework-minus-apex
After compiling framework.jar, execute remount with the fifth step.
Then push framework.jar to system/framework/, and delete the oat, arm, and arm64 directories under this directory (if you don’t delete them, you will always be stuck in the boot animation), and finally run the following command to restart zygote, so The newly replaced framework.jar takes effect.
adb shell stop;adb shell start;
It is relatively simple to compile and replace services.jar
: first, after modifying the code of the framework/base/services part, compile services.jar with the following command
mmm framework/base/services -j12
After compiling services.jar, execute remount with the fifth step, then push services.jar to system/framework, and restart.
Reference:
https://source.android.google.cn/source/downloading?hl=zh-cn
https://source.android.google.cn/docs/setup/about/build-numbers
https://developers. google.cn/android/drivers
https://android.googlesource.com/platform/manifest
https://blog.csdn.net/superlee1125/article/details/115491698