Skip to content

Instantly share code, notes, and snippets.

@jkhsjdhjs
Last active December 22, 2022 22:42
Show Gist options
  • Save jkhsjdhjs/f6717a18106b7328e2ccf5f5e72edbe4 to your computer and use it in GitHub Desktop.
Save jkhsjdhjs/f6717a18106b7328e2ccf5f5e72edbe4 to your computer and use it in GitHub Desktop.
How to compile Android x86 Pie (Version 9) on Arch Linux

How to compile Android x86 Pie (Version 9) on Arch Linux

Why?

Why not just use Ubuntu as in the official guide?

Building Android 9 requires Python 3 in addition to Python 2 nowadays. While even the old Ubuntu versions recommended in the guide offer Python 3, they only have Python 3.5 or older, which lacks some features required by the build script. While you can install Python 3.6 or newer on Ubuntu (just compile it yourself), I then still ran into other issues (can't recall exactly what they were), but eventually decided that it was too much of a hassle and switched back to Arch Linux.

TL;DR Building on the recommended Ubuntu version is too complicated.

Required Packages

pacman -S --needed base-devel bc gcc11 unzip zip dosfstools cdrtools libisoburn python-mako jre8-openjdk-headless repo syslinux openssl-1.1 mtools lib32-util-linux

If you have multiple java SDKs installed, make sure that java-8-openjdk/jre is selected in archlinux-java.

Additionally, the following AUR packages are required:

The lib32-util-linux package from the multilib repository is only required for running syslinux's isohbyrid in the end. The build will "fail" without it, but the ISO will still be generated:

Writing to 'stdio:out/target/product/x86_64/android_x86_64.iso' completed successfully.

/bin/bash: line 1: external/syslinux/bios/utils/isohybrid: No such file or directory
ninja: build stopped: subcommand failed.
15:36:11 ninja failed with: exit status 1

#### failed to build some targets (05:49 (mm:ss)) ####

I can boot the resoluting iso without running isohybrid just fine in my VMs, maybe it's necessary on real hardware.

Getting the sources

Follow https://www.android-x86.org/source.html until after selecting the $TARGET_PRODUCT and $TARGET_BUILD_VARIANT.

Fixing the build

This section contains mandatory adjustments that have to be done for the build to succeed.

Making Python 2 the default python version

Create a new directory containing a symlink named python to /usr/bin/python2 and prepend this directory to your $PATH:

mkdir ~/prefer-python2
ln -s /usr/bin/python2 ~/prefer-python2/python
export PATH="$HOME/prefer-python2:$PATH"

This causes every program searching the $PATH for python to use our newly created symlink instead of the symlink in /usr/bin, because our directory comes first.

Using gcc 11 to build the kernel

The linux kernel included with Android x86 Pie can't be built with gcc 12 or newer (you'll get use-after-free errors), which is currently shipped with Arch Linux. Fortunately Arch Linux also offers a gcc11 package (as of 21.11.2022, may be moved to the AUR in the future). To use it, two lines need to be added to the Makefile used for linux:

kernel/tools/build/Makefile.build

HOSTCC = gcc-11
CC = gcc-11

I just added the lines directly below the copyright notice at the top of the file.

Using an UTF-8 locale for javac

Some java files that are compiled contain non-ascii characters. javac uses the OS locale to determine the encoding of the files it compiles, thus a UTF-8 locale must be set (e.g. en_US.UTF-8).

# localectl set-locale LANG=en_US.UTF-8

Linking against OpenSSL 1.1

Android x86 Pie can't be built against OpenSSL 3.0 because it uses deprecated methods. Fortunately Arch Linux also still offers an openssl-1.1 package, which we can link against.

export LD=/usr/lib/openssl-1.1/

Optional Adjustments

This section contains optional adjustments that can be done to improve the usage of Android x86. None of these are required.

Shutdown instead of displaying the power menu when an ACPI PowerLongPress event is received

This adjustment is really useful when running Android x86 in a VM, as by default Android doesn't shutdown when the power button is hold. The ACPI PowerLongPress event is also used by most hypervisors to shutdown guests, thus when attempting to shutdown an Android x86 guest via the hypervisor it doesn't shutdown but instead displays the power menu. To change this behavior, edit the file frameworks/base/core/res/res/values/config.xml and search for config_longPressOnPowerBehavior. Change the value from 1 to 3.

<!-- Control the behavior when the user long presses the power button.
        0 - Nothing
        1 - Global actions menu
        2 - Power off (with confirmation)
        3 - Power off (without confirmation)
        4 - Go to voice assist
-->
<integer name="config_longPressOnPowerBehavior">3</integer>

Source: https://groups.google.com/g/android-x86/c/9_bJ_dDM1vE/m/GdD9r7ZAGUkJ

Allow shutdown from non-interactive state

Android shuts off the display after some time (i.e. it enters non-interactive state). To allow shutdown from non-interactive state directly, the option config_supportLongPressPowerWhenNonInteractive in the same file needs to be changed to true:

<!-- If this is true, long press on power button will be available from the non-interactive state -->
<bool name="config_supportLongPressPowerWhenNonInteractive">true</bool>

Alternatively, display sleep can be disabled in the Android Developer Options:

  1. Enter Settings -> System -> About tablet
  2. Click Build number 7 times to enable developer settings
  3. Enter Settings -> System -> Developer options
  4. Enable Stay awake

However, Android 9 has a higher CPU usage in interactive state than in non-interactive state. Thus, I prefer having display sleep enabled and holding the menu key for a second to turn the display on. Note that with the above two changes, pressing CTRL+ALT+DEL will also shutdown the system.

Open GApps

For syncing Open GApps with repo, git-lfs is required: pacman -S git-lfs.
For instructions on how to add Open GApps to your manifest file, please refer to the following two sources:

If you encounter issues during the build, see opengapps/aosp_build#253 (comment)

Finally...

Now all that's left is to compile it. Good Luck!

 m -jX iso_img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment