Android实战技巧之四十:Android5.1.1源代码编译与烧写

购买Nexus手机的朋友大多是为了自己修改系统玩,再加上其较高的性价比,在开发者中还是广受欢迎的。我的5太子被我升级到了6.0预览版,玩的正嗨,舍不得换回到5.1时代了。不过鉴于距6.0源码发布还有段日子,5.1的源码编译与烧写仍是主流,下面就记录了整个过程(持续了很长时间,我们要有耐心)。

搭建开发环境

系统推荐Ubuntu 14.04
1.openjdk is needed

$ sudo apt-get update
$ sudo apt-get install openjdk-7-jdk

set it the default java version

$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac

2.required package

sudo apt-get install bison g++-multilib git gperf libxml2-utils make python-networkx zlib1g-dev:i386 zip

遇到问题:

$ sudo apt-get install bison g++-multilib git gperf libxml2-utils make python-networkx zlib1g-dev:i386 zip
[sudo] password for linc: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
make is already the newest version.
zip is already the newest version.
git is already the newest version.
git set to manually installed.
libxml2-utils is already the newest version.
libxml2-utils set to manually installed.
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 g++-multilib : Depends: gcc-multilib (>= 4:4.8.2-1ubuntu6) but it is not going to be installed
E: Unable to correct problems, you have held broken packages

按照如下步骤即可:

$ sudo apt-get install g++-multilib
$ sudo apt-get install  bison git gperf libxml2-utils make python-networkx zlib1g-dev:i386 zip

gcc为必须

$ sudo apt-get install gcc

$ gcc -v
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04) 

repo

$ mkdir ~/bin
$ PATH=~/bin:$PATH

$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo

check sh1 sum:

$ sha1sum repo 
b8bd1804f432ecf1bab730949c82b93b0fc5fede  repo

For version 1.21, the SHA-1 checksum for repo is b8bd1804f432ecf1bab730949c82b93b0fc5fede

repo init

针对Nexus5的5.1.1的初始化如下:

LMY48I  android-5.1.1_r9    Lollipop    Nexus 4, Nexus 5, Nexus 6, Nexus 7 (flo), Nexus 9 (volantis/volantisg), Nexus 10
$ repo init -u https://android.googlesource.com/platform/manifest -b android-5.1.1_r9

Your identity is: linc <xxx@xxx.com>
If you want to change this, please re-run 'repo init' with --config-name

Testing colorized output (for 'repo diff', 'repo status'):
  black    red      green    yellow   blue     magenta   cyan     white 
  bold     dim      ul       reverse 

repo has been initialized in /home/linc/source-android/android-source

repo sync

$ repo sync

一个晚上两个白天,终于下载完成,代码达34G.

Setting up ccache

Put the following in your .bashrc (or equivalent):

export USE_CCACHE=1

run the command:

prebuilts/misc/linux-x86/ccache/ccache -M 50G

Building the System

1.Set up environment

$ source build/envsetup.sh

or

$ . build/envsetup.sh

2.Choose a Target

$ lunch 

You're building on Linux

Lunch menu... pick a combo:
     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_manta-userdebug
     8. aosp_flo-userdebug
     9. aosp_deb-userdebug
     10. full_fugu-userdebug
     11. aosp_fugu-userdebug
     12. aosp_tilapia-userdebug
     13. aosp_grouper-userdebug
     14. aosp_mako-userdebug
     15. aosp_hammerhead-userdebug
     16. aosp_flounder-userdebug
     17. aosp_shamu-userdebug
     18. mini_emulator_x86-userdebug
     19. mini_emulator_arm64-userdebug
     20. mini_emulator_x86_64-userdebug
     21. mini_emulator_mips-userdebug
     22. m_e_arm-userdebug

Which would you like? [aosp_arm-eng] 

Nexus5就选择了15,如果只是用于模拟器就选择1好了。
3.build

make -j4

如果遇到等待其他任务完成的错误,是多线程冲突,就直接make好了。
4.error
1)

No private recovery resources for TARGET_DEVICE hammerhead
host C++: bcc <= frameworks/compile/libbcc/tools/bcc/Main.cpp
clang++: error: unable to execute command: Executable "as" doesn't exist!
clang++: error: assembler command failed with exit code 1 (use -v to see invocation)
make: *** [out/host/linux-x86/obj32/EXECUTABLES/bcc_intermediates/Main.o] Error 1

try make clean.这是没有安装gcc的后果。

build successfully

#### make completed successfully (05:53:54 (hh:mm:ss)) ####

目录结构如下:

