1. The requirements

1.1 hardware

  • 160 GB of available disk space.

I compiled the Android 9.0 source code, downloaded down nearly 40G, compiled 153G, so at least 160G available disk space.

The available disk space needs to be allocated at least 80G.

Note: During compilation, if the compilation fails due to insufficient disk space, you can resize the disk and continue the compilation at the previous pace, so here is a personal compilation version, but at least make sure that the available disk is not too small.

1.2 software

1.2.1 Compiled Android version and required Mac OS version
  • Android 6.0 (Marshmallow) – AOSP Master: Mac OS V10.10 (Yosemite) or later with Xcode 4.5.2 and command line tools
  • Android 5.x (Lollipop) : Mac OS V10.8 (Mountain Lion) with Xcode 4.5.2 and command line tools
  • Android 4.1.x-4.3.x (Jelly Bean) – Android 4.4.x (KitKat) : Mac OS V10.6 (Snow Leopard) or Mac OS X V10.7 (Lion), and Xcode 4.2 (Apple developer tools)
  • Android 1.5 (Cupcake) – Android 4.0.x (Ice Cream Sandwich) : Mac OS V10.5 (Leopard) or Mac OS X V10.6 (Snow Leopard), along with the Mac OS X V10.5 SDK
1.2.2 the JDK
  • Android 7.0 (Nougat) – Android 8.0 (O) : JDK 8U45 or later
  • Android 5.x (Lollipop) – Android 6.0 (Marshmallow) : JDK-7U71-Macosx-x64.dmg
  • Android 2.3.x (Gingerbread) – Android 4.4.x (KitKat) : Java JDK 6

2. Preparation

2.1 Creating a disk image with partition case

Mac OS runs on a case-insensitive file system by default, but since Git doesn’t support such a file system, you need to set up a case-sensitive disk partition on Mac OS. Here we use the command line to create, one is more convenient, two is later extensible.

Enter the following command at the terminal to create a 160 GB disk image:

$ hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 160g ~/android.dmg
Copy the code

An Android.dmg. sparseImage file will be generated in the current user directory, which can be simply understood as the file is the driver required to compile the source code.

You are advised to set the partition size to 160 GB. If the partition size is insufficient, run this command to adjust the partition size. For example, run the following command to change the partition size to 200 GB:

$ hdiutil resize -size 200g ~/android.dmg.sparseimage
Copy the code

Note: Commands to resize partitions take effect only when the disk image of the partition has been unmounted.

2.2 Adding commands and functions for mounting and unmounting disk partitions

To facilitate the loading and unloading of the partitioned disk image, we can add the following functions to the ~/.bash_profile file:

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

To load the partition disk image, run the following command to run /Volumes/android:

$ mountAndroid
Copy the code

Again, the uninstalled function is as follows, which we also added to the ~/.bash_profile file:

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

To uninstall the partition disk image, run the following command:

$ umountAndroid
Copy the code

2.3 Installing Xcode and Other Software Packages

  1. Install Xcode command line tool:
$ xcode-select --install
Copy the code
  1. To install Xcode, go directly to the AppStore.

  2. Install MacPorts or Homebrew. MacPorts and Homebrew are software package management tools that can be used to install, update, and uninstall software packages directly from terminals. Install gnupg via MacPorts and Homebrew.

  3. Importing the path in the ~/.bash_profile file allows you to use port or brew commands to manage packages:

 export PATH=/opt/local/bin:$PATH
 export PATH=/usr/local/bin:$PATH
Copy the code
  1. Install gmake, libsdl, git, and gnupg
  • If MacPorts is used, run the following command:
$ POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg
Copy the code
  • If you are using Homebrew, run the following command:
$ brew install gmake libsdl git gnupg2
Copy the code

2.4 Downloading the Mac OS SDK

Due to a new Mac OS update (here is the 10.14.4 Mojave version), Android source code may be compiled with the following error:

