preface

A few years ago, bloggers were always compiling Android code for a company that built set-top boxes, but there was no Nexus 6, aka Shamu. Today I’m going to swipe a self-compiled Rom for it. Pick up the keyboard is dry.

AOSP:Android Open Source Project

To prepare

  • Ubuntu 14.04 +
  • OpenJDK/JDK
  • A Nexus/Pixel phone

Assuming you already have a newer Ubuntu system and a Google Son phone, we’ll start by installing the JDK.

This article was written by MichaelX, whose blog address is blog.csdn.net/xiong_it.


AOSP compilation environment setup

Its installation

Warning: Do not use the Oracle JDK to compile newer AOSP versions (API 21+/Android 5.0 and above). You will get an error when preparing to make clobber.

Checking build tools versions... ************************************************************* You asked for an OpenJDK based build but your version is Version "1.8.0_121" Java(TM) SE Runtime Environment(build 1.8.0_121-B13)Java HotSpot (TM) 64-bit Server VM(build 25.121 - bl3), mixed mode). * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * the build/core/main. Mk: 230: *** stop.Copy the code

The choice of JDK version: It depends on the Android version you want to build

  • AOSP OpenJDK 8
  • Android 5.x (Lollipop) – Android 6.0 (Marshmallow):OpenJDK 7
  • Android 2.3.x (Gingerbread) – Android 4.4.x(KitKat):Java JDK 6
  • Android 1.5 (Cupcake) – Android 2.2.x (Froyo): Java JDK 5

The blogger wants to build for Android 7.0, so he needs to use OpenJDK 8.

OpenOJDK 8 on github 2. Openopenjdk 8 on Github 3. Add a PPA to install OpenJDK

$ sudo add-apt-repository ppa:openjdk-r/ppa
$ sudo apt-get update

$ sudo apt-get install openjdk-8-jdk

$ sudo update-alternatives --config java
$ sudo update-alternatives --config javacCopy the code

If you are running Ubuntu 15.04 or newer, run the following command to install OpenJDK 8

$ sudo apt-get update
$ sudo apt-get install openjdk-8-jdkCopy the code

Linux uses the following command to view all JDK versions on the machine

$ michaelx@michaelx-ThinkPad:~/AOSP_NBD91Z$ update-java-alternatives -l
// Print out the installed JDK version below
java-1.8. 0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8. 0-openjdk-amd64Copy the code

Linux sets the default JDK commands

// Set the default to OpenJDk8, which must be used here
$ sudo update-java-alternatives -s java-1.8. 0-openjdk-amd64
// Set the default to Oracle JDk8
$ sudo update-java-alternatives -s java-8-oracle
// Set the default to Oracle JDk7
$ sudo update-java-alternatives -s java-7-oracleCopy the code

Ubuntu 14.04: OpenJDK 8

update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so
Copy the code

This prompt is ignored and can be ignored.

Check the Java version. If the following message is displayed, the JDK environment is ok.

$ java -version

openjdk version "1.8.0 comes with _111"
OpenJDK Runtime Environment (build 1.8. 0_111-8u111-b14-3~14.041.-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)Copy the code

If you really can’t switch the default JDK, do what bloggers do and uninstall the Oracle JDK.

Install necessary software

Run the following command on your Ubuntu 14(X64)

$ sudo apt-get install git-core gnupg flex bison gperf build-essential \
  zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 \
  lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache \
  libgl1-mesa-dev libxml2-utils xsltproc unzipCopy the code

Some will be installed, some will not be installed, or the installation will fail, so ignore it and continue. For other system versions, refer directly to: Express a Build environment

Configure USB access rights

Ps: I don’t know what this does, but it is officially recommended, in order to make usb devices accessible to ordinary users.

$ wget -S -O - http://source.android.com/source/51-android.rules | sed "s/<username>/$USER/" | sudo tee >/dev/null /etc/udev/rules.d/51-android.rules; sudo udevadm control --reload-rulesCopy the code

Install the repo

Install repo to download the AOSP source code. Repo is Google according to Git development to specifically manage Android source code with a breakpoint continuation feature. For details about git/repo commands, see Git /repo Common commands. Run the following commands in sequence.