out/target/product/hammerhead$ du -sh *
4.0K    android-info.txt
8.8M    boot.img
4.0K    cache
14M cache.img
64K clean_steps.mk
172K    data
4.0K    fake_packages
81M gen
64K installed-files.txt
8.1M    kernel
16G obj
4.0K    previous_build_config.mk
700K    ramdisk.img
1.4M    ramdisk-recovery.img
2.2M    recovery
9.5M    recovery.img
1.3M    root
2.9G    symbols
288M    system
307M    system.img
135M    userdata.img

Flash device

To flash a device, you will need to use fastboot, which should be included in your path after a successful build. Place the device in fastboot mode either manually by holding the appropriate key combination at boot, or from the shell with

$ adb reboot bootloader

Once the device is in fastboot mode, run

$ fastboot flashall -w

The -w option wipes the /data partition on the device; this is useful for your first time flashing a particular device but is otherwise unnecessary.

For more information about building for and running on actual hardware, see Running Builds.

Flash emulator

1.build generic img
lunch 1即可,然后正常make。

make completed successfully (05:02:47 (hh:mm:ss))

2.启动模拟器

~/source-android/android-source/out/target/product/generic$ emulator -sysdir ~/source-android/android-source/out/target/product/generic -system system.img 
emulator: WARNING: system partition size adjusted to match image file (550 MB > 200 MB)

emulator: WARNING: data partition size adjusted to match image file (550 MB > 200 MB)

Creating filesystem with parameters:
    Size: 69206016
    Block size: 4096
    Blocks per group: 32768
    Inodes per group: 4224
    Inode size: 256
    Journal blocks: 1024
    Label: 
    Blocks: 16896
    Block groups: 1
    Reserved block group size: 7
Created filesystem with 11/4224 inodes and 1302/16896 blocks

模拟器顺利启动,与真机的区别有很多,比如开机画面不同,Home的UI和操控都有所不同。具体原因敬请期待。

