Appuals có vài hướng dẫn phát triển Android hay, như Build Custom ROM từ AOSP – nhưng thường hướng tới môi trường build Linux thuần túy.
Hướng dẫn này chỉ cách build Android kernel trên Windows 10 – vẫn dùng môi trường build Linux, nhưng là Linux subsystem trong Windows 10. Nếu bạn dùng Windows 10 quan tâm phát triển Android, hãy làm theo cẩn thận.
Cụ thể: build kernel cho thiết bị ARM và MediaTek, thêm tính năng, và tổng quan cơ bản về Git.
Yêu cầu
- Windows 10 x64 (có Fall Creator's update)
Thiết lập môi trường Linux
- Windows 10: Settings > Update and Security > For Developers > bật Developers Mode.
- Control Panel > Programs > Turn Windows Features On or Off > bật Windows Subsystem for Linux.
- Reboot PC.
- Khởi chạy Linux subsystem, cho qua quá trình tải. Đặt mật đừng quên.
- Vào Windows Store, tải Ubuntu.
- Chạy Ubuntu trên desktop Win10, nhập username/password.
- Trong Ubuntu, mở terminal native, gõ:
apt-get update - Cập nhật tất cả repo app và dependencies.
- Terminal gõ:
sudo apt-get install -y build-essential kernel-package libncurses5-dev bzip2 - Kiểm tra dependencies: gõ
gcc(không dấu ngoặc). Nếu đã cài, hiệngcc : fatal error : no input file. - Gõ
make. Nếu đã cài, hiệnmake: *** no target specified and no makefile found. stop. - Gõ
git. Nếu đã cài, hiện danh sách lệnh git cơ bản. - Cần toolchains (có GCC, Linaro, và custom). Một số thiết bị cần toolchain khác, không phải kernel nào cũng boot/compile với GCC.
Cho thiết bị ARM
Dùng GCC 4.7.
- Terminal Linux gõ:
mkdir kernel - Gõ:
cd kernel - (Không bắt buộc tên 'kernel', để đơn giản, đặt tên tùy ý.)
- Gõ:
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7
Cho thiết bị ARM 64
Cần compiler 64-bit cho ARM 64, ví dụ aarch64.
Lấy source file cho thiết bị
Phần khó: cần tìm GitHub repo host kernel source. Tìm trên XDA forums.
Ví dụ kernel source Git. Góc trên bên trái thấy "Branch: Completed by xxxx". Có các version kernel/project khác nhau, tách bằng "Testing", "Beta", "Final Release".
Cấu trúc thư mục kernel thường:
- /arch/arm/configs: Chứa config files cho thiết bị, governors, v.v.
- /output/arch/arm/boot/: Nơi lưu zimage.
- build.sh: Script đơn giản hóa build.
- /arm-cortex-linux-gnueabi-linaro_5.2-2015.11-2: Toolchain thường đặt trong kernel source, dễ tìm.
Tải kernel source về.
Mở terminal Linux, đảm bảo đang ở folder kernel (cd kernel).
Gõ: "git clone "URL of the github kernel" -b "name of the branch""
Ví dụ: "git clone https://github.com/atxoxx/android_ke...amsung_msm8974 -b xenomTW"
Build Kernel
Để dễ dàng, điều hướng trong file explorer. Thường là /home/user ID/kernel (hoặc tên folder kernel).
Sẽ thấy 2 folder: toolchain và kernel source. Vào folder kernel source.
Cho thiết bị ARM
Terminal gõ:
#!/bin/bash export ARCH=arm export CROSS_COMPILE= mkdir output make -C $(pwd) O=output "name of defconfig and variant if needed" make -j4 -C $(pwd) O=output
Giải thích lệnh:
- #!/bin/bash: Chạy script trong shell
- export ARCH=arm: Xác định kiến trúc kernel (ví dụ arm64)
- export CROSS_COMPILE=: Đường dẫn toolchain. Phải khớp chính xác, dấu gạch ngang cuối bắt buộc.
- mkdir output: Tạo thư mục lưu zimage đã compile
- make -C $(pwd) O=output: Xác định defconfig cho compile kernel
- make -j4 -C $(pwd) O=output: -j# cho tốc độ compile. Thường set theo CPU. -j32 trên CPU yếu gây unstable.
- cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage: Di chuyển image sang đường dẫn thứ 2
Ví dụ khác:
#!/bin/bash export ARCH=arm export CROSS_COMPILE=$(pwd)/arm-cortex-linux-gnueabi-linaro_5.2-2015.11-2/bin/arm-cortex-linux-gnueabi- mkdir output make -C $(pwd) O=output msm8974_sec_defconfig VARIANT_DEFCONFIG=msm8974_sec_ks01_skt_defconfig SELINUX_DEFCONFIG=selinux_defconfig make -j4 -C $(pwd) O=output cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage
Cho thiết bị ARM 64
#!/bin/bash export ARCH=arm64 export CROSS_COMPILE="path to your toolchain" (phải kết thúc kiểu "nameofarch-something-") mkdir output make -C $(pwd) O=output "name of defconfig and variant if needed" make -j4 -C $(pwd) O=output
Cho thiết bị Mediatek (MTK)
#!/bin/bash export CROSS_COMPILE="path to your toolchain" (phải kết thúc kiểu "nameofarch-something-") export ARCH=arm ARCH_MTK_PLATFORM= make "name of defconfig and variant if needed" make -j4
Hoàn tất bước cho kiến trúc kernel, terminal gõ: sudo bash build.sh
Nhập password user, bắt đầu compile. Thường không quá lâu, compile kernel không như compile toàn bộ ROM. Phụ thuộc CPU – ví dụ AMD Phenom X4 3.4GHz 8GB RAM mất ~10 phút.
Xong sẽ báo "zimage is ready".
Thiết bị ARM và ARM64
Vào /Output/arch/arm/boot/ tìm zimage.
Thiết bị Mediatek
Vào /arch/arm/boot/ tìm zimage.
Không phải build nào cũng ra file Zimage, đôi khi build thành format image khác.
Quan trọng: Nếu compile lại, khuyến khích chạy make clean và make mrproper trước khi bắt đầu lại.
Làm cho Kernel Boot
Có 2 lựa chọn.
Phương pháp anykernel (XDA user @osm0sis). Đọc toàn bộ tutorial, tóm tắt:
- Đặt zImage ở root (dtb/dtbo cũng ở đây cho thiết bị cần custom, fallback về original nếu không include)
- Đặt ramdisk files cần thiết trong /ramdisk, modules trong /modules (đầy đủ path như /modules/system/lib/modules)
- Đặt patch files cần thiết (thường là partial files kèm lệnh) trong /patch
- Sửa anykernel.sh: thêm tên kernel, vị trí boot partition, permission cho ramdisk files, methods cho ramdisk modifications (tùy chọn: đặt banner/version files ở root để hiển thị khi flash)
zip -r9 UPDATE-AnyKernel2.zip * -x .git README.md *placeholder
Phương pháp unpack boot.img từ cùng ROM (CM, TouchWiz, EMUI...) và cùng Android version. Swap Zimage. Quy trình phức tạp, đọc tutorial chính xác, tóm tắt:
- Unzip.
- Dùng command-line
unpackimg <image-filename.img>hoặc drag-drop image. Sẽ tách image và unpack ramdisk vào subdirectory. - Sửa ramdisk tùy ý.
- Script repackimg batch không cần input, chỉ recombine zImage đã tách với ramdisk mới pack dùng toàn bộ info image gốc (cũng tách và lưu).
- Script cleanup batch reset folder về trạng thái ban đầu, xóa split_img+ramdisk directories và packed ramdisk/image mới.
Trước khi flash kernel, backup stock boot.img, rồi flash kernel xem có boot được Android không.
Thêm tính năng vào Kernel
Thêm tính năng làm kernel "ngon" hơn. Nhiều thứ tweak: CPU governors, IO schedulers, overclock GPU, audio enhancements, v.v.
Ví dụ thêm governor (codename Intellimm).
Thấy trong 2 text box đầu: trong "arch/arm/configs/" "msm8974_sec_defconfig" và "cm_msm8974_sec_defconfig" đã sửa.
Giữa dòng 140-141 thêm text: "CONFIG_CPU_FREQ_GOV_INTELLIMM=y" (Bật Intellimm khi compile kernel)
Cùng kỹ thuật cho các text box khác (đã thêm/xóa gì và vị trí).
Tùy tính năng thêm, nhiều/ít file bị sửa, thêm, xóa.
Tóm lại, Commit cho thấy tất cả thay đổi!
Mẹo và thủ thuật chung
Đổi tên và version kernel:
Cách đơn giản:
Sửa dòng trong defconfig file:
"CONFIG_LOCALVERSION="-" sau - trong defconfig
Ví dụ: CONFIG_LOCALVERSION="-XenomTW-3.2.6"
Cách nâng cao:
Vào Makefile ở root folder kernel source.
Thêm dòng:
CONFIG_LOCALVERSION="nameofyourkernel" LOCALVERSION="versionofyourkernel"
KHÔNG sửa dòng Version, PatchLevel, Sublevel, Extraversion.
Phương pháp thay thế:
Vào scripts/mkcompile_h thêm:
LINUX_COMPILE_BY="nameofyourchoice" LINUX_COMPILE_HOST="nameofyourchoice"
Giải quyết vấn đề PATH:
Nếu lỗi "Is your path correct?", thử terminal Linux:
"export PATH="pathtotoolchainlocation"/bin:$PATH"
Truy cập folder Ubuntu từ Windows 10
Đường dẫn Ubuntu thường:
C:\Users"NAME"\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState \rootfs\home
Nhưng không nên edit file trực tiếp từ Windows, sẽ break permission – phải reset permission từ Linux terminal.