system/core/libcutils/threads.c:38:10: error: 'syscall' is deprecated: first deprecated in OS X 10.12 - syscall(2) is unsupported; please switch to a supported interface. For SYS_kdebug_trace use kdebug_signpost(). [-Werror,-Wdeprecated-declarations]
    return syscall(SYS_thread_selfid);
Copy the code

Therefore, you need to download an earlier version of MacOS SDK. You are advised to download MacOSx10.11.sdK.tar. xz, decompress it to the customized directory, and create a soft link. For example, if I put it in the ~/lib directory and create a soft link to it, it will not be deleted in the next Mac OS or Xcode upgrade:

$ sudo ln -s~ / lib/MacOSX10.11. The SDK / Applications/XCode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX10.11. The SDKCopy the code

2.3 Setting the maximum number of file descriptors

In Mac OS, the default upper limit for the number of file descriptors that can be opened at the same time is too low and may be exceeded in a highly parallel compilation process. To increase this limit, add the following line to ~/.bash_profile:

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

At this point, everything is ready, the next is the process of downloading the source code.

3. Download the source code

3.1 illustrates

See the Google tutorial here. Because of the wall, even if we as technicians can climb over, there may still be various problems in use, so here we can download through the mirror site of Tsinghua University.

3.2 Downloading and Installing the Repo Tool

Because Android source code is huge and complex, so Google specially developed Repo to manage the Android source library, here is not introduced, interested in reading the Repo tool introduction.

The official advice is to first have a bin/ directory in the home directory and include that directory in the path:

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

Then download the Repo tool and make sure it executes:

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

3.3 Initializing the Repo Client

In section 2.2, we used the mountAndroid command to load our partitioned disk image to /Volumes/ Android, so we needed to initialize it in that directory:

Switch to this directory and create a working directory for aOSP:

$ cd /Volumes/android/
$ mkdir aosp
$ cd aosp
Copy the code

The current source directory is /Volumes/ Android /aosp.

While Repo is running, it will try to access the official Git source to update itself. If you want to use tsinghua’s image source to update yourself, you can copy the following contents into your ~/.bashrc(if you do not have this file, create one) :

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

Then configure git with your name and email:

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

Run repo init to initialize the master branch:

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

To validate branches other than master, use -b to specify the corresponding branch. To see a list of branches, see source code tags and versions. I specified the Android-9.0.0_R35 branch here:

$repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest - b android - 9.0.0 _r35Copy the code

3.4 Downloading the Android Source tree

Enter the following command on terminal to download the source code:

$ repo sync
Copy the code

Because downloading the source code takes a long time, it’s best to check “Prevent sleep” under “System Preferences” > “Save” on your Mac, as shown below:

The next is a long waiting process, more than 30 G, bandwidth if you can, almost less than two hours to complete the download, will prompt:

Syncing work tree: 100%(xxx/xxx), done.
Copy the code

4. Compile

Run /Volumes/ Android /aosp to compile the Volumes.

4.1 switch bash

Since only bash can be used for Android compilation, if you are using a shell such as ZSH, you need to switch to bash.

$ chsh -s /bin/bash
Copy the code

Then restart the terminal to take effect.

4.2 Environment Settings

Use the envsetup.sh script to initialize the environment:

$ source build/envsetup.sh
Copy the code

4.3 Selecting a Compilation target

Enter lunch and the following prompt will be displayed:

$ 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
     11. mini_emulator_arm64-userdebug
     12. m_e_arm-userdebug
     13. m_e_mips-userdebug
     14. m_e_mips64-eng
     15. mini_emulator_x86-userdebug
     16. mini_emulator_x86_64-userdebug
     17. uml-userdebug
     18. aosp_crosshatch-userdebug
     19. aosp_blueline-userdebug
     20. aosp_cf_x86_auto-userdebug
     21. aosp_cf_x86_phone-userdebug
     22. aosp_cf_x86_tablet-userdebug
     23. aosp_cf_x86_tablet_3g-userdebug
     24. aosp_cf_x86_tv-userdebug
     25. aosp_cf_x86_wear-userdebug
     26. aosp_cf_x86_64_auto-userdebug
     27. aosp_cf_x86_64_phone-userdebug
     28. aosp_cf_x86_64_tablet-userdebug
     29. aosp_cf_x86_64_tablet_3g-userdebug
     30. aosp_cf_x86_64_tv-userdebug
     31. aosp_cf_x86_64_wear-userdebug
     32. cf_x86_auto-userdebug
     33. cf_x86_phone-userdebug
     34. cf_x86_tablet-userdebug
     35. cf_x86_tablet_3g-userdebug
     36. cf_x86_tv-userdebug
     37. cf_x86_wear-userdebug
     38. cf_x86_64_phone-userdebug
     39. cf_x86_64_tablet-userdebug
     40. cf_x86_64_tablet_3g-userdebug
     41. cf_x86_64_tv-userdebug
     42. cf_x86_64_wear-userdebug
     43. aosp_marlin-userdebug
     44. aosp_marlin_svelte-userdebug
     45. aosp_sailfish-userdebug
     46. aosp_walleye-userdebug
     47. aosp_walleye_test-userdebug
     48. aosp_taimen-userdebug
     49. hikey-userdebug
     50. hikey64_only-userdebug
     51. hikey960-userdebug

Which would you like? [aosp_arm-eng] 
Copy the code

All of the compiled targets are listed here. All BUILD targets take the form of build-buildType, where BUILD is a code name for a particular combination of functionality, and BUILDTYPE is one of the following types that indicate what environment to run in:

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

For example, aoSP_ARM-eng BUILD is AOSP_ARM, BUILDTYPE is ENG, AOSP stands for Android open source project, ARM means that the system is running on the ARM architecture processor, more see official documentation.

If you have a Pixel or Nexus real machine, select the corresponding build target, otherwise we will choose the emulator, i.e. 5.AOSP_x86-eng. Which would you like? Enter 5 after [AOSP_ARM-eng]. Also directly choose:

$ lunch 5
Copy the code

or

$ lunch aosp_x86-eng
Copy the code

4.4 Compiling Code

Before compiling, check the number of CPU cores on your Mac by typing:

$ sysctl -n machdep.cpu.core_count
4
Copy the code

If the number of CPU cores is 4, 4 threads can be started to compile the source code:

$ make -j4
Copy the code

The following is a long compilation process, which usually takes 2 to 3 hours. If you see the following message, the compilation is complete:

#### build completed successfully (02:15:52 (hh:mm:ss)) ####
Copy the code

4.5 Running the Emulator

After compiling, the emulator can run the compiled version by typing the following command in the terminal:

$ emulator
Copy the code

5. Android Studio import source code

5.1 Compiling the Idegen Module

After compiling, run the following command to compile the IDEgen module:

$ mmm development/tools/idegen/
Copy the code

After the idegen module is compiled, the following message will be displayed:

#### make completed successfully (46 seconds) ####
Copy the code

In this case, run the following command to generate the corresponding android. Ipr and Android. Iml IDEA project configuration files in the aOSP root directory

$ development/tools/idegen/idegen.sh

Read excludes: 17ms
Traversed tree: 218013ms
Copy the code

5.2 Importing Source Code

Start Android Studio, select an existing Android Studio project, and then select the android.ipr file in the aOSP root directory. If you are prompted to convert, confirm convert. The initial import takes about 20 to 30 minutes:

After the import is successful, in Project Structure, select Modules and remove all dependencies, leaving only the ones shown below. This way, when viewing the source jump, you will not enter android.jar, but directly jump to the project file.

Now you can read the source code freely, enjoy it!

reference

Download, compile, and import the Android source code (AOSP) on macOS (Sierra 10.12) to Android Studio Android AOSP base (3) Android system source code integrated and single edition using Android Studio import source code