Android系统源代码情景分析》随书光盘内容(源代码) 目录如下: 第1篇 初识Android系统 第1章 准备知识 1.1 Linux内核参考书籍 1.2 Android应用程序参考书籍 1.3 下载、编译和运行Android源代码 1.3.1 下载Android源代码 1.3.2 编译Android源代码 1.3.3 运行Android模拟器 1.4 下载、编译和运行Android内核源代码 1.4.1 下载Android内核源代码 1.4.2 编译Android内核源代码 1.4.3 运行Android模拟器 1.5 开发第一个Android应用程序 1.6 单独编译和打包Android应用程序模块 1.6.1 导入单独编译模块的mmm命令 1.6.2 单独编译Android应用程序模块 1.6.3 重新打包Android系统镜像文件 第2章 硬件抽象层 2.1 开发Android硬件驱动程序 2.1.1 实现内核驱动程序模块 2.1.2 修改内核Kconfig文件 2.1.3 修改内核Makefile文件 2.1.4 编译内核驱动程序模块 2.1.5 验证内核驱动程序模块 2.2 开发C可执行程序验证Android硬件驱动程序 2.3 开发Android硬件抽象层模块 2.3.1 硬件抽象层模块编规范 2.3.2 编硬件抽象层模块接口 2.3.3 硬件抽象层模块的加载过程 2.3.4 处理硬件设备访问权限问题 2.4 开发Android硬件访问服务 2.4.1 定义硬件访问服务接口 2.4.2 实现硬件访问服务 2.4.3 实现硬件访问服务的JNI方法 2.4.4 启动硬件访问服务 2.5 开发Android应用程序来使用硬件访问服务 第3章 智能指针 3.1 轻量级指针 3.1.1 实现原理分析 3.1.2 应用实例分析 3.2 强指针和弱指针 3.2.1 强指针的实现原理分析 3.2.2 弱指针的实现原理分析 3.2.3 应用实例分析 第2篇 Android专用驱动系统 第4章 Logger日志系统 4.1 Logger日志格式 4.2 Logger日志驱动程序 4.2.1 基础数据结构 4.2.2 日志设备的初始化过程 4.2.3 日志设备文件的打开过程 4.2.4 日志记录的读取过程 4.2.5 日志记录的入过程 4.3 运行时库层日志库 4.4 C/C++日志入接口 4.5 Java日志入接口 4.6 Logcat工具分析 4.6.1 相关数据结构 4.6.2 初始化过程 4.6.3 日志记录的读取过程 4.6.4 日志记录的输出过程 第5章 Binder进程间通信系统 5.1 Binder驱动程序 5.1.1 基础数据结构 5.1.2 Binder设备的初始化过程 5.1.3 Binder设备文件的打开过程 5.1.4 Binder设备文件的内存映射过程 5.1.5 内核缓冲区管理 5.2 Binder进程间通信库 5.3 Binder进程间通信应用实例 5.4 Binder对象引用计数技术 5.4.1 Binder本地对象的生命周期 5.4.2 Binder实体对象的生命周期 5.4.3 Binder引用对象的生命周期 5.4.4 Binder代理对象的生命周期 5.5 Binder对象死亡通知机制 5.5.1 注册死亡接收通知 5.5.2 发送死亡接收通知 5.5.3 注销死亡接收通知 5.6 Service Manager的启动过程 5.6.1 打开和映射Binder设备文件 5.6.2 注册为Binder上下文管理者 5.6.3 循环等待Client进程请求 5.7 Service Manager代理对象的获取过程 5.8 Service组件的启动过程 5.8.1 注册Service组件 5.8.2 启动Binder线程池 5.9 Service代理对象的获取过程 5.10 Binder进程间通信机制的Java接口 5.10.1 Service Manager的Java代理对象的获取过程 5.10.2 Java服务接口的定义和解析 5.10.3 Java服务的启动过程 5.10.4 Java服务代理对象的获取过程 5.10.5 Java服务的调用过程 第6章 Ashmem匿名共享内存系统 6.1 Ashmem驱动程序 6.1.1 基础数据结构 6.1.2 匿名共享内存设备的初始化过程 6.1.3 匿名共享内存设备文件的打开过程 6.1.4 匿名共享内存设备文件的内存映射过程 6.1.5 匿名共享内存块的锁定和解锁过程 6.1.6 匿名共享内存块的回收过程 6.2 运行时库cutils的匿名共享内存访问接口 6.3 匿名共享内存的C++访问接口 6.3.1 MemoryHeapBase 6.3.2 MemoryBase 6.3.3 应用实例 6.4 匿名共享内存的Java访问接口 6.4.1 MemoryFile 6.4.2 应用实例 6.5 匿名共享内存的共享原理 第3篇 Android应用程序框架 第7章 Activity组件的启动过程 7.1 Activity组件应用实例 7.2 根Activity组件的启动过程 7.3 子Activity组件在进程内的启动过程 7.4 子Activity组件在新进程中的启动过程 第8章 Service组件的启动过程 8.1 Service组件应用实例 8.2 Service组件在新进程中的启动过程 8.3 Service组件在进程内的绑定过程 第9章 Android系统广播机制 9.1 广播机制应用实例 9.2 广播接收者的注册过程 9.3 广播的发送过程 第10章 Content Provider组件的实现原理 10.1 Content Provider组件应用实例 10.1.1 ArticlesProvider 10.1.2 Article 10.2 Content Provider组件的启动过程 10.3 Content Provider组件的数据共享原理 10.3.1 数据共享模型 10.3.2 数据传输过程 10.4 Content Provider组件的数据更新通知机制 10.4.1 注册内容观察者 10.4.2 发送数据更新通知 第11章 Zygote和System进程的启动过程 11.1 Zygote进程的启动脚本 11.2 Zygote进程的启动过程 11.3 System进程的启动过程 第12章 Android应用程序进程的启动过程 12.1 应用程序进程的创建过程 12.2 Binder线程池的启动过程 12.3 消息循环的创建过程 第13章 Android应用程序的消息处理机制 13.1 创建线程消息队列 13.2 线程消息循环过程 13.3 线程消息发送过程 13.4 线程消息处理过程 第14章 Android应用程序的键盘消息处理机制 14.1 键盘消息处理模型 14.2 InputManager的启动过程 14.2.1 创建InputManager 14.2.2 启动InputManager 14.2.3 启动InputDispatcher 14.2.4 启动InputReader 14.3 InputChannel的注册过程 14.3.1 创建InputChannel 14.3.2 注册Server端InputChannel 14.3.3 注册系统当前激活的应用程序窗口 14.3.4 注册Client端InputChannel 14.4 键盘消息的分发过程 14.4.1 InputReader获得键盘事件 14.4.2 InputDispatcher分发键盘事件 14.4.3 系统当前激活的应用程序窗口获得键盘消息 14.4.4 InputDispatcher获得键盘事件处理完成通知 14.5 InputChannel的注销过程 14.5.1 销毁应用程序窗口 14.5.2 注销Client端InputChannel 14.5.3 注销Server端InputChannel 第15章 Android应用程序线程的消息循环模型 15.1 应用程序主线程消息循环模型 15.2 与界面无关的应用程序子线程消息循环模型 15.3 与界面相关的应用程序子线程消息循环模型 第16章 Android应用程序的安装和显示过程 16.1 应用程序的安装过程 16.2 应用程序的显示过程
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值