Android 11 has been issued for a long time, also has a lot of online tutorials about android 11 compilation, but personally I refer to these tutorials to compile time met a lot of big pit, experienced difficulty is to compile it finally came out, now I make some conclusion on compilation process, including my personal problems and solutions encountered in compiling, To provide everyone as a reference, maybe also can help you solve the puzzle in front of the computer, without saying more, first for the above proof.



Use android 11 after compiling successfullyemulatorStart the effect

Zero. Preparation

Before you can begin, your development workstation must meet or exceed the following hardware requirements:

  • For Android 2.3.x (Gingerbread) and later (including the Master branch), a 64-bit environment is required. If it is a lower version, it can be compiled on a 32-bit system.
  • To check out code, you need at least 250 GB of available disk space; If you want to build, you need another 150 GB. If you do multiple builds, you need more space.
  • At least 16 GB of available RAM is required, but Google suggests 64 GB.

Starting in June 2021, Google will use a 72-core machine with 64 GB of internal RAM, and the full build process will take about 40 minutes (incremental builds will take just a few minutes, depending on which files are modified). By comparison, a six-core machine with a similar amount of RAM would take three hours to complete the build process.

The operating system

If you are developing for the AOSP Master branch, use Ubuntu 18.04 (Bionic Beaver).

Warning: Android OS builds on Windows or MacOS are no longer supported as of June 22, 2021.

JDK

The Android Master branch in AOSP comes with a pre-built version of OpenJDK; Therefore, no separate installation is required. Older versions of Android required a separate installation of the JDK. On Ubuntu, use OpenJDK.

A, download

1. REPO download

Because the traditional aOSP acquisition method is not feasible in China, we use the REPO mirror address to 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
Copy the code

Because Android source code references a lot of open source projects, each sub-project is a Git repository, each Git repository has a lot of branch versions, in order to facilitate the unified management of the Git repository of each sub-project, need an upper tool batch processing, so repO was born. Repo is actually made up of a series of Python scripts that do their job by calling Git commands.

2. Modify the REPO tool

The REPO tool updates itself every time it is used by accessing the REPO source, which was designed by the REPO designers to maintain version consistency. Similarly, the REOP source address cannot be connected in the country, so we need to replace the previous source address with a mirror address.

#Edit ~/bin/repo and replace the REPO_URL line with the following:
REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'
Copy the code

Create working directory (name optional)

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY
Copy the code

4, source download

Due to the large size of AOSP source code, you need to complete this step under a good network, here provides two ways to download AOSP source code, you can choose according to their own situation.

(1) Use the monthly update initialization package

As the first synchronization needs to download about 95GB of data, any network failure may cause synchronization failure. Fortunately, Tsinghua University mirror provides us with an initialization package for initialization. Download mirrors.tuna.tsinghua.edu.cn/aosp-monthl… Check the checksum. TXT file. Since all of the code is checkout from the hidden.repo directory, we only keep the.repo directory. After downloading it, unpack it and repo sync again to get the full directory. The usage method is as follows:

Initialization wget - c # https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar download package tar xf aosp - latest. Tar CD aosp # Decompress the AOSP project directory#You can't see anything with ls because there is only a hidden.repo directoryRepo sync # The complete directory can be obtained after a normal synchronization#Or repo Sync-L just checkout code
Copy the code

After that, you can simply run Repo Sync each time to keep in sync.

(2) Traditional initialization method

Initialize the warehouse:

repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest
Copy the code

If you need a specific Android version (list) :

Repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest - b android - 4.0.1 _r1Copy the code

Synchronize the source tree (just execute this command later to synchronize) :

repo sync
Copy the code

When you have successfully synchronized the source tree, you should see the following results on your terminal:

. Checking out file: 100% (15779/15779), done. Checked out files: 7% (1251/15779) Checking out files: 100% (29/29), done. Checking out files: 27% (8/29) Syncing work tree: 100% (568/568), done.Copy the code

At present, android source code is measured to download the whole need about 130G space, this part will cost you at least 2 hours more time, the specific time depends on your network environment.

Second, source code compilation

