1. Configure the Mac OS compilation environment

1. Create a disk mirror that is case-sensitive

The Mac OS file system is case-sensitive but case-sensitive by default. Git does not support such file systems. So compiling Android source code in Mac OS requires creating a case-sensitive disk image.

You can create a case-sensitive disk image directly by running the following command:

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

Executing the above command will create an Android. DMG or android.dmg.sparseimage file that can be mounted as a case-sensitive mirror partition.

-size sets the partition size. At least 25GB is required to compile Android source code. To adjust the size of the mirror partition, run the following command:

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

You can add helper functions to the ~/.bash_profile (or ~/.zshrc if it is ZSH) file to perform mount and unmount:

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

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

Note: If.dmg. sparseImage is generated during disk image creation, replace ~/android. DMG with ~/ android.dmg.sparseImage.

After that, use the mountAndroid command to mount the image, and use the umountAndroid command to unmount the image.

2. Install related tools

2.1 to install the JDK

JDK requirements for different Android versions:

  • Master branch of Android in AOSP: 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

JDK 8U45 or later is required for Android 7.1.1.

2.2 Installing required Applications

To compile the source code for Android 7.1.1, you need Mac OS V10.10 (Yosemite) or later.

  • Xcode 4.5.2 and command line tools

  • Python 2.6 to 2.7

  • GNU Make 3.81-3.82 (For Android 3.2.x (Honeycomb) and later, you need to restore Make 3.82 to a later version to avoid compilation errors)

  • Git 1.7 or later

  1. Install the Xcode command line tool

Use the following command to install the Xcode command-line tool:

xcode-select --install
Copy the code
  1. Install MacPorts

Download and install MacPorts from macports.org.

Then add to the ~/.bash_profile (or ~/.zshrc if it is ZSH) file:

export PATH=/opt/local/bin:$PATH
Copy the code
  1. Install Make, Git, and GPG using MacPorts

Run the following command to complete the installation:

POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg
Copy the code

3. Set the upper limit 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.

So to increase this limit, add the following to the ~/.bash_profile file:

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

Note: it is recommended to add it in ~/.bash_profile, because compiling the Android source code only supports bash, and using ZSH or another shell may cause errors.

Download the source code

1. Install the Repo

To work with Android code, you need to use both Git and Repo, and using Repo to perform basic cross-network operations simplifies things.

Repo is a Git-based code base management tool. Repo can integrate multiple Git code bases when necessary, upload related content to our revision control system, and automate parts of the Android development workflow. Repo is not intended to replace Git, but to make it easier for you to use Git in an Android environment. See the Development section for more information.

  1. Create a bin/ directory in your home directory and add it to your PATH:
mkdir ~/bin
PATH=~/bin:$PATH
Copy the code

Note: The Repo command is an executable Python script that you can place anywhere in the path.

  1. Download the Repo tool
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
Copy the code
  1. Ensure that the Repo is executable
chmod a+x ~/bin/repo
Copy the code

2. Initialize the Repo client

  1. Create a directory

Create a directory in the case-sensitive partition you created earlier to store Android source code with your own name:

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY
Copy the code

To mount partitions using the mountAndroid command, run the /Volumes/android/ directory

  1. Initialize the

Use repo init to get the latest version of the repO and patches, -u to specify where in the working directory each code base contained in the Android source code will be located, and -b to specify the corresponding system version branch. For a list of branches, see source code tags and versions.

Get the Android 7.1.1 version branch:

Repo init -u https://android.googlesource.com/platform/manifest - b android - 7.1.1 _r26Copy the code

After successful initialization, a.repo directory is generated in which files such as lists are stored.

3. Download Android source code

Run the following command to download the Android source code:

repo sync
Copy the code

Note: It is recommended to download or use a mirror, such as tsinghua’s mirror:

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

Compile the source code

Switch shell

Compiling the Android source code only supports bash; if you are using another shell, you need to switch to bash first.

There are two switching methods:

  • Temporary switch:

    After the terminal is restarted, the terminal fails. To switch, enter the shell name directly on the terminal:

    /bin/bash
    Copy the code
  • Change the default shell:

    The switch takes effect after the terminal is restarted. The switchover method is as follows:

    chsh -s /bin/bash
    Copy the code

To view the current shell, run the following command:

echo $0
Copy the code

1. Set the environment

Initialize our build environment with the envsetup.sh script file in the source build directory.

Run the following command:

source build/envsetup.sh
Copy the code

or

. build/envsetup.sh
Copy the code

