第一步:下载源代码
见网站 https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
需要注意 Android源码的版本与手机是有对应的,具体可见 https://source.android.com/source/build-numbers.html#source-code-tags-and-builds
如果直接从google的源去下载android代码,需要翻墙 挂vpn,所以建议使用这个清华的源。
第二步:下载对应设备Nexus5的硬件驱动并抽取相关数据
从谷歌的网站 https://developers.google.com/android/drivers#hammerheadlrx22c 上下载Nexus5的驱动
下载的三个.sh文件,解压后放在源代码的根目录中,执行后 会生成vendor文件夹,驱动相关的文件都会被抽取到这个文件夹中。
接下来就可以开始编译了。。。
第三步:开始编译
在安卓源代码的根目录下输入:
$ source build/envsetup.sh
$ lunch
1. aosp_arm-eng 2. aosp_arm64-eng 3. aosp_mips-eng 4. aosp_mips64-eng 5. aosp_x86-eng 6. aosp_x86_64-eng 7. aosp_deb-userdebug 8. aosp_flo-userdebug 9. full_fugu-userdebug 10. aosp_fugu-userdebug 11. aosp_grouper-userdebug 12. aosp_tilapia-userdebug 13. mini_emulator_arm64-userdebug 14. mini_emulator_arm-userdebug 15. mini_emulator_mips-userdebug 16. mini_emulator_x86-userdebug 17. mini_emulator_x86_64-userdebug 18. aosp_hammerhead-userdebug 19. aosp_mako-userdebug 20. aosp_shamu-userdebug 21. aosp_manta-userdebug
然后选择Nexus5 对应的版本aosp_hammerhead-userdebug 对应关系可以从google官方XX获得。
$ 18
$ make -j8
第四步:开始刷机
电脑连接手机后,在安卓源代码的根目录下输入:
$ sudo adb reboot bootloader
手机随后就会进入recovery模式
$ fastboot flashall –w
(或者 fastboot -w flashall)
等待几分钟后,手机成功重启,刷机成功。
zero@zero-HP-Pro-3380-MT:/media/zero/ANDROID/AOSP5.0$ fastboot -w flashall Creating filesystem with parameters: Size: 13725835264 Block size: 4096 Blocks per group: 32768 Inodes per group: 8144 Inode size: 256 Journal blocks: 32768 Label: Blocks: 3351034 Block groups: 103 Reserved block group size: 823 Created filesystem with 11/838832 inodes and 93654/3351034 blocks Creating filesystem with parameters: Size: 734003200 Block size: 4096 Blocks per group: 32768 Inodes per group: 7472 Inode size: 256 Journal blocks: 2800 Label: Blocks: 179200 Block groups: 6 Reserved block group size: 47 Created filesystem with 11/44832 inodes and 5813/179200 blocks -------------------------------------------- Bootloader Version...: HHZ11k Baseband Version.....: M8974A-1.0.25.0.23 Serial Number........: 0816a5d400ede284 -------------------------------------------- checking product... OKAY [ 0.100s] sending 'boot' (8852 KB)... OKAY [ 0.482s] writing 'boot'... OKAY [ 0.776s] sending 'recovery' (9552 KB)... OKAY [ 0.545s] writing 'recovery'... OKAY [ 0.810s] erasing 'system'... OKAY [ 1.070s] sending 'system' (354297 KB)... OKAY [ 11.312s] writing 'system'... OKAY [ 25.338s] erasing 'userdata'... OKAY [ 8.542s] sending 'userdata' (137318 KB)... OKAY [ 4.521s] writing 'userdata'... OKAY [ 9.131s] erasing 'cache'... OKAY [ 0.642s] sending 'cache' (13348 KB)... OKAY [ 0.630s] writing 'cache'... OKAY [ 1.071s] rebooting... finished. total time: 65.651s
刷机常见问题:
问题一:输入sudo adb reboot bootloader
提示error: device not found
解决:手机中开发者模式里面,一定要打开USB调试。
问题二:如果打开USB调试后,仍然ADB无法连接。
解决:
先在/etc/udev/rules.d/目录下新建一个文件51-Android.rules然后在终端输入lsusb查看usb连接情况:
zero@zero-HP-Pro-3380-MT:~$ lsusb Bus 002 Device 003: ID 0480:0210 Toshiba America Info. Systems, Inc. Bus 002 Device 007: ID 18d1:4ee0 Google Inc.
据第二行这个google手机的ID:18d1 和 4ee0 这两个数值
编辑文件51-Android.rules输入
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee0", MODE="0666", OWNER="<username>"
然后保存
$sudo chmod a+rx /etc/udev/rules.d/51-Android.rules
$sudo service udev restart
问题三:完成问题二的修改的基础上,在手机的fastboot模式下,输入fastboot -w flashall 提示< waiting for device >
这是由于fastboot权限不够,输入fastboot -l devices 会提示 no permissions fastboot
adb调试是在手机开机的状态下进行,而fastboot是在手机关机的时候进行。两者有区别,fastboot的调试需要的是root权限。必须把fastboot的拥有者设置为root。
因此,
$ which fastboot
找到fastboot所在的路径
然后
$ cd xxxx(fastboot的路径)
$ sudo chown root:root fastboot
$ sudo chmod +s fastboot
然后记得必须拔掉usb连接线,重新连接才能有效。