编译android源码官方教程(4)开始编译

时间:2022-06-25 22:54:13

Preparing to Build

The following instructions to build the Android source tree apply to all branches, including master. The basic sequence of build commands is as follows:

Note: If you're building Android 6.0 or later, please see Compiling with Jack for information on this new default toolchain.

Obtain proprietary binaries


AOSP cannot be used from pure source code only and requires additional hardware-related proprietary libraries to run, such as for hardware graphics acceleration. See the sections below for download links and Device binaries requirements for additional resources.

Download proprietary binaries

You can download official binaries for the supported devices running tagged AOSP release branches from Google's Nexus driver page. These binaries add access to additional hardware capabilities with non-open source code. To instead build the AOSP master branch, use the Binaries Preview for Nexus Devices. When building the master branch for a device, use the binaries for the most recent numbered release or with the most recent date.

Extract proprietary binaries

Each set of binaries comes as a self-extracting script in a compressed archive. Uncompress each archive, run the included self-extracting script from the root of the source tree, then confirm that you agree to the terms of the enclosed license agreement. The binaries and their matching makefiles will be installed in the vendor/ hierarchy of the source tree.

Clean up

To ensure the newly installed binaries are properly taken into account after being extracted, delete the existing output of any previous build using:

$ make clobber

Set up environment


Initialize the environment with the envsetup.sh script. Note that replacing source with . (a single dot) saves a few characters, and the short form is more commonly used in documentation.

$ source build/envsetup.sh

or

$ . build/envsetup.sh

Choose a target


Choose which target to build with lunch. The exact configuration can be passed as an argument. For example, the following command:

$ lunch aosp_arm-eng

refers to a complete build for the emulator, with all debugging enabled.

If run with no arguments lunch will prompt you to choose a target from the menu.

All build targets take the form BUILD-BUILDTYPE, where the BUILD is a codename referring to the particular feature combination.

The BUILDTYPE is one of the following:

Buildtype Use
user limited access; suited for production
userdebug like "user" but with root access and debuggability; preferred for debugging
eng development configuration with additional debugging tools

For more information about building for and running on actual hardware, see Running Builds.

Build the code


Please note, this section is merely a summary to ensure setup is complete. See Running Builds for detailed instructions on building Android.

Build everything with make. GNU make can handle parallel tasks with a -jN argument, and it's common to use a number of tasks N that's between 1 and 2 times the number of hardware threads on the computer being used for the build. For example, on a dual-E5520 machine (2 CPUs, 4 cores per CPU, 2 threads per core), the fastest builds are made with commands between make -j16 and make -j32.

$ make -j4

Run it!


You can either run your build on an emulator or flash it on a device. Please note that you have already selected your build target with lunch, and it is unlikely at best to run on a different target than it was built for.

Note: Remember to obtain proprietary binaries or your build will not boot successfully on your target hardware. If you obtain binary blobs at this point you will need to unpack them, make clobber and rebuild.

Flash with fastboot

To flash a device, you will need to use fastboot, which should be included in your path after a successful build. See Flashing a device for instructions.

Emulate an Android Device

The emulator is added to your path automatically by the build process. To run the emulator, type:

$ emulator

Troubleshooting Common Build Errors


Wrong Java Version

If you are attempting to build a version of Android inconsistent with your version of Java, make will abort with a message such as

************************************************************
You are attempting to build with the incorrect version
of java. Your version is: WRONG_VERSION.
The correct version is: RIGHT_VERSION. Please follow the machine setup instructions at
    https://source.android.com/source/initializing.html
************************************************************

This may be caused by:

  • Failing to install the correct JDK as specified in JDK Requirements.
  • Another JDK previously installed appearing in your path. Prepend the correct JDK to the beginning of your PATH or remove the problematic JDK.

Python Version 3

Repo is built on particular functionality from Python 2.x and is unfortunately incompatible with Python 3. In order to use repo, please install Python 2.x:

$ apt-get install python

Case Insensitive Filesystem

If you are building on an HFS filesystem on Mac OS, you may encounter an error such as

************************************************************
You are building on a case-insensitive filesystem.
Please move your source tree to a case-sensitive filesystem.
************************************************************

Please follow the instructions in Initializing the Build Environment for creating a case-sensitive disk image.

No USB Permission

On most Linux systems, unprivileged users cannot access USB ports by default. If you see a permission denied error, follow the instructions Initializing the Build Environment for configuring USB access.

If adb was already running and cannot connect to the device after getting those rules set up, it can be killed with adb kill-server. That will cause adb to restart with the new configuration.