Build your own android kernel

Compile our own Android Kernel in 5 Simple Steps

The Android kernel helps the applications to communicate with the hardware components of the device.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

  1. Most of us are familiar with game mode. What it does is instructs the processor and the graphics processing unit to run at their maximum frequencies.
  2. Another example is power saver mode. It instructs the processor and the graphics processing unit to run at their minimum frequencies.

Need to compile our own kernel: Compiling our own kernel might prove very useful as:

  • The user experience can be further optimized as per the need using our own kernel
  • It can also help in Open Source Development.

Steps to compile our own kernel:

  1. Prerequisites: Below are the prerequisites required to compile our own Android Kernel:
    • Ubuntu or any other Linux based OS
    • Familiar with basic Linux Commands
    • Familiar with Github
    • Device source code

Note: This article is for a device with a 64bit Snapdragon SOC
Install Dependencies Open the terminal and paste the following:

Источник

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.

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).

Источник

Build your own android kernel

Compiling an Android kernel with Clang

Google compiles the Pixel 2 kernel with Clang. They shipped the device on Android 8.0 with a kernel compiled with Clang 4.0 (build.config commit and prebuilt kernel commit) and upgraded to Android 8.1 with a kernel compiled with Clang 5.0 (build.config commit and prebuilt kernel commit).

Google uses Clang’s link-time optimization and control flow integrity in the Pixel 3 kernel, hardening it against return oriented programming attacks (LTO commit, CFI commit).

Google started compiling all Chromebook 4.4 kernels with Clang in R67 (commit, LKML) and going forward, Clang is the default compiler for all future versions of the kernel (commit).

Further information including videos of talks on the motive behind compiling with Clang can be found in the ClangBuiltLinux wiki.

TL;DR: Helps find bugs, easier for Google since all of AOSP is compiled with Clang, compiler specific features such as link-time optimization, and better static analysis for higher code quality.

  • A compatible kernel (4.4 or 4.9 LTS work best)
  • arm64 or x86_64
  • Patience

msm-4.14 and newer uses clang by default so there is no need for a patch stack.

How to compile the kernel with Clang (standalone)

NOTE: I am not going to write this for beginnings. I assume if you are smart enough to pick some commits, you are smart enough to know how to run git clone and know the paths of your system.

/bin:$» \ make -j$(nproc —all) O=out \ ARCH=arm64 \ CC=clang \ CLANG_TRIPLE=aarch64-linux-gnu- \ CROSS_COMPILE=aarch64-linux-android- \ CROSS_COMPILE_ARM32=arm-linux-androideabi- «>

After compiling, you can verify the toolchain used by opening out/include/generated/compile.h and looking at the LINUX_COMPILER option.

A couple of notes:

  1. CLANG_TRIPLE is only needed when using AOSP’s version of Clang.
  2. export CC=clang does not work. You need to pass CC=clang to make like above.
  3. CROSS_COMPILE_ARM32 is needed in recent kernels to support the new compat vDSO.

How to compile the kernel with Clang (inline with a custom ROM)

  1. Add the Clang commits to your kernel source
  2. Make sure your ROM has this commit
  3. Add the following to your BoardConfig.mk file in your device tree: TARGET_KERNEL_CLANG_COMPILE := true