$ cd ~
$ mkdir bin
$ PATH= ~ /bin:$PATH

$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ Chmod a+x ~/bin/repoCopy the code

Configure git

Since you need to download the Android source code, you will need to prepare a Google Gmail email address beforehand. Run the following command to configure the git user name and Gmail.

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

Configure Google git cookies access to download aOSP source site resources in bulk.

Android.googlesource.com/new-passwor…



Copy the command in the box, paste it into the terminal, and execute it.


Download AOSP source code

Create a directory to store Android source code

$ mkdir ~/AOSP_NBD91Z AOSP_NBD91ZNBD91Z
$ cd AOSP_NBD91ZCopy the code

According to the hands of the machine and the following two branches of connection for repo choice: source.android.com/source/buil… Developers.google.com/android/nex…

By default, the AOSP source code is not compatible with the driver. It is only suitable for emulators to run, because the blog wants to build for Nexus 6, so you need to consider the driver problemDownload the drive and save it for later.



Find the corresponding Branch name in the first link (Android – 7.0.0 _r29).

Initialize the REPO branch in the AOSP local directory

$ cd ~/AOSP_NBD91Z
$ repo init -u https://android.googlesource.com/platform/manifest -b android-7.0. 0_r29Copy the code

Download/renew AOSP source code whether to start the first download, or break the continuation of the download, is to execute the following command.

$ repo syncCopy the code

It was a long wait, and the branch (Android-7.0.0_r29) totaled about 80 GIGABytes of code (including the version management files. Repo directory: 46 GIGABytes).

Remember to partition at least 120 GB, 80 GB is not the size of the latest branch. It compiles much larger: this branch compiles about 30 gigabytes of out directories.



But once it’s downloaded, what if it’s nothingVersion controlThe repo directory can be deleted to save disk space.


Compile AOSP preparatory work

Setting the compile cache (optional) speeds up subsequent compilations. If necessary, run the following command in the source directory

$ export USE_CCACHE=1
$ export CCACHE_DIR= ~/AOSP_NBD91Z/.ccache # Directory customization
$ prebuilts/misc/linux-x86/ccache/ccache -M 50G # 50-100g is officially recommendedCopy the code

Updating environment variables

$ vim ~/.bashrc
# Add the following line
export USE_CCACHE=1

$ source ~/.bashrcCopy the code

Release the phone driver

Unzip the downloaded driver files toSource code root, decompression is a few script files, executed in turn, to release the driver, the painting style is roughly as follows

A total of 8 clauses, dozens of small clauses, a line of carriage return press past, heart tired ah.

Highlight: After executing the driver script, you will be shown a long list of protocols, at the end of which you will need to enter: I ACCEPT to agree with the driver protocol before releasing the driver file. Press enter to press fast prompt you do not agree, the driver is not released successfully. After success, there will be an additional vendor directory.

After the driver is released, run make Clobber to clean up the directory of the compiled file.

Similar command: make clean It cleans out/target/product/[product_name] directory.


Compile the AOSP

Get the build platform ready

$ cd AOSP_NBD91Z
$ source build/envsetup.sh
Or, the command above is equivalent to the command below
$ . build/envsetup.shCopy the code

Select compilation Platform

This command can be executed only after the preceding command is executedlunch



I chose 21 because I had a Nexus 6 (Shamu)

On the differences between the modes

  • User Indicates the normal mode for common users
  • Userdebug has root privileges and more debugging functions. Other functions are the same as user mode
  • The best option for ENG developers, with many additional debugging tools

Run make to compile the AOSP source code, or use the -j option to specify the number of parallel compilation threads

Compile with 6 threads. The official recommended maximum number of parallel threads is between J16 and J32.
$ make -j6 

Please do what you can according to your CPU performance. The blogger used -j16 which caused the GUI interface and terminal to freeze, and could only forcibly shut down my SSD for 30s.Copy the code

Another long wait. If nothing goes wrong, then whatout/target/product/[product_name]/Img, recovery.img, etc., will be added to the directory, and you can happily swipe the machine.

Below are the various image files generated and others.


Problems encountered in compiling AOSP

Error: Permission is denied because the blog uses an external hard disk as output for out compilation. Sudo make.

Export OUT_DIR_COMMON_BASE=/media/username/ External disk path /out

