[Android Software/Hacking General [Developers Only]] fabricioemmerick:
Guide to Compiling Custom Kernel Modules in Android

I've spent the better part of today trying to figure out how to compile and load a custom kernel modules in android to aid me in my research. It has been in entirely frustrating experience, as there is almost no documentation on the topic that I can find. Below you will find my attempt at a guide. Hopefully this will help save someone else the hassle.

PREREQUISITES

Disclaimer: This list may be incomplete, since I've not tried it on a fresh install. Please let me know if I've missed anything.

Install the general android prereqs found here .

Download and un(zip|tar) the android NDK found here .

http://developer.android.com/sdk/ndk/index.html

Download and un(zip|tar) the android SDK found here .

http://developer.android.com/sdk/index.html

Download and untar the kernel source for your device. This can usually be found on the website of your device manufacturer or by a quick Google search.

Root your phone. In order to run custom kernel modules, you must have a rooted phone.

Plug your phone into your computer.

PREPARING YOUR KERNEL SOURCE

First we must retrieve and copy the kernel config from our device.

$ cd /path/to/android-sdk/tools
$ ./adk pull /proc/config.gz
$ gunzip ./config.gz
$ cp config /path/to/kernel/.config

Next we have to prepare our kernel source for our module.

$ cd /path/to/kernel
$ make ARCH=arm CROSS_COMPILE=/path/to/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi- modules_prepare

PREPARING YOUR MODULE FOR COMPILATION

We need to create a Makefile to cross-compile our kernel module. The contents of your Makefile should be similar to the following:

obj-m := modulename.o
KDIR := /path/to/kernel
PWD := $(shell pwd)
CCPATH := /path/to/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
default:
$(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules

COMPILING AND INSTALLING YOUR MODULE

$ cd /path/to/module/src
$ make
$ cd /path/to/android-sdk/tools/
$ ./adb push /path/to/module/src/modulename.ko /sdcard/modulename.ko

RUNNING YOUR MODULE

$ cd /path/to/android-sdk/
$ ./adb shell
$ su
# insmod /sdcard/modulename.ko

:D:D:D:D

---------- Post added at 07:40 PM ---------- Previous post was at 07:37 PM ----------

IMPORNTANT TOO

Preparing a build environment

To build an Android kernel, you need a cross-compiling toolchain. Theoretically, any will do, provided it targets ARM. I just used the one coming in the Android NDK:

$ wget http://dl.google.com/android/ndk/android-ndk-r6b-linux-x86.tar.bz2

$ tar -jxf android-ndk-r6b-linux-x86.tar.bz2

$ export ARCH=arm

$ export CROSS_COMPILE=$(pwd)/android-ndk-r6b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-

For the latter, you need to use a directory path containing prefixed versions (such as arm-eabi-gcc orarm-linux-androideabi-gcc), and include the prefix, but not “gcc”.

You will also need the adb tool coming from the Android SDK. You can install it this way:

$ wget http://dl.google.com/android/android-sdk_r12-linux_x86.tgz

$ tar -zxf android-sdk_r12-linux_x86.tgz

$ android-sdk-linux_x86/tools/android update sdk -u -t platform-tool

$ export PATH=$PATH:$(pwd)/android-sdk-linux_x86/platform-tools