Android學習系列(38)--Android原始碼下載和編譯

謙虛的天下發表於2014-04-22

前面多篇文章介紹到如何下載和編譯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

也許是我命好,FQ之後,下載一切順利,沒有以前碰到的和網上說的的一大堆問題,唯一的一個尷尬的問題,下載的過程中經常斷。
早上醒來的時候斷了,繼續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、小結
有了原始碼,可以學習優秀的程式碼,可以從歷史提交看看它們是如何改進的,可以定製自己的系統樣式,可以做的事情很多...

相關文章