Due to the author’s memory is only 4G, and did not partition the swap partition at the beginning, resulting in several insufficient memory compilation failures, there are various log forms indicating insufficient memory:

[ 34% 12287/35393] Building with Jack: out/target/common/obj/JAVA_LIBRARIES/core-all_intermediates/with-local/classes.dex

FAILED: /bin/bash out/target/common/obj/JAVA_LIBRARIES/core-all_intermediates/with-local/classes.dex.rsp

Communication error with Jack server (52). Try 'jack-diagnose'

ninja: build stopped: subcommand failed.

make: *** [ninja_wrapper] Error 1

[ 82% 30024/36285] Aligning zip: out/target/product/shamu/obj/SHARED_LIBRARIES/libdlext_test_runpath_zip_zipaligned_intermediates/libdlext_test_runpath_z ip_zipaligned.zip

[ 82% 30025/36285] Import includes file: out/target/product/shamu/obj/STATIC_LIBRARIES/libverifier_intermediates/import_includes

[ 82% 30026/36285] target thumb C++: libverifier <= bootable/recovery/asn1_decoder.cpp

[ 82% 30027/36285] target thumb C++: libverifier <= bootable/recovery/verifier.cpp

[ 82% 30028/36285] Export includes file: -- out/target/product/shamu/obj/STATIC_LIBRARIES/libverifier_intermediates/export_includes

[ 82% 30029/36285] target thumb C++: libverifier <= bootable/recovery/ui.cpp

ninja: fatal: fork: Cannot allocate memory

make: *** [ninja_wrapper] Error 1

FAILED: /bin/bash out/target/common/obj/JAVA_LIBRARIES/core-all_intermediates/with-local/classes.dex.rsp

