Framework learning: downloading AOSP source code and compiling in detail

Framework learning: downloading AOSP source code and compiling in detail

1. Preparation

Android officially announced that AOSP has always been developed and tested in Ubuntu LTS versions. So to avoid any other unnecessary troubles, please use Ubuntu and other Ubuntu-based distributions.

Minimum environmental requirements:

  • Ubuntu 14.04+
  • JDK1.8+
  • python3
  • GNU make
  • git

2. Download Aosp source code

1. Install GIT

First you need to install Git, because the source code is managed with Git.

 sudo apt - get install git

Next, create a bin folder and add it to the PATH, a bit like the Windows environment variables.

 mkdir ~/ bin
PATH =~/ bin : $PATH
Then initialize Git, email address and name.
git config --global user.email "[email protected]"
git config --global user.name "xxx"

2. Install Python environment

 sudo apt - get install python

3. Install repo and configure

repo is a python script (so we need to configure the Python environment above), because the Android source code contains hundreds of git libraries, simplifying the tools to help manage the git Android version library.

(1) Install the library downloaded by curl:

 sudo apt - get install curl

(2) Download the repo and set the executable permissions.

 curl https : // mirrors .tuna .tsinghua .edu .cn / git / git - repo > ~/ bin / repo
chmod a + x ~/ bin / repo

(3) Add download source

As for Google's AOSP, because the FQ and data volume are too large, and it requires a VPN to affect the speed, domestic mirrors are given priority (this article uses the Tsinghua source).

 export REPO_URL = 'https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

4. Initialize and synchronize source code

(1) Create a folder

Create an AOSP folder and cdd into it. You will need to download the source code here later:

 mkdir aosp
cd aosp

(2) Initialize the Aosp warehouse

 repo init - u https : // aosp .tuna .tsinghua .edu .cn / platform / manifest

(3) Initialize and specify the version

 repo init - u https : // aosp .tuna .tsinghua .edu .cn / platform / manifest - b android - 8.0 .0_r36

AOSP corresponding relationship view address: Correspondence

(4) Start synchronizing source code

 repo sync - j4
- j indicates the number of concurrent connections.

As Android source code becomes larger and larger, the probability of repo sync failure is also increasing. Therefore, we can avoid using repo sync and use the method of downloading pre-downloaded packages to download source code.

5. Pre-download package method

1. Download the pre-download package

 Download via Thunder on Windows or Linux : https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar
wget - c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar #Download the initialization package

2. Unzip the pre-downloaded package

 tar xf aosp - latest .tar
cd AOSP -> decompress the AOSP project directory. You can't see anything if you use ls at this time, because there is only a hidden .repo directory.

3. View branches

 cd .repo / manifests
git branch - a

4. Select the version you want to synchronize in the aosp directory

 repo init - b android - 9.0 .0_r55
repo sync # Synchronize normally to get the complete directory or set the branch you want to synchronize directly in the aosp directory :
repo init - u https : // aosp .tuna .tsinghua .edu .cn / platform / manifest - b android - 8.0 .0_r36
repo sync # Synchronize normally to get the complete directory if only specific modules are loaded :
repo sync platform / prebuilts / clang / host / darwin - x86

5. Prevent the script from getting stuck when downloading source code

Starting source code downloads through custom Shell scripts can effectively prevent the script from getting stuck when synchronizing source code.

(1) Create a new down.sh file in the AOSP folder

 # !/ bin / bash
echo " ======= start repo sync ======= "
cd ~/ Desktop / AOSP
repo sync - j4
while [ $ ? == 1 ] ; do
echo " ====== sync failed ! re - sync again ===== "
sleep 3
repo sync - j4

(2) Execute the down.sh file

sh down.sh

