前面多篇文章介绍到如何下载和编译Android或者CM源码,不过一直都是放在《拓展系列》里。
随着学习的深入,android源码是非常有参考和学习价值,强烈推荐大家都去下载,编译,学习,所以把它提到《学习系列》专讲一章基础篇。
刚装了UBUNTU14.04,所以尝试在新环境下编译一下android源码。
因为和谐原因,我临时买了个VPN,推荐一下(还算稳定):http://a.wy002.com/34750 或者云梯
1、参考链接(官方):
环境:http://source.android.com/source/initializing.html
下载:http://source.android.com/source/downloading.html
编译:http://source.android.com/source/building-running.html
2、Ubuntu14.04(x64)+ OpenJDK7
最新的代码用默认的openjdk7就可以编译通过,反而用jdk6无法编译。
安装openjdk7:
$ sudo apt-get update
$ sudo apt-get install openjdk-7-jdk
$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac
安装其他组件:
$ sudo apt-get install git gnupg flex bison gperf build-essential \
zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev \
libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 \
libgl1-mesa-dev g++-multilib mingw32 tofrodos \
python-markdown libxml2-utils xsltproc zlib1g-dev:i386
$ sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so
我这里安装很快(我配置了163的mirror),一切顺利。
3、连上VPN
直接参考这个VPN官方说明:
https://www.51jsq.org/setting/ubuntu.shtml
4、安装repo
$ mkdir ~/bin
$ PATH=~/bin:$PATH
$ curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo
为了方便,你可以继续配置一下repo:
在.bashrc中添加:
alias repo='~/bin/repo'
5、开始下载
$ mkdir android
$ cd android
$ repo init -u https://android.googlesource.com/platform/manifest //下载最新
上面是初始化(init)repo版本库,真正的下载:
$ repo sync
也许是我命好,*之后,下载一切顺利,没有以前碰到的和网上说的的一大堆问题,唯一的一个尴尬的问题,下载的过程中经常断。
早上醒来的时候断了,继续repo sync,下班回来一看,断了,repo sync ...
大概断断续续的花了2天2夜在今天晚上加班回来下载好了。
提醒一下,如果你下载出错了,请务必仔细看看是什么错误,不是一味的repo sync,我这次这么顺利简直“不科学”~
下载好了!!!!
6、开始编译
初始化一些参数和变量:
$ source build/envsetup.sh
定义设备类型,我们使用模拟器(aosp_arm-eng):
/*
xxx@ubuntu:~/Data/android$ 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. vbox_x86-eng
8. aosp_hammerhead-userdebug
9. aosp_mako-userdebug
10. aosp_manta-userdebug
11. aosp_tilapia-userdebug
12. aosp_grouper-userdebug
13. aosp_deb-userdebug10、小结
14. aosp_flo-userdebug
15. mini_armv7a_neon-userdebug
16. mini_mips-userdebug
17. mini_x86-userdebug Which would you like? [aosp_arm-eng] 1 ============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.4.3.2.1.000.000
TARGET_PRODUCT=aosp_arm
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_2ND_ARCH=
TARGET_ARCH_VARIANT=armv7-a
TARGET_CPU_VARIANT=generic
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.13.0-24-generic-x86_64-with-Ubuntu-14.04-trusty
HOST_BUILD_TYPE=release
BUILD_ID=OPENMASTER
OUT_DIR=out
============================================
*/
定义好了后,开始编译(是最新的4.4.3的版本):
/*
xxx@ubuntu:~/Data/android$ make -j8
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.4.3.2.1.000.000
TARGET_PRODUCT=aosp_arm
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_2ND_ARCH=
TARGET_ARCH_VARIANT=armv7-a
TARGET_CPU_VARIANT=generic
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.13.0-24-generic-x86_64-with-Ubuntu-14.04-trusty
HOST_BUILD_TYPE=release
BUILD_ID=OPENMASTER
OUT_DIR=out
============================================
including ./abi/cpp/Android.mk ...
including ./art/Android.mk ...
including ./bionic/Android.mk ...
including ./bootable/diskinstaller/Android.mk ...
including ./bootable/recovery/Android.mk ...
including ./build/libs/host/Android.mk ...
including ./build/target/board/Android.mk ...
including ./build/tools/Android.mk ...
including ./cts/Android.mk ...
// ... ...
Installed file list: out/target/product/generic/installed-files.txt
Target system fs image: out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img
Running: mkuserimg.sh out/target/product/generic/system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img ext4 system 576716800 out/target/product/generic/root/file_contexts
// ... ...
+ ENABLE_SPARSE_IMAGE=
+ '[' out/target/product/generic/system = -s ']'
+ '[' 6 -ne 5 -a 6 -ne 6 ']'
+ SRC_DIR=out/target/product/generic/system
+ '[' '!' -d out/target/product/generic/system ']'
+ OUTPUT_FILE=out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img
+ EXT_VARIANT=ext4
+ MOUNT_POINT=system
+ SIZE=576716800
+ FC=out/target/product/generic/root/file_contexts
+ case $EXT_VARIANT in
+ '[' -z system ']'
+ '[' -z 576716800 ']'
+ '[' -n out/target/product/generic/root/file_contexts ']'
+ FCOPT='-S out/target/product/generic/root/file_contexts'
+ MAKE_EXT4FS_CMD='make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/generic/system'
+ echo make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/generic/system
make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/generic/system
+ make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/generic/system
Creating filesystem with parameters:
Size: 576716800
Block size: 4096
Blocks per group: 32768
Inodes per group: 7040
Inode size: 256
Journal blocks: 2200
Label:
Blocks: 140800
Block groups: 5
Reserved block group size: 39
Created filesystem with 1295/35200 inodes and 100299/140800 blocks
+ '[' 0 -ne 0 ']'
Install system fs image: out/target/product/generic/system.img
out/target/product/generic/system.img+ maxsize=588791808 blocksize=2112 total=576716800 reserve=5947392
*/
编译完成。
7、运行模拟器
在前面 souce build/envsetup.sh中已经添加emulator命令了:
$ emulator
就能跑起一个模拟器了:
8、修改和编译系统应用代码
我们修改一下系统应用Mms的标题:
使用mm命令编译:
/*
xxx@ubuntu:~/Data/android/packages/apps/Mms$ mm
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.4.3.2.1.000.000
TARGET_PRODUCT=aosp_arm
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_2ND_ARCH=
TARGET_ARCH_VARIANT=armv7-a
TARGET_CPU_VARIANT=generic
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.13.0-24-generic-x86_64-with-Ubuntu-14.04-trusty
HOST_BUILD_TYPE=release
BUILD_ID=OPENMASTER
OUT_DIR=out
============================================
make:进入目录'/home/xxx/Data/android'
target R.java/Manifest.java: Mms (out/target/common/obj/APPS/Mms_intermediates/src/R.stamp)
warning: string 'menu_insert_smiley' has no default translation.
target Java: Mms (out/target/common/obj/APPS/Mms_intermediates/classes)
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
Copying: out/target/common/obj/APPS/Mms_intermediates/classes-jarjar.jar
Copying: out/target/common/obj/APPS/Mms_intermediates/emma_out/lib/classes-jarjar.jar
Copying: out/target/common/obj/APPS/Mms_intermediates/classes.jar
Proguard: out/target/common/obj/APPS/Mms_intermediates/proguard.classes.jar
ProGuard, version 4.10
Reading program jar [/home/xxx/Data/android/out/target/common/obj/APPS/Mms_intermediates/classes.jar]
// ... ...
target Dex: Mms
Copying: out/target/common/obj/APPS/Mms_intermediates/classes.dex
target Package: Mms (out/target/product/generic/obj/APPS/Mms_intermediates/package.apk)
nothing matches overlay file ic_contact_picture.png, for flavor ,,,,,,,,,,,,mdpi,,,,,,,
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
warning: string 'menu_insert_smiley' has no default translation.
warning: string 'menu_insert_smiley' is missing 19 required localizations: az_AZ en_AU en_CA en_GB en_IN en_NZ en_SG en_US eo_EU hy_AM ka_GE km_KH lo_LA mn_MN ne_NP si_LK zh_CN zh_HK zh_TW
dex2oatd I 21424 21424 art/dex2oat/dex2oat.cc:1082] dex2oat: out/host/linux-x86/bin/dex2oatd --runtime-arg -Xms64m --runtime-arg -Xmx64m --boot-image=out/target/product/generic/dex_bootjars/system/framework/boot.art --dex-file=out/target/product/generic/obj/APPS/Mms_intermediates/package.apk --dex-location=/system/priv-app/Mms.apk --oat-file=out/target/product/generic/obj/APPS/Mms_intermediates/package.odex --android-root=out/target/product/generic/system --instruction-set=arm --instruction-set-features=default
dex2oatd I 21424 21424 art/dex2oat/dex2oat.cc:252] dex2oat took 1.189425041s (threads: 8)
Notice file: packages/apps/Mms/NOTICE -- out/target/product/generic/obj/NOTICE_FILES/src//system/priv-app/Mms.apk.txt
Install: out/target/product/generic/system/priv-app/Mms.apk
Install: out/target/product/generic/system/priv-app/Mms.odex
// ... ...
Install: out/target/product/generic/data/app/MmsTests.apk
Install: out/target/product/generic/data/app/MmsTests.odex
make:离开目录“/home/xxx/Data/android”
*/
主要是生成了两个文件:
out/target/product/generic/system/priv-app/Mms.apk
out/target/product/generic/system/priv-app/Mms.odex
9、安装到手机
因为是系统应用,为了重启有效,使用push命令把两个文件push到模拟器中对应的位置:
// 注意现在一些核心的应用的位置由以前的system/app调整为了system/priv-app,不要push错了
/*
xxx@ubuntu:~/Data/android$ adb push out/target/product/generic/system/priv-app/Mms.odex system/priv-app
2893 KB/s (2085348 bytes in 0.703s)
xxx@ubuntu:~/Data/android$ adb push out/target/product/generic/system/priv-app/Mms.apk system/priv-app
3315 KB/s (1785258 bytes in 0.525s)
*/
看下模拟器效果:
标题被替换了。
类似了,你可以修改framework,替换内核,不过有些需要重启模拟器,有些需要重新编译相关的所以模块,这里只是简单的介绍,以后有机会再细说。
10、小结
有了源码,可以学习优秀的代码,可以从历史提交看看它们是如何改进的,可以定制自己的系统样式,可以做的事情很多...