Can you compile Android13 system source code with Ubuntu?

Foreword

I have been working on Android applications for many years, but I have never known anything about AOSP. I don’t know anything about it. Car systems are so popular right now, so I want to get involved. I started to try to compile the system source code, and then I checked the official Android website. The latest version is still available. It does not support mac and window, but only supports ubuntu. I went to clean up the hard disk and prepared to flash the system. It may be because of the Macmini, but it never succeeded. It was very embarrassing. Finally, I built a virtual machine.

The whole process was quite troublesome. I downloaded the latest source code of Android 13, because the source code needs to be circumvented. I used Tsinghua University’s mirror, and the download was quite fast. It didn’t take two or three days as others said, but it still took half a day. It also took half a day to compile. There are many pitfalls in the process. I will record it and share it with everyone so that we can learn together.

1. Build environment

1.1. Preparation

VMWare: https://www.vmware.com/products/fusion/fusion-evaluation.html Ubuntu mirror: http://mirrors.aliyun.com/ubuntu-releases/

1.2, VMWare installation of Ubuntu

1.2.1. My computer is a Mac, so I downloaded the VMWare Fusion virtual machine. The official Android website recommends choosing ubuntu version 18.0.4, and the memory size given should be as large as possible. Mine is 16G, and the disk is given 12G. The size is 500G

1675172744307.jpg

1.2.2. If it is a Mac and a new hard drive is given, choose case sensitivity when formatting, otherwise Androidstudo will report an inexplicable error when running the source code

1675173592846.jpg

1.3. Download git and Python3

1.3.1, git installation and download

Install git:

 sudo apt install git

Install dependent tools:

sudo apt install git-core libssl-dev libffi-dev gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++ -multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11- dev libz-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip

Set up git identity and add your email and name:

git config --global user.email "[email protected]"
git config --global user.name "zhangxiaoxiao"

1.3.2, Python3 installation and switching

Download Python3:

sudo apt install python3

Configure update-alternatives, which is used to switch the currently used Python version. You need to set a soft link:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 (python2 installation address) 2 (weight number)
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 (python3 installation address) 3 (weight number)

Switch Python version:

sudo update-alternatives --config python

1675174640671.jpg

2. Source code download and compilation

AOSP (Android Open Source Project) is an Android open source project opened by Google. The official Chinese website is: source.android.google.cn/ Generally speaking, AOSP is an Android system source code project through which the Android operating system can be customized. Domestic mobile phone manufacturers It is a customized system developed on this basis. Because of the wall, if you cannot connect to the Google server to obtain the AOSP source code, you can download it from the Tsinghua University mirror station or the University of Science and Technology of China mirror. I am using the example of Zhongke

2.1. Download repo tool

Create bin and add it to PATH:

mkdir ~/bin
PATH=~/bin:$PATH

Install curl library:

sudo apt-get install curl

Download the repo and set permissions

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
## If the above URL is inaccessible, you can use the following:
## curl -sSL 'https://gerrit-googlesource.proxy.ustclug.org/git-repo/ + /master/repo?format=TEXT' |base64 -d > ~/bin/repo
chmod a + x ~/bin/repo

2.2. Download source code

Then create a working directory (with any name)

mkdir aosp
cd aosp

Initialize the warehouse:

repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest
## If you are prompted that you cannot connect to gerrit.googlesource.com, you can edit ~/bin/repo and replace the REPO_URL line with the following:
## REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'

1675178768113.jpg If you need something specific Android versions (Android version list):

repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-13.0.0_r24

Synchronize the source tree (you only need to execute this command to synchronize in the future):

repo sync

After waiting for several long hours, it is finally completed. If successful, there will be a prompt like this.

WechatIMG321.png

2.3. Install the build environment

Install jdk8

sudo apt-get update
sudo apt-get install openjdk-8-jdk

Install dependencies:

sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++ -multilib
sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386
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 libgl1-mesa-dev libxml2-utils xsltproc unzip m4
sudo apt-get install lib32z-dev ccache
sudo apt-get install libssl-dev

When installing sudo apt-get install libesd0-dev, the error “Unable to locate package libesd0-dev” will be reported. The solution is

sudo gedit /etc/apt/sources.list //Add the following two lines at the end of the line
deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main universe

1675180175543.jpg

2.4, Compilation

Use the envsetup.sh script to initialize the environment:

source build/envsetup.sh

