Android devices power the world. From tiny IoT devices to industrial machines, numerous sophisticated applications rely on this flexible and powerful operating system (OS). But why is that?
Besides the superficial aspects, Android allows for efficient use of hardware resources, leading to performant yet customizable devices and applications. One reason for that is the Android kernel.
This article will provide helpful insights into developing, building, and modifying custom kernel images. We will illuminate the differences between the general Linux kernel and provide an angle on how to approach customizations.
From Android Common Kernel (ACK) to Generic Kernel Image (GKI)
Before discussing the Android kernel, it is essential to differentiate some terms and provide some background. Android is an operating system derived from Linux. An Android kernel is based on a long-term support (LTS) version of a stable, mainline Linux kernel. Together with Android-specific alterations and additions it forms an Android Common Kernel (ACK).
Vendors and device manufacturers use the ACK and modify it to their needs, ultimately creating a product kernel specific to a device. These additional layers lead to complexity, resulting in a fragmentation problem. Since Android powers a broad range of applications and devices, vendor kernels often substantially differ from the base ACK, leading to difficulties in maintaining and rolling out updates.
The generic kernel image project aimed to resolve this problem. Since Android 5.4, modifications are offloaded, creating a Generic Kernel Image (GKI) and multiple vendor modules loaded through the Kernel Module Interface.
What makes an Android kernel?
But what distinguishes an ACK or GKI from the original Linux kernel? Android kernels are patched versions of official Linux kernels designed to meet specific device requirements that differ from Linux devices.
While an Android kernel's exact set of features varies, an ACK is, at its core, a stable Android Linux kernel with additional hardware drivers, distinct functionalities, and settings. Examples of functionalities of an Android kernel include different power management, graphics, and file system design. These alterations lead to better performance when running on limited hardware resources.
The power of kernel customization
But why should you consider customizing an Android kernel anyway? What is custom kernel Android? There are multiple good reasons for modifying a mainline kernel. First and foremost, the officially released GKIs are built to cover a broad range of mainly mobile devices. By doing so, they balance features and settings. Furthermore, available features may vary between kernel versions.
Breaking into kernel Android development enables developers to tweak this crucial part to their needs. Modifying the kernel settings and removing or adding features will enhance device performance and let developers fine-tune their custom devices. Modifications may, for example, aim to achieve better battery performance or strip down unused features for a smaller disk size. Those alterations are handy when building highly customized professional or industrial devices with different requirements than common mobile devices.
Android kernel: A powerhouse of security features
Implementing alterations at the kernel level sometimes involves risks. Android is well-known for its robust security features, and most of these features directly interact with the Android kernel. Developers must be aware of the security mechanisms and implications of modifications to the kernel. The most recognizable features in the Android kernel are:
- Android Verified Boot (AVB)
- SELinux Android
- Trusted Execution Environment (Android TEE)
When the device is started, the Android kernel is immediately covered by AVB, establishing a chain of trust from the bootloader to system and vendor partitions and protecting this critical process from tampering.
Android further comprises application sandboxing, fundamental processes, and user isolation to keep devices safe. Under the hood, the Android kernel, specifically SELinux Android , drives these essential security features, which Android is well-known for.
Android TEE provides another layer of security by creating a secure and isolated environment for sensitive operations. In principle, this feature is hardware-enabled, meaning that physical precautions are taken inside the device to protect it from vulnerable or malicious software.
These three security features and many more subtle components contribute to a safe Android and are powered by the Android kernel. Therefore, developing and modifying the kernel must ensure these features remain intact.
Getting started with kernel development on Android
Customization is at the heart of almost every Android device. Among the most impactful ways of customization is Android kernel programming. However, Android kernel development may seem more complex to begin with. There are multiple good resources from which beginners and experts can draw knowledge.
Communities and documentation for Android kernel development
Linux and Android are both community-driven projects. Multiple expert communities exist, including xda-developers and kernelnewbies relevant to Android kernel development. Depending on the individual knowledge, an introduction to kernel development course offered by multiple providers might be worth looking at.
Another helpful body of knowledge is the official docs. Android kernel documentation covers the ACK/GKI architecture, important frameworks, concepts, and features. The official Linux kernel documentation is also recommendable and covers almost everything there is to know.
How to build a generic kernel image from scratch
After looking at the fundamentals, we will outline one way of using the Android building system to build an Android kernel from the source.
Setting up the environment
Building kernel images requires only a few prerequisites you might already have in place:
- Android debug bridge (adb) to interact and flash the target device
- Bazel to build and compile the kernel image (Android 13+)
- Git-repo (Repo) to conveniently interact with the source repositories
Note: We recommend downloading and installing Bazel separately when experimenting with kernel builds. Although this build tool also ships with the source repository, having Bazel on the host will simplify the process and command line operations.
Downloading the source repository
Similar to the AOSP ROM, multiple official Android kernel versions are available. These different versions exist for the most popular hardware architectures. Kernel versions are organized as branches in the repository.
We start by creating a directory for the source repository:
>> mkdir kernel-build
From within the directory, we are using repo to initialize the project and pull the desired kernel version Android:
>> repo init -u https://android.googlesource.com/kernel/manifest -b common-android-mainline
>> repo sync
Note (Blockquote): You can refer to the official AOSP repository for an extensive list of available branches (Android kernel version update)
Building the kernel image
We assume that we have made some alterations in the source code and can now proceed with building the kernel image. For this, we will now use the preinstalled Bazel:
>> bazel run //common:<kernel>
This command compiles the custom Android kernel image using the default “mixed-build” setting (//common). A custom setting is also available, but it is only recommendable in specific cases.
When compiling a kernel, the compilation is usually limited to the same architecture as the host system. To overcome this limitation, we may need to enable cross-compile using Bazel. Given that, a list of available architectures (to substitute <kernel>) can be queried using:
>> bazel query "kind('py_binary', //common:*)"
After that, we can embed our Android build image into our device. There are multiple options; for example, we can flash our device with a new build or reboot the kernel using adb Android and fastboot without flashing. Either way, we have now successfully compiled a custom Android OS kernel.
Customization with emteria
Custom kernel images can give devices a competitive advantage. The previous example build is simplified for the sake of this article, and tweaking an Android kernel is often tricky.
This is why we at emteria offer support with Android kernel customizations. We use our extensive customization knowledge to help our customers implement requirements into an Android kernel and speed up the development process to meet operational needs.
Android kernel: the key to device performance
The Android kernel is an integral part of Android. While it has become more universal through the shift from ACK to GKI, developers still have plenty of angles to enhance and configure kernels off-standard. Memory footprint and power management are among the most popular features and are almost always worth considering for custom devices.
Prebuilt mainline GKIs usually strive to balance features and performance to meet the needs of numerous devices, including smartphones, tablets, and microcomputers. Features and settings may also differ between kernel versions, further promoting customization efforts. Modifying an Android kernel is essential to meet overall device requirements, paving the way for successful products.
Effective development of Android kernels requires a sound knowledge of the internals of the Linux and Android kernel to alter desired features without interfering with essential features such as Android security. This is where expertise comes in handy.
An optimal foundation
No more tradeoffs. Build devices to your needs. At emteria, we provide a platform and tools to create devices that exceed requirements. From Android custom ROM to Over-the-Air software updates. Build, maintain, and upgrade your device with trust by relying on emterias’ expert knowledge. Contact us to learn more about our Android customization offers!
Frequently asked questions
Does Android use the Linux kernel?
Yes and no. While Android and, more precisely, the Android kernel originates from Linux, the Android kernel only uses the kernel as a foundation. Significant differences exist in the design and use of both kernels, resulting from the primary focus and use for each operating system.
Which kernel is used in Android?
Android uses a Generic Kernel Image (GKI) from a stable mainline Linux kernel. Android-specific patches enhance this base Linux kernel to account for its different uses compared to Linux, forming the Android Kernel Image (AKI). There is no single kernel since these are often specifically tailored to a device.
How to build an Android kernel?
There are multiple options for building a custom Android kernel. The AOSP provides developers with all the tools and scripts to download, build, and embed the kernel into a custom Android build image. The kernel image is built using Bazel (Kleaf), which comes with the source repository.
What is the GKI kernel in Android?
The Generic Kernel Image (GKI) is the de facto standard for building and running kernels as part of Android build images. GKIs are prebuilt kernel images shared among all Android devices. Customizations are kept separately in vendor modules, thus allowing for independent and easier kernel updates.
Build unique products, boost device performance
See why emteria is the chosen Android™ customization & management platform for OEM solution builders — and what it can do for your team and customers.