Out of memory error (version 1.2 - rc4 'Carnac (298900 f95d7bdecfceb327f9d201a1348397ed8a843843 by [email protected])).

GC overhead limit exceeded.

Try increasing heap size with Java option '-Xmx<size>'.

Warning: This may have produced partial or corrupted output.

[ 31% 11494/36285] host C++: libartd-compiler <= art/compiler/optimizing/graph_visualizer.cc

[ 31% 11494/36285] Building with Jack: out/target/common/obj/JAVA_LIBRARIES/libprotobuf-java-nano_intermediates/classes.jack

[ 31% 11494/36285] build out/target/common/obj/JAVA_LIBRARIES/sdk_v21_intermediates/classes.jack

ninja: build stopped: subcommand failed.

[ 6% 2375/35393] target Java: icu4j (out/target/common/obj/JAVA_LIBRARIES/icu4j_intermediates/classes)

Note: Some input files use or override a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

Note: external/icu/icu4j/main/classes/core/src/com/ibm/icu/impl/Relation.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

[ 6% 2394/35393] host C++: libLLVMMC_32 <= external/llvm/lib/MC/MCDwarf.cppninja: fatal: fork: Cannot allocate memory

make: *** [ninja_wrapper] Error 1

[  5% 1883/35393] Docs droiddoc: out/target/common/docs/api-stubs
FAILED: /bin/bash out/target/common/docs/api-stubs-timestamp.rsp
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000bdb80000.72876032.0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 72876032 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /home/michaelx/AOSP_NBD91Z/hs_err_pid508.log
[  5% 1883/35393] Docs droiddoc: out/target/common/docs/system-api-stubs
DroidDoc took 27 sec. to write docs to out/target/common/docs/system-api-stubs
ninja: build stopped: subcommand failed.
make: *** [ninja_wrapper] Error 1Copy the code

There are three solutions to running out of memory:

  1. Increase machine memory
  2. Adding a swap partition
  3. Modify the prebuild/ SDK /tools/jack-admin file

The first way is not to say, add root memory to the machine, trench exclusive. The second way: increase of swap: hancj.blog.51cto.com/89070/19791… The third way:

# backup jack - admin
$ cp prebuild/sdk/tools/jack-admin ~/Docments/jack-admin.original

# Change the jack-admin file
$ vim prebuild/sdk/tools/jack-admin

# start-server method, my jack-admin in line 443, modify this method with a sentence:
# JACK_SERVER_COMMAND="java -Djava.io.tmpdir=$TMPDIR $JACK_SERVER_VM_ARGUMENTS -cp $LAUNCHER_JAR $LAUNCHER_NAME"
# Change to the following line to increase the Java heap size.
JACK_SERVER_COMMAND="java -Djava.io.tmpdir=$TMPDIR $JACK_SERVER_VM_ARGUMENTS -Xmx8000M -cp $LAUNCHER_JAR $LAUNCHER_NAME"Copy the code

The above increase of -XMx8000m means that the maximum memory used by Java heap at runtime is not more than 8000M. This value is the result obtained by the author after many tests. 2048M and 4096M still failed to be compiled, and 8000M passed compilation, which may be largely related to the author’s own hardware limitations

Another way to modify: change the variable on line 29 of jack-admin: JACK_SERVER_VM_ARGUMENTS="${JACK_SERVER_VM_ARGUMENTS:= -dfile. encoding -utf-8 -xx :+TieredCompilation}"  JACK_SERVER_VM_ARGUMENTS="${JACK_SERVER_VM_ARGUMENTS:=-Dfile.encoding-UTF-8 -XX:+TieredCompilation -Xmx8000M}" But this modification still didn't work, and the compilation failed.Copy the code

The author is to try the second, three ways to solve.

[37% 13421/35393] Ensure Jack Server is installed and started FAILED: /bin/bash -c “(prebuilts/sdk/tools/jack-admin install-server prebuilts/sdk/tools/jack-launcher.jar Prebuilts/SDK/tools/jack – server – 4.8 ALPHA. Jar 2 > &1 | | (exit 0)) && (JACK_SERVER_VM_ARGUMENTS = \ “- Dfile. Encoding = utf-8 -XX:+TieredCompilation\” prebuilts/sdk/tools/jack-admin start-server 2>&1 || exit 0 ) && (prebuilts/sdk/tools/jack-admin The update server prebuilts/SDK/tools/jack – server – 4.8 ALPHA. Jar 4.8 ALPHA 2 > &1 | | && exit 0) (prebuilts/SDK/tools/jack – admin update jack prebuilts/SDK/tools/jacks/jack – 2.28. The jar 2.28 RELEASE | | exit 47; Prebuilts/SDK/tools/jack – admin update jack prebuilts/SDK/tools/jacks/jack – 3.36. CANDIDATE. Jar 3.36. CANDIDATE | | exit 47; Prebuilts/SDK/tools/jack – admin update jack prebuilts/SDK/tools/jacks/jack – 4.7 BETA. Jar 4.7 BETA | | exit 47) “jack server already installed in “/home/michaelx/.jack-server” Launching Jack server java -XX:MaxJavaStackTraceDepth=0 -Djava.io.tmpdir=/tmp -Dfile.encoding=UTF-8 -XX:+TieredCompilation -cp /home/michaelx/.jack-server/launcher.jar com.android.jack.launcher.ServerLauncher Jack server failed to (re)start, try ‘jack-diagnose’ or see Jack server log No Jack server running. Try ‘jack-admin start-server’ No Jack server running. Try ‘jack-admin start-server’ [ 37% 13421/35393] target thumb C++: libicui18n <= external/icu/icu4c/source/i18n/coptccal.cpp [ 37% 13421/35393] target thumb C++: libicui18n <= external/icu/icu4c/source/i18n/compactdecimalformat.cpp [ 37% 13421/35393] target thumb C++: libicui18n <= external/icu/icu4c/source/i18n/cpdtrans.cpp ninja: build stopped: subcommand failed.

Jack Server failed to (re)start

$ cd /prebuild/sdk/tools/

$ jack-admin stop-server
$ jack-admin start-serverCopy the code

conclusion

Here, I believe that everyone can easily engage in the machine, if there is a need for Nexus tutorial, please leave a comment, next time a Nexus process. Put two hd screenshots of the Nexus 6 after it is refreshed

This article was originally written by MichaelX at blog.csdn.net/xiong_it. Please indicate the source of reprint

Welcome to: MichaelX’s Blog

Refer to the link

Thanks to AOSP website: source.android.com/source/init… AskUbuntu:askubuntu.com/questions/7… CSDN:blog.csdn.net/brightming/…

Etc… .