Choose which target to build with lunch, and select the required target according to the device. The device can be officially provided by Google. Download it according to the driver address provided by Google: developers.google.cn/android/dri… Or you can use the computer simulator “sdk_phone_x86_64” , I don’t have any equipment here and I’m using a computer simulator.

lunch sdk_phone_x86_64

Build code

m j8 (8 is equal to the number of cores * 2)

Other instructions

- croot: Changes directory to the top of the tree.
- m: Makes from the top of the tree.
- mm: Builds all of the modules in the current directory.
- mmm: Builds all of the modules in the supplied directories.
- cgrep: Greps on all local C/C++ files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- godir: Go to the directory containing a file.
- clean - m clean removes all output and intermediate files for this configuration. This content is the same as rm -rf out/
The mmm instruction is used to compile the specified directory. Generally speaking, each directory only contains one module. For example, here we want to compile the Setting module and execute the instruction:
mmm packages/apps/Settings/

WechatIMG322.png Start the emulator

emulator

WechatIMG482.png

When the simulator is closed, it still exists in the background and needs to be closed by itself.

pkill qemu

3. Custom ROM real machine flashing

USB debugging configuration: (If the device USB can be connected to the computer virtual machine, it is ignored. If it is not connected to the computer using a virtual machine, it is ignored) developer.android.google.cn/studio/run/…

Driver download: developers.google.cn/android/drivers#angleropr6.170623.019 1675256709006.jpg After downloading the corresponding driver, put the compressed package into the root directory of the downloaded source code. After decompression, you will get two script files. Run the attached file from the root directory of the source code tree. self-extracting script and confirm that you agree to the terms of the accompanying license agreement. The binaries and their corresponding makefiles will be installed in the vendor/ hierarchy of the source tree

# My directory/home/zhangxiaoxiao/aosp
extract-qcom-barbet.sh
extract-google_devices-barbet.sh

1675258441080.jpg

To ensure that newly installed binaries can be used effectively after decompression, use the following command to delete all existing output from previous builds:

make clobber

Enter Bootloader mode: (Unlock BL lock)

adb reboot bootloader

View connected devices:

fastboot devices

Flash:

fastboot flashall -w

4. Process error collection

Unable to install libesd0-dev

Solution:
sudo vim /etc/apt/sources.list //Add the following two lines at the end of the line

deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main universe

Update software sources and reinstall:
sudo apt-get update & amp; & amp; sudo apt-get install libesd0-dev

There is not enough memory. The new version requires 16G. It may be that you have opened too many threads. For example, make -j8. Another reason is that the swap area may not be set up, that is, the disk space is used as temporary memory. Note that the virtual machine or computer needs to be restarted after the setting is completed.

//View memory size
free -m
//Create a swap partition file: to add a 16G swap partition, the command is written as follows, where count is equal to the desired block size
dd if=/dev/zero of=/home/swapfile bs=1M count=16384
//Set the swap partition file and establish the swap file system
mkswap /home/swapfile
//Enable swap partition file immediately
swapon /home/swapfile

//If you want to enable it automatically when the system boots, add a line to the file /etc/fstab
/home/swapfile swap swap defaults 0 0

KVM virtualization requires hardware support to report errors

//Error message
INFO | Android emulator version 31.3.9.0 (build_id 9070145) (CL:N/A)
INFO | Storing crashdata in: /tmp/android-zhangxiaoxiao/emu-crash.db, detection is enabled
INFO | Duplicate loglines will be removed, if you wish to see each indiviudal line launch with the -log-nofilter flag.
ERROR | x86_64 emulation currently requires hardware acceleration!
CPU acceleration status: KVM requires a CPU that supports vmx or svm

//The solution for installing kvm is as follows
https://help.ubuntu.com/community/KVM/Installation
//Simulator reference
https://blog.csdn.net/mvp_Dawn/article/details/107678057

After making, you cannot directly start the emulator to start the computer simulator, and an error will be reported. Lunch aosp_x86-eng version will not work. If Android12 changes it, an error will be reported.

ERROR | Failed to create Vulkan instance.
qemu-system-x86_64: Could not open '/home/zhangxiaoxiao/aosp/out/target/product/generic_x86_64/userdata-qemu.img': No such file or directory

//Solution
lunch sdk_phone_x86_64

Thank you

www.jianshu.com/p/6dee223ac… blog.csdn.net/kfyzjd2008/… www.jianshu.com/p/53941de91… www.pudn.com/news/62599c… www.cnblogs.com/caoxinyu/p/…