3. AOSP source code directory structure

  • abi Application Binary Interface Application Binary Interface, I believe that students have encountered abi in SO library calls. If the platform is not supported, it means that ABI is not supported.
  • art Android Runtime Android runtime. This will compile the bytecode into binary machine code in advance and save it, so it will load faster when executed. The Dalvik virtual machine compiles after loading, so ART is a little faster than Dalvik. Sacrificing space to gain time
  • Bionic is a basic library that bridges the Android system and the Linux kernel. Bionic is pronounced as bīˈänik, which means "bionic"
  • bootable system boot related programs
  • build is a tool used to build the Android system, that is, to compile the Android system
  • cts Compatibility Test Suite
  • dalvik dalvik virtual machine, a virtual machine used to parse and execute dex files
  • developers Developer Directory
  • developerment Development directory, for example, application is in it, apps
  • Devices Device-related configuration information, such as Sony, HTC, and your own products, can be defined in this directory
  • docs
  • external open source module related files
  • frameworks system architecture, the core of Android
  • hardware hal layer code, hardware abstraction layer
  • libcore core library
  • libnativehelper native helper library, implements JNI related files
  • ndk native development kit
  • out output directory, the directory generated after compilation, the relevant output is here
  • packages Application packages. Some system applications are here, such as Bluetooth, Launcher, Camera, Dialer, etc.
  • pdk Plug-in Development Kit (PDK) is designed to help you build your own pattern projects
  • platform_testing Platform Testing
  • prebuilts Precompiled files for x86/arm architecture
  • SDK software development kit
  • system underlying system files
  • toolchain
  • tools tool files
  • Makefile mk file, used to control compilation

4. AOSP source code compilation

Compiling AOSP source code requires configuring the JAVA environment.

1. Install the Java compilation environment

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

2. Enter the AOSP folder and compile

(1) Initialize the compilation environment

 source build / envsetup.sh

(2) Delete the results of the previous compilation. This step is not required for the first compilation.

 make clobber

(3) Select the compiled version corresponding to the device

 lunch XX

Select the compilation version corresponding to the device. For example, compile the development engineer version lunch aosp_x86-eng, which is convenient for debugging

Compilation version selection

If the lunch command does not have a corresponding compiled version, the following information will be output:

 You 're building on Linux
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. full_fugu-userdebug
8. aosp_fugu-userdebug
9. mini_emulator_arm64-userdebug
10. m_e_arm-userdebug
11. m_e_mips64-eng
12. m_e_mips-userdebug
13. mini_emulator_x86_64-userdebug
14. mini_emulator_x86-userdebug
15. aosp_dragon-userdebug
16. aosp_dragon-eng
17. aosp_marlin-userdebug
18. aosp_sailfish-userdebug
19. aosp_flounder-userdebug
20. aosp_angler-userdebug
21. aosp_bullhead-userdebug
22. hikey-userdebug
23. aosp_shamu-userdebug
Which would you like? [aosp_arm-eng]

Here you need to select the format of the compilation target (select the previous number and press Enter). The format of the compilation target is composed of BUILD-BUILDTYPE. For example, the BUILD of aosp_arm-eng is aosp_arm and the BUILDTYPE is eng. BUILD indicates the environment in which the compiled image can run, aosp stands for the Android open source project, and arm means that the system runs on an arm architecture processor;

BUILDTYPE refers to the compilation type, which has the following three types:

  • user: used to officially release the version to the market, with limited permissions. For example, if you do not have root permissions, you cannot dedug, and adb is disabled by default.
  • userdebug: Based on the user version, root and debug permissions are opened, and adb is enabled by default. It is generally used to debug real devices.
  • eng: Version for developers, with the highest permissions (root, etc.), and development configuration with additional debugging tools. Generally used for simulators.
  • If you don't have a Nexus device and just want to run it on the simulator after compiling, you can select aosp_x86 for BUILD and eng for BUILDTYPE.

(4) Start compiling

 make - j8

The number after j is the number of threads, which shall not exceed the total number of threads on your CPU.

A successful compilation will display as follows:

 Creating filesystem with parameters :
Size : 2147483648
Block size : 4096
Blocks per group : 32768
Inodes per group : 8192
Inode size : 256
Journal blocks : 8192
Label : system
Blocks : 524288
Block groups : 16
Reserved block group size : 127
Created filesystem with 2216 / 131072 inodes and 199826 / 524288 blocks
[ 100 % 7669 / 7669 ] Install system fs ima... / target / product / generic_x86 / system .img
out / target / product / generic_x86 / system .img + maxsize = 2192446080 blocksize = 2112 total = 2147483648 reserve = 22146432
#### make completed successfully ( 01 : 24 : 41 ( hh : mm : ss ) ) ####

