This is the second day of my participation in the More text Challenge. For more details, see more text Challenge

The preparatory work

Configure the Android Studio

** Adjust memory size: ** Android Studio requires a large amount of memory to load The Android source code, so it is common to run out of memory and need more memory. Go to Help-> Edit Custom VM Options, such as “-xms4096m -XMx4096m”

More information:

  • The Android Studio’s official website
  • Configure the Android Studio

Configure repo

Download the repo tool

mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
Copy the code

Or use tunaGit – ‘image

And then create a working directory (whatever name you want)

Mkdir ~/aosp // Create a directory. You can define CD ~/aospCopy the code

Initialize the warehouse:

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

If you are prompted that you cannot connect to Gerrit.googlesource.com, refer to the update section of the Git-repo help page.

If you need a specific Android version (List of Android versions) :

Repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest - b android - 9.0.0 _r1Copy the code

Synchronize the source tree (just execute this command in the future) :

#Synchronize all available projects
repo sync
#Only the currently initialized branch is synchronized
repo sync -c -j4
#Only the Frameworks/Base library is synchronized
repo sync platform/frameworks/base -c -j4
Copy the code

Repo sync failure

So we need to create an automatic download script that will automatically execute repo sync if an error occurs, create a down.sh script in the aosp folder, (create a down.sh file in the aosp folder and open it with an editor, -j 8 can be changed by yourself, your total number of CPU threads *2:

 #! /bin/bash
     repo sync -j 8  
     while[$? = 1];do  
             echo"======sync failed, re-sync again======" Sleep 3 repo sync-j8done 
Copy the code

If you only need to synchronize part of the project

Replace the top two lines repo sync -j 8 with the following statement

repo sync platform/development platform/frameworks/base -j8
Copy the code

After the repo sync command, you can specify the project to be synchronized. The above command specifies two projects to open. Repo /manifest.xml, you can see the name and path of the project.

Then the command line is executed, and the download starts. (Note: Terminal is in the AOSP folder)

chmod a+x down.sh
./down.sh
Copy the code

Replace the existing AOSP source code with remote

If you already have the AOSP source code in some way (or if you just init it) and you want to synchronize the AOSP part of the code through TUNA, Just need to repo/manifest. This remote XML to the aosp fetch from https://android.googlesource.com instead https://aosp.tuna.tsinghua.edu.cn/.

<manifest>

   <remote  name="aosp"
-           fetch="https://android.googlesource.com"
+           fetch="https://aosp.tuna.tsinghua.edu.cn"
            review="android-review.googlesource.com" />

   <remote  name="github"
Copy the code

At the same time, to modify. Repo/manifests. Git/config, will be

url = https://android.googlesource.com/platform/manifest
Copy the code

Change to

url = https://aosp.tuna.tsinghua.edu.cn/platform/manifest
Copy the code

conclusion

We don’t need all the code to analyze the source code, because AOSP includes not only the system source code, but also some tool code, such as AAPT, ADB, etc., which we don’t need, and even the system source code, we don’t need to read all of it, if you do read all of it, you will never read all of it.

If you only want to download a single directory, such as we want to download the platform/frameworks/base/directory of the code, we can git clone android.googlesource.com/platform/fr… But again, there’s the problem of going over the wall, and of course we can use mirrors.

Mirror address aosp.tuna.tsinghua.edu.cn/, for instance we should download platform/frameworks/base/directory, use the git clone aosp.tuna.tsinghua.edu.cn/platform/fr… If you have enough bandwidth, you can usually download the single source code you want in a few minutes.

If you want to download a single file, or search for file names and codes, you can go to AndroidXref.com/, which has some of the Android source code

For details, see How to Read Android Framework Source Code in Android Studio

Todo www.jianshu.com/p/367f0886e… Compile the source code

Wiki.archlinux.org/index.php/A…

There are arch system to install aosputil external hard disk to ext4 format

Python2 environment switch

source.android.com/setup

Source.android.com/source/buil…

Blog.csdn.net/dd864140130…

Introduction to Android Architecture

The overall architecture

The Android system architecture is divided into five layers, which are application layer, application framework layer, system runtime layer, hardware abstraction layer and Linux kernel layer from top to bottom.

The Android source root directory describe
abi Abi: Application binary interface
art A new runtime environment that needs to be differentiated from the Dalvik VM
bionic System C library
bootable Boot boot related code
build Store system compilation rules and generic and other basic development and configuration packages
cts Android Compatibility Test Suite standard
dalvik Dalvik virtual machine
developers Developer directory
development Application development
device Device Configuration
docs Reference Documents
external Some open source modules used by Android
frameworks Application framework, the core part of the Android system, written in Java and C++
hardware The main hardware adaptation layer HAL code
libcore Core library related files
libnativehelper Support Functions for Android’s Class libraries is an acronym for Support Functions for Android’s class libraries
ndk The Android NDK (Android Native Development Kit) is a series of Development tools that allow developers to embed unmanaged code written in C/C++ in Android applications
out The compiled code is printed in this directory
packages Application package
pdk Short for Plug Development Kit, local Development Kit
platform_testing The platform test
prebuilts Precompiled resources for x86 and ARM architectures
sdk SDKS and emulators
system File systems and applications and components are implemented in C language
toolchain Toolchain file
tools Tool file
Makefile Global Makefile that defines compilation rules (usually with a.mk file suffix)

Application layer

The application layer is located at the top of the entire Android system. The applications developed by developers and the applications built in the system are in the application layer. The packages directory in the source root corresponds to the system application layer.

Packages directory describe
apps Core application
experimental Third party applications
inputmethods Input method directory
providers Content provider directory
screensavers The screen saver
services Communication service
wallpapers The wallpaper

Application framework layer part

Application framework layer is the core part of the system. On the one hand, it provides interfaces to the application layer, and on the other hand, it connects with C/C++ library and hardware abstraction layer. The main implementation code for the application framework layer is in the /frameworks/base and /frameworks/av directories

In the frameworks/base/services part of the system service, in the frameworks/av/services stored audio and camera services.

The directory structure of /frameworks/base is as follows

/ frameworks/base directory describe / frameworks/base directory describe
api Define the API cmds Important commands: am and app_proce
core Core library data Data files such as fonts and sounds
docs The document graphics Graph image correlation
include The header file keystore Related to data signing certificates
libs library location Location-related libraries
media Multimedia correlation library native The local library
nfc-extras NFC related obex Bluetooth transmission
opengl 2 d / 3 d graphics API packages Setup, TTS, VPN program
sax The XML parser services System services
telephony Telephone communication management test-runner Test Tool Related
tests Relevant test tools tool
wifi Wifi network

C/C++ library section

C/C++ libraries in system runtime layer (Native) are of various types and powerful functions. C/C++ libraries are not completely in one directory. Here give several common and more important C/C++ library directory location.

Directory location describe
bionic/ The System C library developed by Google is open source under a BSD license.
/frameworks/av/media System media library
/frameworks/native/opengl 3D graphics library/third party graphics rendering library
/frameworks/native/services/surfaceflinger Graphics display library, mainly responsible for graphics rendering, overlay and rendering functions
external/sqlite A C++ implementation of lightweight relational database SQLite
external/webp,external/webrtc Network engine library

The system runtime part

Implementation of Dalvik VIRTUAL machine: / Dalvik. Starting from Android 5.0, the default running environment of Android applications is ART (Android Runtime), and ART mode has faster and higher running efficiency. ART: /art

Hardware abstraction layer part

Hardware/libHardware: AUDIO, NFC, power implementation RIL: wireless hardware devices and telephone implementation

Read the tool

vim+ctags+cscope

Linux platform to read the source tool

Source Insight/Understand

(1) Source Insight

How to read Android source code

OpenGrok

Build a large source reading environment – using OpenGrok

Tool to use -Mac build OpenGrok view Android source code

androidxref

Android Code Search

Online, openGrok has not recommended, the current official has an online version, very good use.

Android Code Search

Android Studio

Import the AOSP source code into Android Studio

reference

Get Android source code, first source module architecture

Github asop mirror

Android system architecture and system source directory

Download AOSP Android source code (minimum and fastest download method, skip compilation and directly import android Studio) Ubuntu 16.04

Building the AndroidStudio source development environment