Execution Result:

including device/asus/fugu/vendorsetup.sh
including device/generic/mini-emulator-arm64/vendorsetup.sh
including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh
including device/generic/mini-emulator-mips/vendorsetup.sh
including device/generic/mini-emulator-mips64/vendorsetup.sh
including device/generic/mini-emulator-x86/vendorsetup.sh
including device/generic/mini-emulator-x86_64/vendorsetup.sh
including device/google/dragon/vendorsetup.sh
including device/google/marlin/vendorsetup.sh
including device/htc/flounder/vendorsetup.sh
including device/huawei/angler/vendorsetup.sh
including device/lge/bullhead/vendorsetup.sh
including device/linaro/hikey/vendorsetup.sh
including device/moto/shamu/vendorsetup.sh
including sdk/bash_completion/adb.bash
Copy the code

2. Pick a target

Use Lunch to select the target to compile.

Executing the lunch command displays the optional build target type:

lunch
Copy the code

Execution Result:

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. full_fugu-userdebug 8. aosp_fugu-userdebug 9. mini_emulator_arm64-userdebug 10. m_e_arm-userdebug 11. m_e_mips-userdebug  12. m_e_mips64-eng 13. mini_emulator_x86-userdebug 14. mini_emulator_x86_64-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]Copy the code

BUILD targets are in the form of build-build type, where BUILD is a code name for a particular combination of functionality.

BUILDTYPE is one of the following types:

Compile type usage
user Limited authority; Suitable for a build 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

Development use, the general choice – ENG.

Because you are going to run the compiled source using an emulator and the CPU is x86, choose 5. aoSP_x86-eng or 6. aoSP_x86_64-eng.

Run the following command:

lunch aosp_x86-eng
Copy the code

Execution Result:

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = PLATFORM_VERSION_CODENAME = REL PLATFORM_VERSION = 7.1.1 TARGET_PRODUCT=aosp_x86 TARGET_BUILD_VARIANT=eng TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=x86 TARGET_ARCH_VARIANT=x86 TARGET_CPU_VARIANT= TARGET_2ND_ARCH= TARGET_2ND_ARCH_VARIANT= TARGET_2ND_CPU_VARIANT= HOST_ARCH=x86_64 HOST_2ND_ARCH=x86 HOST_OS= Darwin HOST_OS_EXTRA=Darwin 16.6.0-x86_64-i386-64bit HOST_CROSS_OS= HOST_CROSS_ARCH= HOST_CROSS_2ND_ARCH= HOST_BUILD_TYPE=release BUILD_ID=NOF27C OUT_DIR=out = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Copy the code

3. Compile source code

To compile the source code using the make command in the source path, you can set the parallel tasks with the -jn parameter, where N is the number of parallel tasks, usually between 1 and 2 times the number of CPU threads.

Here we use the following command:

make -j4
Copy the code

After successful compilation, the following message is displayed:

#### make completed successfully (02:51:05 (hh:mm:ss)) ####
Copy the code

4. Run the emulator

After compiling, execute emulator command to run emulator:

emulator
Copy the code

The first startup takes a long time.

4. Problems encountered in compilation

1. Can not find the SDK at 10.8 / Developer/SDKs/MacOSX10.8 SDK

The build/core/combo/mac_version.mk file contains the following Settings:

Mac_sdk_versions_supported := 10.8 10.9 10.10 10.11Copy the code

This shows the supported Mac SDK versions.

Because the computer runs MacOS Sierra 10.12, In the/Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs directory only MacOSX10.12. SDK, so there will be an error.

Solutions:

Some solutions online are to modify the configuration in the mac_version.mk file to support version 10.12:

Mac_sdk_versions_supported := 10.8 10.9 10.10 10.11 10.12Copy the code

Error: ‘syscall’ is deprecated when compiling source code. This is because syscall was deprecated in version 10.12.

So you cannot change the configuration in mac_version.mk, It needs to be in the/Applications/Xcode. The app/Contents/Developer/Platforms/MacOSX. Add MacOSX10.11 platform/Developer/SDKs directory. The SDK.

In github.com/phracker/Ma… Download MacOSX10.11. The SDK. And copied to/Applications/Xcode. The app/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs directory, can be solved.

2. OOM

Run the following command to change the memory size:

export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
Copy the code

Then restart the Jack service:

./prebuilts/sdk/tools/jack-admin kill-server
./prebuilts/sdk/tools/jack-admin start-server
Copy the code

3. Other warning information can be ignored if compilation is not terminated