I just graduated from school a few years ago, and I came into contact with AOSP when I was involved in the development of Launcher in my old employer. At that time, the early education tablet was still customized based on Android 4.4 source code. After running away, I have been fishing in the application layer. Recently, I want to review some basic postures, so this article is here

Tips: This article is not my own practice, it is based on the article of several big heads in the Reference department. Asop source code download time, the author does not compile and write source code requirements, just to solve the BUG or understand the underlying mechanism to look at the source code, SDK comes with android.jar and some online source sites have been enough. The process of setting up the environment is basically the same, use search engines when you encounter problems


0 x1, nouns

1) AOSP

Chinese website: source. Android. Google. Cn /

Android Open Source Package, Android system Open Source Package, Android mobile terminal platform (Qualcomm, MTK, etc.) based on the original Android code to change, to form their own platform code, Other handset vendors then provide their own mobile solutions (system customization) based on the platform code.

Asop source code is compiled to produce a series of artifacts (in the OUT directory) :

  • /out/host → Android development tools related products, including various SDK tools, such as ADB, Dex2OAT, AAPT, etc.;
  • /out/target/common → Some generic compilation artifacts, including Java application code and Java libraries;
  • /out/target/product/[product_name] → Platform-specific C/C++ code and binaries (e.g. System.img, ramdisk.img, userdata.img, boot.img, etc.);

Android. jar is a classes.jar file. The out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes. The jar. In some previous scenarios, it was necessary to use the API hidden in the SDK (@hide annotation). One solution was to replace the Android.jar in the native SDK with the classes.jar generated by aOSP compilation. Android Studio 3.5 import classes.jar from AOSP

(2) the Gerrit

Android uses Git as a code management tool and has developed Gerrit for code review to better centrally manage code. Essentially a Git server that provides a set of permission controls for the Git repository hosted on its server, as well as a Web page for Code Review.

3. ‘

Repo command line tool, using Python to encapsulate Git commands, simplifying the centralized management of multiple Git libraries.

0x2. Repo workflow

The diagram below:

Brief description of key commands:

  • Repo init → Initialize the repo in the current directory and generate a.repo directory, which contains a manifest.xml file that lists how each project is cloned (repository address, mapping to workspace address, branch mapping, etc.);
  • Repo sync → Synchronize all projects to local workspace;
  • Repo start → Create and switch to local work branch;
  • Git related commands → local modification of some Project code, regular submission;
  • Repo Upload → Publish code changes to the audit server;
  • Repo Prune → After feature development, merge, safely remove outdated theme branches;

See source Control Workflow for more details.

0x3. System Preparations

You can download the source code on any system, but if you want to compile it, you need Linux or Mac OS.

If You want to compile for Windows, you can do this indirectly by installing the above system on a VIRTUAL machine, the usual way: Docker or VirtualBox. The latter installation example can be seen: “Android AOSP foundation (a) VirtualBox installation Ubuntu”

See Building a Build Environment for more details.

0x4 download the source code

Run a wave of the following command ~

Install Git, Curl, and Python
sudp apt-get install git
sudo apt-get install curl
sudo apt-get install python

Create bin and add it to PATH
mkdir ~/bin
PATH=~/bin:$PATH

Download the Repo tool and make it executable
# Tips: foreign source may have a problem, can be replaced by domestic mirror, such as: https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

Create a working file directory
mkdir aosp
cd aosp

While running, Repo will try to update itself with an official Git source. If you want to update it with a tuna image, you can copy it
# Add the following content to ~/.bashrc
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

# ⑤ Git set identity (name, email)
git config --global user.name  "User Name"
git config --global user.email "[email protected]"

# ⑥ Initialize the repository (also support to replace the mirror source)
repo init -u https://android.googlesource.com/platform/manifest

# also supports the specified branch, optional value view: https://source.android.com/source/build-numbers#source-code-tags-and-buildsRepo init -u https://android.googlesource.com/platform/manifest - b android - 9.0.0 _r8Sync code (drag aOSP source code to working directory, usually several hours)
repo sync

# Tips: Some projects may not be found during the download process, which may be due to mirroring problems or network problems.
-jn, -j4
repo sync -c platform/frameworks/layoutlib

Asop does not include kernel code. If necessary, you can download it separately. There are many versions, such as:
Android emulator kernel, MSM, Qualcomm MSM chip
mkdir kernel
cd kernel
git clone https://aosp.tuna.tsinghua.edu.cn/kernel/goldfish.git
cd goldfish
See which kernel version branches are available for download
git branch -a
Download the kernel code of the corresponding versionGit checkout remotes/origin/android - goldfish - 3.4Copy the code

See Download Source code for more details.

In addition, foreign images may be unstable. You can also use Tsinghua image directly, because the first synchronization may fail. You are advised to download the initialization package aosp-latest.


0 x 5, compile

Again, here are some nouns:

  • Makefile → Android platform compilation system, with Makefile written out of an independent project, define the compilation rules, automatic compilation, will be scattered in hundreds of Git library code integration, unified compilation, and the product classification output to a directory, packaged into mobile ROM, You can also generate SDK and NDK for application development.
  • Android.mk → define the necessary parameters of a module, so that the module is compiled with the platform, simply speaking, it is to tell the system to compile the source code with what rules, and generate the corresponding target file;
  • Kati → Google’s special Android widget, based on Golang and C++, does: convert Android makefiles into Ninja files
  • Ninja → a small compilation system dedicated to speed, think of makefiles as a high-level language, which is assembly, with the file suffix.ninja;
  • Android.bp → Replace the configuration file of Android.mk;
  • Blueprint → Parse the Android.bp file into Ninja syntax;
  • Soong → Makefile is a replacement for compiling systems, parsing android.bp files and converting them to Ninja files;