To test and verify everything is working:

  1. Build a kernel image: m kernel or m bootimage
  2. Open the out/target/product/*/obj/KERNEL_OBJ/include/generated/compile.h file and look at the LINUX_COMPILER option.

Getting the Clang patchset

The core Clang patchset comes from mainline. It has been backported to three places:

If your kernel has 4.4.165 or 4.9.139 and newer, you automatically have the patchset and can start building with Clang!

The branches in this repository will be dedicated to adding this patchset when it does not exist and enhancing it by fixing all of the warnings from Clang (from mainline, the Pixel 2 and 3, msm-4.9/msm-4.14, and my own knowledge).

msm-3.18-android-10 — based on kernel.lnx.3.18.r41-rel. All relevant configs should build with -Werror .

msm-4.4-android-10 — based on kernel.lnx.4.4.r38-rel. All relevant configs should build with -Werror .

msm-4.9-oreo — based on the latest Oreo branch for the Snapdragon 845 kernel.lnx.4.9.r7-rel. Uses sdm845-perf_defconfig .

msm-4.9-pie — based on the latest Pie branch for the Snapdragon 845 kernel.lnx.4.9.r11-rel. Uses sdm845-perf_defconfig .

msm-4.9-android-10 — based on kernel.lnx.4.9.r25-rel. All relevant configs should build with -Werror .

The general structure of these commits is as follows:

  1. The core compilation support (if needed)
  2. Fixing Qualcomm specific drivers to compile with Clang
  3. Fixing warnings that come from code in mainline
  4. Fixing warnings that come from code outside of mainline

You should pick the commits that I have committed (nathanchance).

Additionally, there are fixes for:

Every time there is a branch update upstream, the branch will be rebased, there is no stable history here! Ideally, I will not need to add any commits but I will do my best to keep everything in the same order.

NOTE: 3.18 Clang is not supported officially by an OEM. I’ve merely added it here as I decided to support it with my Pixel XL kernel.

How to get a Clang toolchain

You can either use a prebuilt version of Clang (like from AOSP) or build a version yourself from the upstream sources.

For prebuilts, I recommend AOSP’s Clang, which is used for both the kernel and the platform so it is highly tested. You can git clone that repo to always have access to the latest version available, which is what I recommend. That is currently Clang 9.0.8. If you would like to just download the minimum version of Clang supported for the branches in this repo, direct tarball links are provided below:

If you would like to build the latest version of Clang, you can do so with tc-build . There are a lot of fixes for the Linux kernel that happen in LLVM/Clang so staying up to date is critical. If you experience any issues with Clang and an Android kernel, please report them to this repo.

How to get binutils

binutils are used for assembling (and usually linking) the Linux kernel right now. When using AOSP Clang, you should use AOSP’s GCC to avoid weird incompatibility issues. If you want source-built binutils, there is a build-binutils.py script available in tc-build .

The preferred method for getting help is either opening an issue on this repository or joining the Linux kernel newbies chat on Telegram.

About

Information on compiling Android kernels with Clang

Источник

[Guide][Noobs Familiar]How To Build Android Kernel With Features!

Albe96

Senior Member

What Is Kernel?
The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system.[1] It is the first program loaded on start-up. It handles the rest of start-up as well as input/output requests from software, translating them into data-processing instructions for the central processing unit. It handles memory and peripherals like keyboards, monitors, printers, and speakers.

Is There A Connection Between Kernel And Android?
Haha,Sorry but yes.Kernel is the main component for Android.Basically Android devices use the Linux kernel, but it’s not the exact same kernel other Linux-based operating systems use. There’s a lot of Android specific code built in, and Google’s Android kernel maintainers have their work cut out for them. OEMs have to contribute as well, because they need to develop hardware drivers for the parts they’re using for the kernel version they’re using. This is why it takes a while for independent Android developers and hackers to port new versions to older devices and get everything working. Drivers written to work with the Gingerbread kernel on a phone won’t necessarily work with the Ice Cream Sandwich kernel. And that’s important, because one of the kernel’s main functions is to control the hardware. It’s a whole lot of source code, with more options while building it than you can imagine, but in the end it’s just the intermediary between the hardware and the software. So basically if any instruction is given to mobile it first gives the command to kernel for the particular task execution.​

Open The Terminal and Paste following Command!

Upgrade The Built-In Environments!

Now When It Comes About Kernel Source,Where You’ll Find That?

No Worries,Search In Your Device Open Source Projects Websites To Get The Source OR You May Met With Github. Search In Github For Kernel Source For Your Device!

Here Are Some Sites,Where You Can Download Kernel Source Though:

Note: If You’ve Download The Source Then Extract It In A Directory. Or If You Want To Clone/Download Source From Github Then Follow Next Steps! ​

To Clone From Github,You Have To Install Repo Tool!

A. Open The Terminal and Paste Following Command!

2.Then Click On Clone Or Download Button,And Copy The Link!

B. Open A Terminal And Type This Command!

Explaination:
1. —>> Replace With The Link You Copied From Github To Clone The Source!
2. android/kernel ->> This Is My Directory Where I Want To Clone It!

Источник

Читайте также:  Лаунчер для samp android
Оцените статью