How build android kernel

How to Build a Custom Android Kernel

If you’ve ever wondered “how to build an Android kernel”, this guide is for you. Building your own kernel can be a rewarding experience as it will give you a greater degree of control over your Android device, from the CPU, RAM, GPU to even the battery.

This is a very hands-on process that involves a lot of compiling and console commands, but if you’re familiar with Linux (or good at following directions) it should be no problem.

Please note this guide is for non-Mediatek devices. Appual’s has a kernel compiling guide specific ally for Mediatek-based Android devices here: How to Build Mediatek Android Kernel from Source

Other Appual’s articles of interest include:

If you’re building a custom kernel, you will just need to clone the kernel from Git with commands provided below. But if you’re compiling a stock kernel, you need to know where to get the original kernel from source (for all sorts of reasons).

Original Kernel Sources for Various Brands:

For downloading the kernel, either use a git clone or download the tarball file and extract it.

Here is the git command:

So as an example, this would be the command to grab the latest Nexus 6P Nougat 3.10 kernel from Google:
git clone -b android-msm-angler-3.10-nougat-mr2 https://android.googlesource.com/kernel/msm/ angler

This should clone the kernel / msm repo into an angler folder, and automatically checkout the android-msm-angler-3.10-nougat-mr2.

Now because most Android devices are ARM based, we’ll need to use a compiler that will target ARM devices – this means a host/native compiler won’t work, unless you’re compiling on another ARM device. You have a few options here. You can either compile one yourself if you know how, using something like Crosstool-NG. Alternatively, you can download a prebuilt compiler – such as the one Google provides for Arm 32-bit and Arm64.

Before downloading a prebuilt compiler, you need to know the exact architecture of your device, so use an app like CPU-Z to determine it.

One other popular toolchain would be UberTC – but for any kernels higher than 4.9, you’ll need to patch them, and compiling with Google’s toolchain first is best practice.

In any case, once you’ve decided on the toolchain, you need to clone it.
git clone

Now point the Makefile to your compiler, running it from within the toolchain folder.

Now tell the Makefile your device architecture.

  • export ARCH=arm64 && export SUBARCH=arm64

Next locate the developer’s proper config file for the kernel you are building. It should typically be in the form of _defconfig or _defconfig. The defconfig will instruct the compiler what options to include in the kernel.

Generic Qualcomm configs may also be found, these will usually be something like (msm-perf_defconfig, msmcortex-perf_defconfig).

Building the Kernel

When those commands are successful, you should have: an Image, Image-dtb, Image.gz, or Image.gz-dtb file at the end.

If those commands failed, you might need to specify the output directory when making a new CAF based kernel, like this:

mkdir -p out
make O=out clean
make O=out mrproper
make O=out
make O=out -j$(nproc –all)

If it still doesn’t want to work, something is broken – check your headers or bring it up with the kernel developers.

Читайте также:  Kiosk mode android пароль

If the kernel was successfully compiled, you now need to flash it. There are two different ways of doing this – you can unpack and repack the bootimage using either Android Image Kitchen or AnyKernel2.

There may also be some nuances based on specific devices – you’ll need to ask your device developers for assistance if this is the case.

Flashing the Kernel in Android Image Kitchen

Extract your Android device’s boot image from the latest available image (whether stock or custom ROM).

Now unpack the image using this code:
unpackimg.sh .img

Next locate the zImage file, and replace it with your compiled kernel image – rename it to what was in the boot image.

Now run this code to repack the image:
repackimg.sh

Now you can flash the new boot image using fastboot, TWRP, etc.

Flashing the Kernel in AnyKernel2

Download the latest AnyKernel2

Apply this patch to flush out all the demo files.
wget https://github.com/nathanchance/AnyKernel2/commit/addb6ea860aab14f0ef684f6956d17418f95f29a.diff
patch -p1
diff —git a/Makefile b/Makefile
index 1aaa760f255f..bfccd5594630 100644
— a/Makefile
+++ b/Makefile
@@ -326,7 +326,7 @@ include $(srctree)/scripts/Kbuild.include
AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
-REAL_CC = $(CROSS_COMPILE)gcc
+CC = $(CROSS_COMPILE)gcc
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm
@@ -340,10 +340,6 @@ DEPMOD = /sbin/depmod
PERL = perl
CHECK = sparse
-# Use the wrapper for the compiler. This wrapper scans for new
-# warnings and causes the build to stop upon encountering them.
-CC = $(srctree)/scripts/gcc-wrapper.py $(REAL_CC)