Relationship description:

  • As Android projects get bigger and makefiles take longer to compile, Android 7.0 introduced Ninja for faster and more efficient parallelization.
  • Soong parses the Android. Bp syntax defined by Blueprint and converts it to Ninja files.
  • Makefiles (.make or.mk) are converted to Ninja files (.ninja) via kati;
  • Makefiles are designed to be written by developers, while Ninja is designed to be generated by other programs, similar to high-level and assembly languages.

1. Prepare the compilation environment

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

# Tips: To use Ubuntu 14+, you also need to install the following dependency packages
sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip
Copy the code

② Source code consolidation

Initialize the environment
source build/envsetup.sh

# 2 Delete out and intermediate files, clean will delete this configuration generated, clobber will delete all configuration generated
make clobber

Select the compile target, the following command will enter the menu, select the corresponding version, enter the serial number press Enter
BUILDTYPE is one of the following types:
* * * * * * * * * * * * * * * * * *
# userdebug → Similar to user, but with root permissions and debug permissions, generally used for debugging the real machine.
# eng → Development configuration with additional debugging tools, with maximum permissions (root, etc.), generally used for emulators.
> Pixel 3A XL > AOSP_bonito -userdebug
lunch

You can also specify the compile target directly, for example, lunch aosp_bonito-eng
# You can also use the serial number directly, such as lunch 8, but it is not recommended, because different system versions may have different serial numbers ~

The -jn parameter is used to set the number of parallel tasks to be compiled. The number of CPU cores should be 6, and the N value should be 6-12
# change the number of CPU cores to 32
make -j6

# can also print output to the log file: make - j6 2 > &1 | tee build_20211206_1403. The log

After the success of the # compiler generates out directory, such as: here ~ / aosp/out/target/product/bonito

If necessary, you can also type: make SDK, compile SDK to generate the modified Android.jar
Copy the code

③ source code sheet

Reprogramming usually takes several hours, and sometimes it just involves modifying one of the application modules. You only need to compile the module separately.

source build/envsetup.sh
lunch aosp_bonito-eng
Go to the module directory
cd package/apps/Setting

The following are optional instructions for compiling individual modules:
# mm → compile modules in the current directory, not dependent modules
# MMM → compile modules in the specified directory without compiling dependent modules
# mMA → Compile modules and their dependencies in the current directory
# mmmma → compile all modules in the specified path, excluding dependencies
mm

Setting. Odex will be generated as well
Apk: adb push/ADB install
You can also use the make snod command to repackage the system. Img file and run the emulator to view it
Copy the code

0 x6, flash

① Your son Pixel or Nexus

You can download the Driver Binaries for Nexus and Pixel Devices based on your model and Android version. Then unzip and execute:

./extract-qcom-sargo.sh
./extract-google_devices-sargo.sh
Copy the code

Enter the bootloader and run the burn command (-w stands for clearing data).

adb reboot bootloader
fastboot flashall -w
Copy the code

Then wait for the burn to complete

② Android VM

You have to build your own AVD image, choose sdk_phone_x86_64 build target for lunch, and attach SDK and SDk_repo package in addition to regular build.

If the above method does not work, Copy the image to one of the SDK/system-images/android-xx/ directories.

③ Other mobile phones

Not a pro son, drive, manufacturer library what, can only find a third party ROM for secondary development, before the most famous CM (CyanogenMod), but it seems cool, you can try LineageOS

Limited to space, I will describe one by one, interested in trying their own can refer to: “Diy compile Android(LineageOS) source code”

Other Ways to view Android source code

In addition to directly downloading the full source code locally, you can view it directly online, or directly through AS.

① Online Viewing

Don’t need a pile of code, and multiple versions pick to see, very fragrant ~

  • Cs.android.com (official, fast, may require scientific Internet access)
  • Platform_frameworks_base (Github)
  • Googlesource (very full, suitable for a single module direct Git clone)
  • AndroidXref (a little old, latest version to Android 9)

② AS Quick check

Click Settings → Appearance&Behavior → System Settings → Android SDK → Select the required Android SDK version

Then build. Gradle set the compileSdk version, Sync it and click on the jump Framework class to jump to the compileSdk.

0x8, Debug Android system source code

① Creating an index

Android source code provides direct generation of AS identifiable file tools, after the compilation of Android source code, will generate an android.ipr in the source root directory.

Open the file AS Open an Existing Android Studio Project and wait until the index is generated.

After the index is generated, adjust the directory for AS monitoring source code, such AS the out directory, which will change after each compilation. However, this directory will not be used basically, so indexes should be eliminated directly. Take the Out directory AS an example, and other unnecessary directories should be handled in the same way:

② Set the JDK and SDK

Select the corresponding SDK according to the source version:

Set the JDK:

③ Debug source code

Open the breakpoint and click Attach Debugger to Android Process

Select the process you want to debug, and you can debug it happily

Debuggable = 1, you can select a 64-bit image that is not Google Play when creating an emulator, or you can get a device that is ro.debuggable = 1.

Most of the time debugging source code is not all the AOSP source, you can also directly import part of the source code for debugging.

In addition, the real machine debugging, there may be adjusted trial number and source inconsistent, line number run notes, most of the reason is the domestic ROM on the relevant source code modified, you can see the call method in the Debugger/Frames, which method to understand this step into, and then Ctrl+F to find the method name, positioning to the correct position.

0 x9 and summary

This section is a quick review of the basic AOSP posture to prepare for the learning of the Framework, and we will learn more about it later


References:

  • Liu Wangshu: AOSP foundation

  • Android System Basics

  • [Toss Framework] Opening: source code compilation and burning

  • Build Android source code debugging environment from scratch

  • Android Studio views and debugs AOSP source code

  • There are two easy ways to read Android source code