After downloading the source code successfully, we will start the compilation process. The Android official website provides a more detailed introduction. However, AFTER compiling, I encountered a problem that I could not start the emulator. In fact, the MK file exists, and only the Lunch version is available. Other files are for proprietary devices, which will cause the simulator to fail to work normally.

1. Set the environment

Use the envsetup.sh script to initialize the environment:

source build/envsetup.sh
Copy the code

2. Choose a target [lunch]

Use Lunch to select the goal to build. Lunch product_name-build_variant selects product_name as the product to build and build_variant as the variant to build, and stores these choices in the environment, To be read by subsequent calls to m and other similar commands. The exact configuration can be passed as a parameter. For example, the following command represents a complete build against the emulator, with all debugging enabled.

lunch sdk_phone_x86_64
Copy the code

Note that the lunch aoSP_ARM-eng given in this step on android official website cannot run normally after the actual compilation is completed. This is a big pit, many people are stuck in this step for a long time, the real thing that can run on the simulator is sdk_phone_x86_64

If you run it without parameters, Lunch will prompt you to select the target from the menu.

All BUILD targets take the form of build-buildType, where BUILD is a code name for a particular combination of functionality. BUILDTYPE is one of the following types:

Building type usage
user Limited authority; Suitable for production environment
userdebug Similar to user, but with root permission and debugging functions. Is the preferred compilation type for debugging
eng Development configuration with additional debugging tools

3. Build code

Build everything with m. M can handle parallel tasks with the -jn argument. If you do not provide the -j parameter, the build system automatically selects the parallel task count that you think is best for your system.

m
Copy the code

In general, the optimal number of threads for multithreaded compilation is four times the number of your processor core and twice the number of logical threads. For example, my compilation host CPU is 8 cores and 16 threads, so I can choose to compile with 32 threads, and the compilation command becomes M-j32

4. Start the simulator

The build process automatically adds the emulator to your path. To run the emulator, enter the following command:

emulator
Copy the code

If everything is going well, at this stage you can see your own android operating system running on the emulator, but as mentioned above, the compilation process is not always smooth. If you run into problems, you can refer to the following troubleshooting problems

[Appendix] Troubleshooting

Repo syntax error

If you run repo init or any other repo command and you get output similar to the following, the version of an annrepo you are using does not match the Python version on your computer system.

File "./repo"
line 175 except OSError
e: SyntaxError: invalid syntax
Copy the code

It is worth noting that Google has officially abandoned the REPo Python2.x version, and the existing REPO uses the Python3.x version, You need to update your Python version to 3.x or use the following command to use repo(where ~/bin/repo is the path where your repo was installed):

python3 ~/bin/repo
Copy the code

2, ‘userdata-qemu.img’: No such file or directory

If the following output is displayed after you run the emulator command, it indicates that you have selected an Android operating system dedicated to other devices in the lunch step. You need to run the lunch sdk_phone_x86_64 command again to select and rebuild your Android source code.

root@xxx:~/aosp# emulator: Android emulator version 30.7.4.0 (build_id 7479360) (CL:N/A) emulator: WARNING: encryption is off handleCpuAcceleration: feature check for hvf cannot add library /root/aosp/prebuilts/android-emulator/linux-x86_64/qemu/linux-x86_64/lib64/vulkan/libvulkan.so: failed added library /root/aosp/prebuilts/android-emulator/linux-x86_64/lib64/vulkan/libvulkan.so emulator: ERROR: VkCommonOperations.cpp:537: Failed to create Vulkan instance. qemu-system-x86_64: Could not open '/root/aosp/out/target/product/generic_x86_64/userdata-qemu.img': No such file or directoryCopy the code

Unable to connect to ADB daemon on port: 5037

If the following output is displayed when you run emulator, your ADB tool is not running yet.

emulator: ERROR: AdbHostServer.cpp:102: Unable to connect to adb daemon on port: 5037
Copy the code

You need to execute the following code to run the ADB tool

adb start-server
Copy the code

Above is the whole process I compiled 11 android source code and the problems, actually compiled android source took me nearly a month of time, there are a lot of little pit pit failed to record, if you met other errors during compilation, are welcome to leave a message below, if I just met, can provide some references for you, At the same time, you can also enrich this article’s troubleshooting guide.