CHECKFLAGS := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \
-Wbitwise -Wno-return-void $(CF)
CFLAGS_MODULE =

Using a higher GCC toolchain (5.x, 6.x, 7.x or even 8.x) will require you to nuke the GCC wrapper script as above and use a unified GCC header file (pick the following if you have an include/linux/compiler-gcc#.h file):

Even if you get a lot of warnings, they’re not necessary to fix (typically).

Источник

How to Build Android Kernel on Windows 10

Appual’s has a few great guides on Android development, such as How to Build a Custom ROM from Android Open Source Project – but these guides are usually geared towards a pure Linux build environment.

In this guide, we are going to show you how to build an Android kernel on Windows 10 – yes, we will still be using a Linux build environment, but it will be a Linux subsystem within Windows 10. So if you are a Windows 10 user interested in developing for Android, follow our guide carefully.

In this guide, we will specifically be learning how to build a kernel for ARM and MediaTek devices, adding features, and a basic overview of using Git.

Requirements

  • Windows 10 x64 (with Fall Creator’s update)

Setting up the Linux Environment

  1. On Windows 10, go to Settings > Update and Security > For Developers > enable Developers Mode.
  2. Now go to Control Panel > Programs > Turn Windows Features On or Off > enable Windows Subsystem for Linux.
  3. Reboot your PC.
  4. Launch the Linux subsystem, and allow it to go through the download process. Set a password and don’t lose it.
  5. Now go to the Windows app store, and download Ubuntu.
  6. Launch Ubuntu on the Windows 10 desktop, and it will request a username and password.
  7. In Ubuntu, launch the native terminal and type the following command: apt-get update
  8. This will proceed to update all repos for apps and dependencies.
  9. Next in the terminal type: sudo apt-get install -y build-essential kernel-package libncurses5-dev bzip2
  10. In order to check if all dependencies were correctly installed, type ‘gcc’ in the terminal (no quotes).
  11. If “gcc” is already installed, you should see “gcc : fatal error : no input file”
  12. Now you can type ‘make’ in the terminal. If “make” is already installed, you should see “make: *** no target specified and no makefile found. stop.”
  13. Next type ‘git’, and iff “git” is already installed, you should see bunch of basic git commands.
  14. Now we need some toolchains (there are several types, including GCC, Linaro, and a handful of custom ones). Some devices may require different toolchains, for example, not all device kernels will boot or compile with GCC.

For ARM Devices

We will be using GCC 4.7 for this.

  1. Open the Linux terminal and type: mkdir kernel
  2. Now type: cd kernel
  3. (it doesn’t have to be ‘kernel’, this is for simplicity sake, you can name it whatever you want.)
  4. Now type: git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7

For ARM 64 Devices

You need a 64-bit kernel compiler for ARM 64 devices, for example aarch64.

Getting the Source Files for your Device

This is a tricky part, as you need to find a GitHub repo that hosts your kernel source. You will of course need to search for it, most likely it can be found on XDA forums.

On the upper left side, you should see “Branch: Completed by xxxx”.

There are different versions of a kernel / project, usually separated by “Testing”, “Beta”, “Final Release”, etc.

The kernel folders are typically as follows:

  • /arch/arm/configs: This contains various config files for the device, such as governors, etc.
  • /output/arch/arm/boot/: This is where the zimage will be stored.
  • build.sh: A script that will simplify the building process.
  • /arm-cortex-linux-gnueabi-linaro_5.2-2015.11-2: This is typically a toolchain placed in the kernel source, thus making it easier to find.

You are going to need to download your kernel source.

Open the Linux terminal and make sure that you are in the kernel folder you previously created (cd kernel).

Then type in terminal: “git clone “URL of the github kernel” -b “name of the branch”

Building the Kernel

To make it easier, you can navigate to the location in the file explorer. It should be /home/user ID/kernel (or whatever you named the kernel folder).

You should see two folders inside, for the toolchain and the kernel source. Go inside the kernel source folder.

For ARM Devices

In a terminal, type the following commands:

Here is an overview of what these commands do, to make this easier in the future.

  • #!/bin/bash: Tells the script to run in shell command
  • export ARCH=arm: Defining which kernel architecture type it is (For example arm64 etc)
  • export CROSS_COMPILE= : Locate where the toolchain is. It must match the exact path, and the dash in the end is really mandatory.
  • mkdir output: This creates a directory for saving the compiled zimage
  • make -C $(pwd) O=output : Defining defconfig for guiding kernel compilation.
  • make -j4 -C $(pwd) O=output: When the building process begins, -j# tells it how fast to try and compile. Typically, you set this number according to your CPU. Setting on -j32 on a budget CPU, for example, would probably cause massive instability.
  • cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage: This is for moving image to a second path.

For ARM 64 Devices

For Mediatek (MTK) Devices

When you have completed the steps necessary to your kernel architecture, you can type in the terminal: sudo bash build.sh

You will then enter your user password, and the compiling process will begin.

It may take a while but typically not very long, compiling a kernel is not like compiling an entire Android ROM. This is really CPU dependent – for example, an AMD Phenom X4 3.4GHz with 8GB of RAM should take around 10 minutes to compile from start to finish.

When it finishes, it should notify you with a message like “zimage is ready”.

ARM and ARM64 Devices

Go to “/Output/arch/arm/boot/” to find your zimage.

Mediatek Devices

Go to “/arch/arm/boot/” to find your zimage.

Not all kernel builds will result in a Zimage file, it can sometimes be built as other image formats.

Important: If you are going to compile again, it is recommended that you enter the commands make clean and make mrproper before you begin the compile process again.

Making the Kernel Boot

There are two options for you to choose.

You can either use the anykernel method (as defined by XDA user @osm0sis in this XDA thread). You should read the entire tutorial, but a summary of the steps is as follows:

  1. Place zImage in the root (dtb and/or dtbo should also go here for devices that require custom ones, each will fallback to the original if not included)
  2. Place any required ramdisk files in /ramdisk and modules in /modules (with the full path like /modules/system/lib/modules)
  3. Place any required patch files (generally partial files which go with commands) in /patch
  4. Modify the anykernel.sh to add your kernel’s name, boot partition location, permissions for included ramdisk files, and use methods for any required ramdisk modifications (optionally, also place banner and/or version files in the root to have these displayed during flash)
  5. `zip -r9 UPDATE-AnyKernel2.zip * -x .git README.md *placeholder`

The other method you have available is unpacking the boot.img from the same ROM (such as CM, TouchWiz, EMUI, etc.) and the same Android version. You would then swap the Zimage. Again, this is a really complicated process and you should read the exact tutorial, but a summary of the steps is:

  1. Unzip.
  2. Either use the command-line “unpackimg ”, or simply drag-and-drop the image. This will split the image and unpack the ramdisk to a subdirectory.
  3. Alter the ramdisk as you like.
  4. The repackimg batch script requires no input and simply recombines the previously split zImage with the newly packed modified ramdisk using all the original image information (which was also split and saved).
  5. The cleanup batch script resets the folder to its initial state, removing the split_img+ramdisk directories and any new packed ramdisk or image files.

Before you flash your kernel, you should create a backup of your stock boot.img, and then flash your kernel to see if it allows your Android system to boot.

Adding Features to Your Kernel

Adding features to your kernel is a great way to spice it up. There are a lot of things you can tweak, such as CPU governors, IO schedulers, overclocking the GPU, audio enhancements, etc.

An example for adding a governor is here (this governor is codenamed Intellimm).

We can see in the first 2 text boxes that in “arch/arm/configs/” “msm8974_sec_defconfig” and “cm_msm8974_sec_defconfig” have been modified.

Between the lines 140 and 141 of this files this text has been added : “CONFIG_CPU_FREQ_GOV_INTELLIMM=y”
(This line is for enabling Intellimm when you’re compiling your kernel)

Same technique applies to the other text boxes (what has been added and deleted and it’s location)

Depending on the features you add, more or less files can be modified, added or deleted.

So to sum it up, a Commit let’s you see all the changes which have been made and everything else!

General Tips and Tricks

How to change the kernel name and version:

The simple method:

Edit this line in your defconfig file:

The advanced methods:

Navigate to the Makefile in the root folder of your kernel source.

Add these lines:

DO NOT modify the lines Version, PatchLevel, Sublevel, or Extraversion.

Alternative method:

Go to scripts/mkcompile_h and add these lines:

Solving PATH problems:

If you encounter the error “Is your path correct?”, try this in the Linux terminal:

Accessing your Ubuntu folders from Windows 10

Your path to Ubuntu should typically be:

But you should not edit files directly from Windows, as this will typically break the permissions on them – you would then need to reset the permissions from within Linux terminal.

Источник

Оцените статью