
Open Devices
For some of the Xperia™ devices, we provide Android™ Open Source Project (AOSP) device configurations on GitHub. This means that the software will be open for you as a developer to use and contribute to. This is a way for us to support the open Android community, and it is also a tool for us to facilitate and verify contributions to AOSP.
How to build and flash a Linux kernel for AOSP supported devices
Why rebuild the kernel?
There can be several reasons for why you want to rebuild and compile a custom kernel. You might, for instance, be a kernel developer, or you might have custom hardware that the standard kernel doesn’t support. Rebuilding a kernel enables you to make modifications to your device that are normally not supported by the manufacturer.
Note: If you’re not an experienced developer, Sony advises you not to rebuild the original kernel, as it is normally not needed. At Sony, we’re proud to deliver great software experiences through our rigorously tested and official software releases.
Prerequisites
This tutorial assumes that you are running Linux or Mac. On the Android Open Source Project website, you can find detailed instructions on how to set up and initialise a Linux build environment. You will probably also want to use a version control system like Git to handle your project. You can download Git from the Git webpage.
Rebuild the kernel
In order to rebuild the Linux kernel for your Sony Xperia device, you need to carefully follow the steps below. All code commands should be entered in the command prompt.
Guides
1Set up the source code and environment
Select one of the Android build guides (see the AOSP build instructions page) and do the following steps of the guide:
- Prepare your Java environment
- Install the necessary tools to make an Android build
- Download Repo tool and set PATH
- Initialise the AOSP tree
- Add the necessary patches from the AOSP upstream branch
Note: If you already have the environment set then steps 1-3 are optional. This automatically rebuild only works with Android 6.0.1 and newer build guides.
2Prepare the repository for kernel rebuild
To rebuild the kernel you have to remove the pre-build kernel folder by typing the command:
rm –r device/sony/common-kernel
Note: The open device tree will automatically build the kernel from source if a pre-built kernel is not found.
3Rebuild the boot image
To rebuild the boot image with a kernel built from source, use the following commands:
source build/envsetup.sh && lunch
When prompted, pick the number corresponding to your device in the list displayed and press enter. To start the build, type:
make -j <insert the cpu thread number of your computer> bootimage
Note: The build will take some minutes to complete.
4Flash the boot image file
Use the Fastboot protocol to flash the boot image file to your phone or tablet. Enter the following command:
fastboot flash boot out/target/product/<device>/boot.img
When prompted to connect your device, first make sure that your device is turned off. Then, hold down the Volume Up button and connect the device to your computer with a USB cable. Your device’s notification light should shine blue to confirm it’s in Fastboot mode.
When the flashing is completed, reboot your device by entering:
fastboot reboot
1Unlock the boot loader
All the work on building and compiling a kernel is based on you having unlocked the boot loader of your device. Instructions on how to do this can be found in our new and improved unlocking boot loader pages. Please remember that this is only for advanced kernel developers. We recommend standard users not to unlock the boot loader, as it is not needed.
Note: There is no turning back after unlocking the boot loader. You may void the warranty of your device, and you will not be able to revert it back to a locked and original state again.
2Download a cross compiler
A cross compiler is used to build ARM binaries on a different architecture, such as x86. This allows you to compile the kernel into a format that the device can run. We recommend the use of the GCC 4.8 cross compiler provided in the Android Open Source Project.Download the GCC 4.8 cross compiler using the following commands:
mkdir /android && cd /android
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/
Note: Any ARM cross compiler capable of compiling Linux kernels can be used, such as CodeSourcery Lite, and Linaro.
3Export the cross compiler
The cross compiler needs to be exported to specify which compiler to be used by the build system.Export the GCC 4.8 cross compiler using the following command:
export CROSS_COMPILE=/android/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-
Note: In case of using another cross compiler than GCC 4.8, replace /android/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi- with the correct search path of the cross compiler of your choice.
4Download the kernel source from GitHub
The Sony kernel sources for flagship devices are available on the SonyXperiaDev GitHub. More sources for other devices will be added over time. Download by entering:
git clone https://github.com/sonyxperiadev/kernel
cd kernel
git branch -r
git checkout
5Identify your device’s config file name
In order to configure the kernel, you need first to identify your device’s config file name. Please note that only devices featured in the Sony GitHub release and Sony GitHub unified kernels are shown here.
Device | Config file name |
---|---|
Device Xperia X | Config file name aosp_loire_suzu_defconfig |
Device Xperia X Compact | Config file name aosp_loire_kugo_defconfig |
Device Xperia X Performance | Config file name aosp_tone_dora_defconfig |
Device Xperia XZ | Config file name aosp_tone_kagura_defconfig |
Device Xperia Z5 Premium | Config file name aosp_kitakami_suzuran_defconfig |
Device Xperia Z5 Compact | Config file name aosp_kitakami_satsuki_defconfig |
Device Xperia Z5 | Config file name aosp_kitakami_sumire_defconfig |
Device Xperia Z4 Tablet | Config file name aosp_kitakami_karin_defconfig,
aosp_kitakami_karin_windy_defconfig |
Device Xperia Z3+ | Config file name aosp_kitakami_ivy_defconfig |
6Configure the kernel
Configure the kernel is a required step before building it. Kernel drivers can be turned on or off, or be built as modules. This has to be configured in order for the kernel to function as desired. Configure the kernel by entering:
make ARCH=arm64 CROSS_COMPILE=$CROSS_COMPILE <device_defconfig>
Note: Replace
7Build the kernel and needed modules
When building the kernel a compressed kernel image file is created and placed in the default target arch/arm/boot/zImage on your computer. To start the build, enter:
make ARCH=arm64 CROSS_COMPILE=$CROSS_COMPILE -j <cpu_thread_number>
Note: The
8Copy needed binaries to your Android repository
For building a new boot image with the compiled kernel, the kernel binary “arch/arm64/boot/Image.gz-dtb” has to be renamed “kernel-
Note: Please note that this is a kernel binary with an appended dtb and there is no need to copy the .dtb separately. Wireless drivers are built into the kernel and there is no kernel module for them.
For environment setup instructions, see the AOSP build instructions. To generate the images enter the following commands:
source build/envsetup.sh && lunch
When prompted, pick the number corresponding to your device in the list displayed and press enter. To start the build, type:
make -j <insert the cpu thread number of your computer> bootimage
Note: The build will take some minutes to complete.
9Download the Fastboot protocol
Fastboot is a diagnostic protocol included in the Android SDK. Make sure you have the Android SDK installed before proceeding. Fastboot will be used to modify the flash file system on your device via a USB connection from the computer. If you unlocked your boot loader in Step 1, you will already have Fastbootinstalled.
10Flash the boot image file
Use the Fastboot protocol to flash the boot image file to your phone or tablet. Enter the following command:
fastboot flash boot out/target/product/<device>/boot.img
When prompted to connect your device, first make sure that your device is turned off. Then, hold down the Volume Up button and connect the device to your computer with a USB cable. Your device’s notification light should shine blue to confirm it’s in Fastboot mode.
When the flashing is completed, reboot your device by entering:
fastboot reboot
You have now gone through the process of how to download a Sony kernel source, and then how to build and flash the kernel to your device. However, we have not gone into detail on how to modify the kernel itself. For more details, we recommend you to deep dive into forums and websites such as XDA Developers and XDA University, where there are many skilled people sharing their knowledge.
1Unlock the boot loader
All the work on building and compiling a kernel is based on you having unlocked the boot loader of your device. Instructions on how to do this can be found in our new and improved unlocking boot loader pages. Please remember that this is only for advanced kernel developers. We recommend standard users not to unlock the boot loader, as it is not needed.
Note: There is no turning back after unlocking the boot loader. You may void the warranty of your device, and you will not be able to revert it back to a locked and original state again.
2Download a cross compiler
A cross compiler is used to build ARM binaries on a different architecture, such as x86. This allows you to compile the kernel into a format that the device can run. We recommend the use of the GCC 4.8 cross compiler provided in the Android Open Source Project.Download the GCC 4.8 cross compiler using the following commands:
mkdir /android && cd /android
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/
Note: Any ARM cross compiler capable of compiling Linux kernels can be used, such as CodeSourcery Lite, and Linaro.
3Export the cross compiler
The cross compiler needs to be exported to specify which compiler to be used by the build system.Export the GCC 4.8 cross compiler using the following command:
</pre>
<pre>export CROSS_COMPILE=/android/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
Note: In case of using another cross compiler than GCC 4.8, replace /android/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi- with the correct search path of the cross compiler of your choice.
4Download the kernel source from GitHub
The Sony kernel sources for flagship devices are available on the SonyXperiaDev GitHub. More sources for other devices will be added over time. Download by entering:
git clone https://github.com/sonyxperiadev/kernel
cd kernel
git branch -r
git checkout
5Identify your device’s config file name
In order to configure the kernel, you need first to identify your device’s config file name. Please note that only devices featured in the Sony GitHub release and Sony GitHub unified kernels are shown here.
Device | Config file name |
---|---|
Device Xperia Z3 | Config file name aosp_shinano_leo_defconfig |
Device Xperia Z3 Compact | Config file name aosp_shinano_aries_defconfig |
Device Xperia Z3 Tablet Compact | Config file name aosp_shinano_scorpion_defconfig |
Device Xperia Z2 | Config file name aosp_shinano_sirius_defconfig |
Device Xperia Z2 Tablet | Config file name aosp_shinano_castor_defconfig |
Device Xperia Z Ultra | Config file name aosp_rhine_togari_defconfig |
Device Xperia Z1 | Config file name aosp_rhine_honami_defconfig |
Device Xperia Z1 Compact | Config file name aosp_rhine_amami_defconfig |
Device Xperia E3 | Config file name aosp_yukon_flamingo_defconfig |
Device Xperia M2 | Config file name aosp_yukon_eagle_defconfig |
Device Xperia T2 Ultra | Config file name aosp_yukon_tianchi_defconfig |
Device Xperia T3 | Config file name aosp_yukon_seagull_defconfig |
6Configure the kernel
Configure the kernel is a required step before building it. Kernel drivers can be turned on or off, or be built as modules. This has to be configured in order for the kernel to function as desired. Configure the kernel by entering:
make ARCH=arm CROSS_COMPILE=$CROSS_COMPILE <device_defconfig>
Note: Replace
7Build the kernel and needed modules
When building the kernel a compressed kernel image file is created and placed in the default target arch/arm/boot/zImage on your computer. To start the build, enter:
make ARCH=arm CROSS_COMPILE=$CROSS_COMPILE -j <cpu_thread_number>
Note: The
8Copy needed binaries to your Android repository
For building a new boot image with the compiled kernel, the kernel binary “arch/arm64/boot/Image.gz-dtb” has to be renamed “kernel-
Note: Please note that this is a kernel binary with an appended dtb and there is no need to copy the .dtb separately. Wireless drivers are built into the kernel and there is no kernel module for them.
For environment setup instructions, see the AOSP build instructions. To generate the images enter the following commands:
source build/envsetup.sh && lunch
When prompted, pick the number corresponding to your device in the list displayed and press enter. To start the build, type:
make -j <insert the cpu thread number of your computer> bootimage
Note: The build will take some minutes to complete.
9Download the Fastboot protocol
Fastboot is a diagnostic protocol included in the Android SDK. Make sure you have the Android SDK installed before proceeding. Fastboot will be used to modify the flash file system on your device via a USB connection from the computer. If you unlocked your boot loader in Step 1, you will already have Fastbootinstalled.
10Flash the boot image file
Use the Fastboot protocol to flash the boot image file to your phone or tablet. Enter the following command:
fastboot flash boot out/target/product/<device>/boot.img
When prompted to connect your device, first make sure that your device is turned off. Then, hold down the Volume Up button and connect the device to your computer with a USB cable. Your device’s notification light should shine blue to confirm it’s in Fastboot mode.
When the flashing is completed, reboot your device by entering:
fastboot reboot
You have now gone through the process of how to download a Sony kernel source, and then how to build and flash the kernel to your device. However, we have not gone into detail on how to modify the kernel itself. For more details, we recommend you to deep dive into forums and websites such as XDA Developers and XDA University, where there are many skilled people sharing their knowledge.