The Android Open Source Project (AOSP) starts with an introduction to how to download and compile AOSP (Android Open Source Project) on Ubuntu installed with VirtualBox. Since my computer is macOS, I first tried to download and compile AOSP on macOS, which encountered many problems. Therefore, I sort out this article for future reference. At the same time, Ubuntu has also been tried, with fewer problems, and the solutions are listed at the end.

OS: macOS Catalina 10.15.6 AOSP Target platform: Android-9.0.0_R1

1. Configure the macOS environment

Git is used to download and manage aOSP source code because Git is case-sensitive. So the first thing to do is to partition a case-sensitive disk.

Create a case-sensitive disk

Use the following command to create a case-sensitive, journaling disk.

Hdiutil CREate-type SPARSE -fs' Case-sensitive Journaled HFS+ '-size 200g ~/ Android.dmgCopy the code

The size can be adjusted according to the need. If you just download the source code, 100G is almost enough. To compile, you need 200GB or more. If the disk space is insufficient, run the following command to adjust the size of the created disk:

Note: The created disk file suffixes may be android.dmg. sparseImage and Android.dmg respectively, depending on the system. Therefore, the file names of the following commands are inconsistent.

hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage
Copy the code

You can add the following two methods to ~/. Bash_profile to easily mount and unmount disks. You may need to perform the source ~/. Bash_profile import method before executing this method:

# mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg.sparseimage -mountpoint /Volumes/android; }

# unmount the android file image
function umountAndroid() { hdiutil detach /Volumes/android; }
Copy the code

Once mounted, you can see the disk in Finder or disk tools in the format of MacOS Extended, case sensitive, log disk. As shown below:

In addition, the default maximum number of open files in macOS is low and may be exceeded when compiling AOSP in parallel. So if you plan to compile the source code in parallel (preferably because there is too much code), you will need to set the file descriptor cap by adding the following to ~/.bash_profile, or you may need to execute source ~/.bash_profile for the configuration to take effect:

# set the number of open files to be 1024
ulimit -S -n 1024
Copy the code

At this point, everything about disk files is set up. Here’s how to install the software for download.

Install required software

Install Xcode

Run the following command:

xcode-select --install
Copy the code

Install Git using Homebrew

Run the following command to install Homebrew: Click to go to the Homebrew official website

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Copy the code

Run the following command to install and configure Git:

# install git by homebrew
brew install git

# config git user info
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Copy the code

Check whether PYTHon2.7 is installed

Python2.7 is required by aosp, python3 is not. MacOS comes with python2.7. You can use the following command to check:

python --version
Copy the code

Have the following output:

Python 2.7.16
Copy the code

Install jdk8

Click to download JDK 8 and select the macOS version to download and install. Once the installation is complete, verify by running the following command:

java -version
Copy the code

Download the REPO tool

Aosp source code is managed by Git, but because of the huge amount of code, simple use of Git has been unable to meet the goal of daily convenient operation. So Google based on Git, using Python to write repo this tool, so that developers can be more convenient and easy to manage the source code, click to see repo use details.

First, create the REPO tool store folder.

mkdir ~/bin
Copy the code

Then, download the REPO and give the REPO executable permissions.

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo

chmod a+x ~/bin/repo
Copy the code

Finally, add the REPo tool to the PATH PATH so that we can access it directly from the terminal later, regardless of where the REPO is located. Add the following to ~/.bash_profile:

export PATH=~/bin:$PATH
Copy the code

At this point, the related software installation is complete. Next, download the source code.

2. Use repo to download the source code

Creating a folder

First, go to the disk we created and mounted earlier, and create and enter the AOSP source download folder.

cd /Volumns/android
mkdir aosp
cd aosp
Copy the code

Initialize the version library

Then, initialize a specified version of the AOSP source library. If the command is followed by the -b argument, it initializes the source repository for the specified version, otherwise it is the default master branch. For a list of branches, refer to aOSP codes, tags, and breakdown version numbers. The initialization code is as follows:

# default is master
repo init -u https://android.googlesource.com/platform/manifest

#Define 9.0.0 _r1 branchRepo init -u https://android.googlesource.com/platform/manifest - b android - 9.0.0 _r1Copy the code

If you have network problems, you can use tsinghua image source. For details, please refer to AOSP Tsinghua Open Source Software Image site. It should be noted that if you simply read the code, use tsinghua source. If you plan to compile the code, it is recommended to use the official source of Google if possible, because there is no synchronization problem in Tsinghua source, which may cause some unnecessary trouble.

Tips: Use tsinghua source as far as possible at night, during the day tsinghua source request is large, download source is very easy to interrupt. At night, it’s smoother.

The initialization is successful if the following output is displayed:

Download the source code

Run the following command in the initialized AOSP source directory to start downloading the source code.

repo sync
Copy the code

If the following output is displayed, the download is successful:

At this point, the android 9.0.0 code download alone takes up 99GB. As shown in the figure below.

3. Build source code

Introduction to aOSP construction system

Aosp was originally built using make source code. But because make was slow and error-prone on Android, Google turned to Soong, written in Go, to build the system. Soong is an alternative to the make build system, which uses.mk files to write compilation rules, and Soong, which uses Blueprint’s.bp files. The.bp file is a jSON-like syntax structure, which is much simpler. Soong eventually compiled the.bp file into a Ninja file, which was compiled by Ninjia. Ninjia is a small build system designed to be built into an advanced build system that builds as fast as possible. Its build files are readable but not suitable for manual writing — similar to assembly language, usually by compiling build files from other advanced build systems into NINJia files as input.