The image file will be generated in the source code directory out/target/product/angler:

  • system.img: system image
  • ramdisk.img: root file system image
  • userdata.img: user data image
  • recovery.img:recovery image
  • boot.img: boot image
  • vendor.img: driver image

Finally, three important image files will be generated in the out/target/product/generic_x86/ directory: system.img, userdata.img, and ramdisk.img. Here is a brief introduction to the three image files:

  • system.img: system image, which contains the main directories and files of the Android system. It is parsed by init.c and mounted to the /system directory.
  • userdata.img: user image, which stores user data in the Android system. It is parsed by init.c and mounted to the /data directory.
  • ramdisk.img: root file system image, including some important files for starting the Android system, such as init.rc.

3. Compilation error resolution

(1) libncurses.so.5 is missing

Error message:

 error while loading shared libraries : libncurses .so .5 : cannot open shared object file : No such file or directory

Solution:

 sudo apt - get update
for 32 - bit binaries :
sudo apt -get install libncurses5 : i386
for 64 - bit binaries :
sudo apt - get install libncurses5

(2) Lack of M4

Error message:

 / bin / bash : m4 : command not found

Solution:

 sudo apt - get install m4

(3) Remove all localization settings

Error message:

 FAILED : out / host / linux - x86 / obj / EXECUTABLES / checkpolicy_intermediates / policy_scan.c

Solution:

 export LC_ALL = C
LC_ALL = C is to remove all localization settings so that the command can be executed correctly, but ~/ .bashrc cannot be modified, which will cause Chinese characters in the terminal to be displayed as numbers ( which should be the corresponding encoding )

(4) Problems with xmllint

Error message:

 / bin / bash : xmllint : command not found

Solution:

 sudo apt - get install libxml2 - utils

(5) Insufficient compilation memory

Error message

 Try increasing heap size with java option '-Xmx<size>' error

Solution:

 export JACK_SERVER_VM_ARGUMENTS = "-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
jack - admin kill - server
jack - admin start - server

5. Run the simulator

Start the simulator

After the compilation is complete, you can run the Android virtual machine with the following command. Since the source and lunch commands have been executed before, you can run them directly:

 source build / envsetup.sh
lunch aosp_x86 - eng
emulator

The simulator will start

6. Compile a single module from AOSP source code

The above compilations are all for the entire Android system. If we want to compile the system's Settings application module, this belongs to the source code single compilation of a module.

Execute in the AOSP root directory:

 source build / envsetup.sh
lunch aosp_x86 - eng

Enter the Settings directory:

 cd packages / apps / Settings

Compile the modules in the current directory through mm, and do not compile dependent modules.

mm

After the compilation is successful, there will be a prompt to generate the file storage path. In addition to the Settings.odex file, Settings.apk will also be generated in the out/target/product/generic_x86/system/priv-app/Settings directory.

In addition, there are the following commands for single editing:

  • mmm: Compile the modules in the specified directory without compiling other modules it depends on.
  • mma: Compile the modules in the current directory and their dependencies.
  • mmma: compile all modules in the specified path and include dependencies.

If you modify the source code of the system module, there are two ways to view the generated APK file:

  • Install the APK via adb push or adb install.
  • Use the make snod command to regenerate system.img and run the simulator to view


<<:  Detailed explanation of R8 in Android

>>:  iOS 16.4 Quick Security Response Update

Recommend

Can smart projectors bypass radio and television supervision?

Previously, the State Administration of Radio, Fi...

Case | Marketing suggestions for 6 types of mobile financial users

Based on the research and analysis answers given ...

The sixth episode of Aiti Tribe Clinic: How does machine learning judge emergencies?

【51CTO.com original article】 Activity description...

Tips for recording notes on Xiaohongshu!

To understand the issue of note collection, just ...

JD.com: Technical exchanges spark innovation

On August 8, the first JD Technology Salon was he...

The Arctic is so cold, can it catch fire?

Is the Arctic on fire? Author: Fan Gang You may f...

Information flow advertising placement case in the wedding industry!

I believe everyone is familiar with information f...

How to build a user operation system?

However, with the rapid expansion of product scal...