下载Android上层系统源代码
建立Android源码目录
$ mkdir WORKING_DIRECTORY
$ cd WORKING_DIRECTORY
初始化repo
repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1
-u为源码的git服务器地址,-b为源码的某个分支,没有-b就checkout出master分支。
如果不清楚源码服务器上的分支,可以列出远程服务器上的分支。
git ls-remote --tags https://android.googlesource.com/platform/manifest
目录refs包含tags和heads两个子目录,其中存放了不同分支的头的索引,可以通过索引查看有哪些branch。
下载源代码
repo sync
下载指定模块源代码
查看有哪些模块可以下载
repo manifest -o -
上述命令等于读取的是本地源码目录中的.repo/manifest/default.xml文件,可以直接打开看这个文件。
下载模块源代码
repo sync platform/system/core
SDK编译
编译Android源码和内核时不会自动编译SDK,可以下载官方提供的SDK,也可以自己手动编译SDK。
开发应用程序的时候,常常通过SDK所带的模拟器来调试APK应用程序,比在真机上高效和便捷,模拟器可以配置出各种参数,可以验证应用程序的“适配”能力。
Linux和MacOS:
1. 下载源代码,和其他下载代码一样。
2. 选择SDK对应的产品。
$ lunch sdk-eng
注意:如果通过lunch没有找到SDK相关的产品,直接就输入上述命令。
3. 运行命令编译SDK。
$ make sdk
Windos:
- 运行于Windows环境下的SDK编译需要基于前面Linux的编译结果(只能在Linux环境下生成的结果,而不支持MacOS)。
- 执行Linux下编译生成SDK的所有步骤,生成Linux版本的SDK。 $ sudo apt-get install mingw32 tofrodos
- 再次执行编译命令。
$ source ./build/envsetup.sh
$ lunch sdk-eng
$ make win_sdk
可以利用多核心CPU加速编译,如:
$ make -j4 sdk
面向Host 和Target编译结果都在源码目录的out下。
- target: 通过make命令生成的都在这个目录下。
- host: SDK生成的文件存放在这里。
编译单独模块
模块编译会清除SDK生成的目录,所以可以先编译模块,然后再编译SDK。
执行如下命令,
$ . build/envsetup.sh
会多出一些其他命令,可以打开build/envsetup.sh看到这些命令的函数实现。
Invoke “. build/envsetup.sh” from your shell to add the following functions to your environment:
- lunch: lunch -
- tapas: tapas [ …] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
- croot: Changes directory to the top of the tree.
- m: Makes from the top of the tree.
- mm: Builds all of the modules in the current directory, but not their dependencies.
- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
To limit the modules being built use the syntax: mmm dir/:target1,target2.
- mma: Builds all of the modules in the current directory, and their dependencies.
- mmma: Builds all of the modules in the supplied directories, and their dependencies.
- cgrep: Greps on all local C/C++ files.
- ggrep: Greps on all local Gradle files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- sgrep: Greps on all local source files.
- godir: Go to the directory containing a file.
- make: 不带任何参数,应用于编译整个系统,时间较长,除非必要编译整个系统。
- make MediaProvider: 这种对应于单个模块编译,会把该模块依赖的其他模块也一起编译。例如make libmedia,就会把libmedia依赖的哭全部编译好。这样速度也会慢,它需要搜索整个源码来定位MediaProvider模块所用的Android.mk文件,还要判断该模块所依赖的模块是否有修改。
如果只知道目标模块的名称,应用make 模块名称方式来编译模块。
例如,make libmedia。
如果是初次编译也可以采取这种方法。 - mmm packages/providers/MediaProvider:该命令编译指定目录下的目标模块,而不编译它所依赖的模块。所以,如果初次编译,采用这种方式编译一个模块会出错,因为这个模块依赖的模块还没有被编译。
- mm: 这种命令方式先用cd进入目录,如cd到packages/providers/MediaProvider目录,然后执行mm命令。该命令编译当前目录下的模块。和mmm一样,只编译目标模块,所以这两个命令很快。
- 参数-B: 一般的编译方式都使用增量编译,即只编译发生变化的目标文件。如果需要重新编译所有目标文件,可以使用-B参数。例如,make -B 模块名称,mmm -B, mm -B。
======================================
下载Android Linux Kernel
根据image确定内核的版本
$ dd if=kernel bs=1 skip=$(LC_ALL=C grep -a -b -o $'\x1f\x8b\x08\x00\x00\x00\x00\x00' kernel | cut -d ':' -f 1) | zgrep -a 'Linux version'
例如,对于nexus5的系统来说:
$ dd if=zImage-dtb bs=1 skip=$(LC_ALL=C od -Ad -x -w2 zImage-dtb | grep 8b1f | cut -d ' ' -f1 | head -1) | zgrep -a 'Linux version'
下载Kernel源代码
mkdir ~/kernel
cd kernel
针对不同的处理器有不同的内核版本;
$ git clone https://android.googlesource.com/kernel/common.git
$ git clone https://android.googlesource.com/kernel/x86_64.git
$ git clone https://android.googlesource.com/kernel/exynos.git
$ git clone https://android.googlesource.com/kernel/goldfish.git
$ git clone https://android.googlesource.com/kernel/msm.git
$ git clone https://android.googlesource.com/kernel/omap.git
$ git clone https://android.googlesource.com/kernel/samsung.git
$ git clone https://android.googlesource.com/kernel/tegra.git
例如下载通用版本:
$ git clone https://android.googlesource.com/kernel/common.git
下载选择分支的代码:
cd common // 进入common版本内核
git branch -a // 查看有哪些分支
git checkout remote/origin/Android-3.0 // 选择keernel3.0分支
// 或者是可以根据上面的依据image得到的版本
git checkout <commit_from_first_step>