Readers may wonder at this point, Google from make to Soong can not happen overnight, so how to integrate the two? The answer is that Google developed kati for the.mk file, compiled it into ninjia, and handed it to Ninjia for compilation.

In general, Soong built it by parsing.bp files into ninjia files and compiling.mk files into ninjia files through kati.

If you want to learn more, the following resources are available:

  • Android Make Build System
  • kati
  • Soong
  • Ninja, a small build system with a focus on speed

Initialize the build environment

Execute the envsetup.sh script to initialize the environment, and envsetup.sh adds a series of commands for compilation.

Note: Compile commands need to be executed under bash shell. If you are using another shell such as ZSH, you need to run bash to enter bash shell before running build commands.

source build/envsetup.sh
Copy the code

Select the target system for compilation

lunch
Copy the code

The output is as follows:

/Volumes/Windows/ AOSP lunch You're building on Darwin Lunch Menu... pick a combo: 1. aosp_arm-eng 2. aosp_arm64-eng 3. aosp_mips-eng 4. aosp_mips64-eng 5. aosp_x86-eng 6. aosp_x86_64-eng 7. aosp_car_arm-userdebug 8. aosp_car_arm64-userdebug 9. aosp_car_x86-userdebug 10. aosp_car_x86_64-userdebug ...... Which would you like? [aosp_arm-eng]Copy the code

You can enter English or serial number. Because the CPU of my computer is x86 architecture, AOSP_x86-ENG is selected here for compilation, which will be faster when running Android VIRTUAL machine.

Begin to compile

To start compiling, run the make command, or add the -j parameter if you want to build in parallel. The following command is an example:

make

# simplify
m

# multi task
m -j16
Copy the code

There are a number of problems encountered during compilation, which are explained in the next section. Finally, build completed successfully is printed.

Running simulator

If you have just finished compiling, execute emulator directly. Otherwise, run the source build/envsetup.sh command again. lunch aosp_x86-eng; Import the emulator command.

emulator
Copy the code

Finally, I successfully run the Android VIRTUAL machine I built!

4. Possible problems and solutions

Could not find a supported MAC SDK

The problem details are as follows:

Internal error: Could not find a supported MAC SDK: [" 10.10 "" 10.11" "10.12" "10.13"] Ninja: Build Stopped: subcommand failed.Copy the code

Perform the ls/Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs found that MacOSX. The SDK, There is also a corresponding macOSX 10.15.sdk soft link. The MAC SDK version on the local PC is 10.15.

Solutions:

Modify the build/soong/cc/config/x86_darwin_host go file, add under darwinSupportedSdkVersions “10.15”, (remember to bring a comma in English).

Fault 2: An error occurs during compilation after the preceding operations are performed

Details of the error are as follows:

ld: symbol(s) not found for architecture i386
Copy the code

The reason is that AOSP does not support all versions of the MAC SDK. For example my computer’s MAC SDK version 10.15, the build configuration file/soong/cc/config/x86_darwin_host. Go the configuration is as follows:

darwinSupportedSdkVersions = []string{
        "10.10"."10.11"."10.12"."10.13".Copy the code

Here you can see that the official test passed and the supported MAC SDKS are the above versions. Newer versions are not supported.

Finally, Unable to make AOSP Systemimage on macOS Mojave found the answer to the problem.

Solutions:

Download version 10.13 at Phracker/MacOsx-SDks. Placement after decompression to Mac SDK folder/Applications/Xcode. The app/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs.

Problem 3: After performing the above operations, recompiling returns to problem 1

The reason is that the AOSP build system is looking for a rule problem with the MAC SDK, and you can trick the build system by using the following methods.

Solutions:

Remove the existing macosx10.15.sdk soft link and run the following command to create a soft link named Macosx10.15.sdk for the previously copied 10.13 SDK to impersonate the 10.15 SDK. At last the problem was solved.

There is no way to create soft links by creating a surrogate in the Finder.

CD/Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs sudo ln -s MacOSX10.13. The SDK MacOSX10.15. The SDKCopy the code

Android – Running AOSP build on Mac (Yosemite and later) – Stack Overflow

Q4: Sepolicy_tests_intermediates /sepolicy_tests error

Details of the problem are as follows:

. [ 89% 64112/71309] build out/target/pr... icy_tests_intermediates/sepolicy_tests FAILED: out/target/product/generic/obj/ETC/sepolicy_tests_intermediates/sepolicy_tests ...Copy the code

The reason is that the libc++_static library has been repeatedly introduced. Remove the system/sepolicy/tests/Android. Pb in the file libc++ _static that line. A later submission fixed the problem, click to see the diff for that submission. Its changes are shown below:

Resources: Click to jump

The resources

  • Android Advanced Pointing North
  • Releases Releases · Phracker/Macosx-sdks · GitHub
  • AOSP Tsinghua University open Source software mirror station
  • Google and StackOverflow helped a lot in solving the problem. Thank you

Note: Some information has been listed in the article and will not be covered here

Copyright Notice This article is issued on wechat public account: AndroidRain synchronously sent to CSDN blog, search author QinGeneral synchronously sent to Jane book, search author QinGeneral without authorization can be reproduced, even without the need to retain the above copyright notice; Please be sure to indicate the author when reprinting.