Jane address book: www.jianshu.com/p/2beae17c9…

Android source code compilation of four processes :1. Source download; 2. Build the compilation environment; 3. Compile source code; 4 Simulator Operation

  • 1. Download source code

    • Make sure you have Git installed first.

      Sudo apt-get install git git config -- global user.email"test@test.com "git config -- global user.name"test"Copy the code
    • Use tsinghua University mirror

      • Android image usage help
      • Start by downloading the REPO tool
      mkdir ~/bin
      PATH=~/bin:$PATHcurl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo chmod a+x ~/bin/repo / / rejected the connection can use tuna git - 'mirror, mirror address will address to tsinghua university See https://mirrors.tuna.tsinghua.edu.cn/ for detailshelp/git-repo/
      Copy the code
      • Use the initialization package updated every month (you can also use the traditional initialization method, because the network is slow, the download will fail, or directly download the initialization package and then synchronize)

      • Download the address of the initialization package updated monthly

      • Since all the code is checkout from the hidden.repo directory, only the.repo directory remains, and you can download it and unzip it and repo sync again to get the full directory

      Using method is as follows: https://mirrors.tuna.tsinghua.edu.cn/aosp-mo nthly/aosp wget - c - latest. TarDownload the initialization package
      tar xf aosp-latest.tar
      cd AOSP   Extract AOSP project directory
      You can't see anything because there is only a hidden.repo directory
      repo sync Create a complete directory
      # or repo sync-l only checkout code
      Copy the code
      • If you need a specific version, you will need to switch to the source code of the specified branch. Run the following command to switch to the source code of the specified branch:

        Branch list code, tag, and breakdown version number

        Repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest - b android - 8.0.0 _r2 repo syncCopy the code
  • 2. Build a compilation environment

    • Hardware requirements:

      • 64-bit operating systems can only build up to 2.3.x, if you want to build up to 2.3.x, you need a 32-bit operating system. The more disk space, the better, at least 100GB. If you want to run Linux on a virtual machine, you need at least 16GB of RAM/swap.
    • Software requirements

    • Installation of its eight

      sudo apt-get update
      sudo apt-get install openjdk-8-jdk
      
      Copy the code
    • Dependency Settings:

      sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib sudo apt-get install -y git flex  bison gperf build-essential libncurses5-dev:i386 sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc Zlib1g-dev :i386 sudo apt-get install DPKG -dev libsdl1.2-dev libesd0-dev sudo apt-get install git-core gnupg flex bison gperf build-essential sudo apt-get install zip curl zlib1g-dev gcc-multilib g++-multilib sudo apt-get install libc6-dev-i386 sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4 sudo apt-get install lib32z-dev ccacheCopy the code
      • There may be a problem with “unable to locate package libesd0-dev” in dependency Settings
        Solution: In the/etc/apt sources. The list to add image source deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse then sudo Apt-get update install apt-get update install apt-get updateCopy the code
    • Operating System Requirements

      Android version Compile the minimum version of Ubuntu required
      Android 6.0 to AOSP Master Ubuntu 14.04
      Android 2.3.x to Android 5.x Ubuntu 12.04
      Android 1.5 to Android 2.2.x Ubuntu 10.04
  • JDK Version Requirements

    Android version Compile the required JDK version
    AOSP’s Android mainline OpenJDK 8
    Android 5.x to Android 6.0 Oracle JDK 7
    Android 2.3.x to Android 4.4.x Oracle JDK 6
    Android 1.5 to Android 2.2.x Oracle JDK 5
  • Official build environment documentation address

    Setting up the Compilation Environment

  • Initialize the compilation environment

    sourceBuild /envsetup.sh or.build /envsetup.shCopy the code
    • Select the target
    . lunch aosp_arm64-eng
    
    Copy the code
    • This command indicates that the simulator is fully compiled and that all debugging functions are enabled. If you run the command without providing any parameters, Lunch will prompt you to select a target from the menu. All BUILD targets take the form of build-buildType, 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 production environments (no root and dedug, etc.)
      userdebug The root and debug permissions are enabled based on the user version.
      eng The developer’s version, which has maximum permissions, also comes with a number of debug tools
  • 3. Compile source code

    • You can compile any code using make. GNUMake can handle parallel tasks with the -jn parameter, and the number of tasks typically used, N, is between one and two times the number of hardware threads on the machine used at compile time. For example, on a dual-core E5520 computer (2 cpus, 4 cores per CPU, 2 threads per kernel), you can use commands between make-j16 and make-j32 for the fastest compilation speed.
    make -j8
    Copy the code
    • Compilation of

  • 4. Compile and run

    • Running simulator

      // If you want to run the emulator directly after compiling the source code, enter the emulator command. Build /envsetup.sh lunch(select the target version of the source you just compiled) emulatorCopy the code

  • Import the source code into Android Studio to view it

    • Compile idegen

      sourceBuild/envsetup. Sh / / sets the file to temporary variables MMM development/tools/idegen / / / generated idegen. Jar file (#### build completed successfully (49 seconds) ####
      
      Copy the code
    • Run the idegen.sh script

      . development/tools/idegen/idegen.sh
      
      Copy the code

      See the following figure, which indicates that the compilation of idegen is complete. After successful execution, two files android. Ipr and Android.

      • Android. Ipr generally stores project-specific Settings, such as paths to modules and Modules libraries, compiler configuration, entry points, and so on.
      • Android. Iml is used to describe modules. It includes modules paths, dependencies, order Settings, and so on. A project can contain multiple *.iml files.
      • Open Android Studio, choose File->Open to pop up the path selection box, enter the corresponding source root path, and then select Android. ipr File to start importing the source code. The import speed depends on the computer performance, usually 10 to 20 minutes.
      • So far the entire source code compilation is complete, and can be viewed source code.
  • A pit encountered during compilation

    • Error: library-pathout/host/linux-x86/lib64/libsepolwrap.so does not exist

      • Sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev 2 Verify that JDK environment variables are configured
    • OpenJDK methods for configuring environment variables

      1. Run the sudo gedit /etc/profile command in the gedit text editor to add environment variables to /etc/profile 2. Add the following lines to the end of the open /etc/profile file:export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
      export JRE_HOME=${JAVA_HOME}/jre 
      export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib 
      export PATH=${JAVA_HOME}/bin:$PATH
      
      Copy the code
    • There was an issue compiling Android 8.0

      loadlocale.c:130:_nl_intern_locale_data: ?? 'cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))'????? Aborted (core dumped)Copy the code

      This can be added at the end of build/envsetup.sh

      export LC_ALL=C
      Copy the code

      Refer to the solution address

    • Try increasing heap size with Java option ‘-xmx

      To solve the problem, execute the following commands in sequenceexport JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
      jack-admin kill-server && jack-admin start-server
      Copy the code

      Refer to the solution address

The last

  • At this point, Android source code compilation is complete, source code compilation is a process that requires patience, hope to see the article you can also compile success. If there are any mistakes in the article, please mention them to me, and we can make progress together. If you think my article helps you, please also give me a like or attention.

  • The resources

    Setting up the Compilation Environment

    Hands-on implementation of Android source code (AOSP) download, compile, run, import, debugging

    Android image usage help

    AOSP Sources in the IDE