Android實戰技巧之四十:Android5.1.1原始碼編譯與燒寫

poclist發表於2016-09-17

購買Nexus手機的朋友大多是為了自己修改系統玩,再加上其較高的價效比,在開發者中還是廣受歡迎的。我的5太子被我升級到了6.0預覽版,玩的正嗨,捨不得換回到5.1時代了。不過鑑於距6.0原始碼釋出還有段日子,5.1的原始碼編譯與燒寫仍是主流,下面就記錄了整個過程(持續了很長時間,我們要有耐心)。

搭建開發環境

系統推薦Ubuntu 14.04
1.openjdk is needed

<code class="hljs lasso has-numbering">$ sudo apt<span class="hljs-attribute">-get</span> update
$ sudo apt<span class="hljs-attribute">-get</span> install openjdk<span class="hljs-subst">-</span><span class="hljs-number">7</span><span class="hljs-attribute">-jdk</span></code>

set it the default java version

<code class="hljs bash has-numbering">$ <span class="hljs-built_in">sudo</span> update-alternatives --config java
$ <span class="hljs-built_in">sudo</span> update-alternatives --config javac</code>

2.required package

<code class="hljs lasso has-numbering">sudo apt<span class="hljs-attribute">-get</span> install bison g<span class="hljs-subst">++-</span>multilib git gperf libxml2<span class="hljs-attribute">-utils</span> make python<span class="hljs-attribute">-networkx</span> zlib1g<span class="hljs-attribute">-dev</span>:i386 zip</code>

遇到問題:

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

The following packages have unmet dependencies:
 g++-multilib : Depends: gcc-multilib (>= <span class="hljs-number">4</span>:<span class="hljs-number">4.8</span><span class="hljs-number">.2</span>-<span class="hljs-number">1</span>ubuntu6) <span class="hljs-keyword">but</span> <span class="hljs-keyword">it</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> going <span class="hljs-keyword">to</span> be installed
E: Unable <span class="hljs-keyword">to</span> correct problems, you have held broken packages</code>

按照如下步驟即可:

<code class="hljs lasso has-numbering">$ sudo apt<span class="hljs-attribute">-get</span> install g<span class="hljs-subst">++-</span>multilib
$ sudo apt<span class="hljs-attribute">-get</span> install  bison git gperf libxml2<span class="hljs-attribute">-utils</span> make python<span class="hljs-attribute">-networkx</span> zlib1g<span class="hljs-attribute">-dev</span>:i386 zip</code>

gcc為必須

<code class="hljs lasso has-numbering">$ sudo apt<span class="hljs-attribute">-get</span> install gcc

$ gcc <span class="hljs-attribute">-v</span>
<span class="hljs-keyword">Thread</span> model: posix
gcc version <span class="hljs-number">4.8</span><span class="hljs-number">.4</span> (Ubuntu <span class="hljs-number">4.8</span><span class="hljs-number">.4</span><span class="hljs-subst">-</span><span class="hljs-number">2</span>ubuntu1~<span class="hljs-number">14.04</span>) 
</code>

repo

<code class="hljs smalltalk has-numbering"><span class="hljs-char">$ </span>mkdir ~/bin
<span class="hljs-char">$ </span><span class="hljs-class">PATH</span>=~/<span class="hljs-method">bin:</span><span class="hljs-char">$P</span>ATH

<span class="hljs-char">$ </span>curl <span class="hljs-method">https:</span>//storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
<span class="hljs-char">$ </span>chmod a+x ~/bin/repo</code>

check sh1 sum:

<code class="hljs ruby has-numbering"><span class="hljs-variable">$ </span>sha1sum repo 
b8bd1804f432ecf1bab730949c82b93b0fc5fede  repo
</code>

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

repo init

針對Nexus5的5.1.1的初始化如下:

<code class="hljs  has-numbering">LMY48I  android-5.1.1_r9    Lollipop    Nexus 4, Nexus 5, Nexus 6, Nexus 7 (flo), Nexus 9 (volantis/volantisg), Nexus 10</code>1
<code class="hljs lasso has-numbering">$ repo init <span class="hljs-attribute">-u</span> https:<span class="hljs-comment">//android.googlesource.com/platform/manifest -b android-5.1.1_r9</span>

Your identity is: linc <span class="hljs-subst"><</span>xxx@xxx<span class="hljs-built_in">.</span>com<span class="hljs-subst">></span>
<span class="hljs-keyword">If</span> you want <span class="hljs-keyword">to</span> change this, please re<span class="hljs-attribute">-run</span> <span class="hljs-string">'repo init'</span> <span class="hljs-keyword">with</span> <span class="hljs-subst">--</span>config<span class="hljs-attribute">-name</span>

Testing colorized output (for <span class="hljs-string">'repo diff'</span>, <span class="hljs-string">'repo status'</span>):
  black    red      green    yellow   blue     magenta   cyan     white 
  bold     dim      ul       reverse 

repo has been initialized <span class="hljs-keyword">in</span> /home/linc/source<span class="hljs-attribute">-android</span>/android<span class="hljs-attribute">-source</span></code>

repo sync

<code class="hljs ruby has-numbering"><span class="hljs-variable">$ </span>repo sync</code>

一個晚上兩個白天,終於下載完成,程式碼達34G.

Setting up ccache

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

<code class="hljs bash has-numbering"><span class="hljs-keyword">export</span> USE_CCACHE=<span class="hljs-number">1</span></code>

run the command:

<code class="hljs lasso has-numbering">prebuilts/misc/linux<span class="hljs-attribute">-x86</span>/ccache/ccache <span class="hljs-attribute">-M</span> <span class="hljs-number">50</span>G</code>

Building the System

1.Set up environment

<code class="hljs bash has-numbering">$ <span class="hljs-built_in">source</span> build/envsetup.sh</code>

or

<code class="hljs ruby has-numbering"><span class="hljs-variable">$ </span>. build/envsetup.sh</code>

2.Choose a Target

<code class="hljs vbnet has-numbering">$ lunch 

You<span class="hljs-comment">'re building on Linux</span>

Lunch menu... pick a combo:
     <span class="hljs-number">1.</span> aosp_arm-eng
     <span class="hljs-number">2.</span> aosp_arm64-eng
     <span class="hljs-number">3.</span> aosp_mips-eng
     <span class="hljs-number">4.</span> aosp_mips64-eng
     <span class="hljs-number">5.</span> aosp_x86-eng
     <span class="hljs-number">6.</span> aosp_x86_64-eng
     <span class="hljs-number">7.</span> aosp_manta-userdebug
     <span class="hljs-number">8.</span> aosp_flo-userdebug
     <span class="hljs-number">9.</span> aosp_deb-userdebug
     <span class="hljs-number">10.</span> full_fugu-userdebug
     <span class="hljs-number">11.</span> aosp_fugu-userdebug
     <span class="hljs-number">12.</span> aosp_tilapia-userdebug
     <span class="hljs-number">13.</span> aosp_grouper-userdebug
     <span class="hljs-number">14.</span> aosp_mako-userdebug
     <span class="hljs-number">15.</span> aosp_hammerhead-userdebug
     <span class="hljs-number">16.</span> aosp_flounder-userdebug
     <span class="hljs-number">17.</span> aosp_shamu-userdebug
     <span class="hljs-number">18.</span> mini_emulator_x86-userdebug
     <span class="hljs-number">19.</span> mini_emulator_arm64-userdebug
     <span class="hljs-number">20.</span> mini_emulator_x86_64-userdebug
     <span class="hljs-number">21.</span> mini_emulator_mips-userdebug
     <span class="hljs-number">22.</span> m_e_arm-userdebug

Which would you <span class="hljs-keyword">like</span>? [aosp_arm-eng] </code>

Nexus5就選擇了15,如果只是用於模擬器就選擇1好了。
3.build

<code class="hljs lasso has-numbering">make <span class="hljs-attribute">-j4</span></code>

如果遇到等待其他任務完成的錯誤,是多執行緒衝突,就直接make好了。
4.error
1)

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

try make clean.這是沒有安裝gcc的後果。

build successfully

<code class="hljs css has-numbering">#### <span class="hljs-tag">make</span> <span class="hljs-tag">completed</span> <span class="hljs-tag">successfully</span> (05<span class="hljs-pseudo">:53</span><span class="hljs-pseudo">:54</span> (<span class="hljs-tag">hh</span><span class="hljs-pseudo">:mm</span><span class="hljs-pseudo">:ss))</span> ####</code>

目錄結構如下:

<code class="hljs avrasm has-numbering"><span class="hljs-keyword">out</span>/target/product/hammerhead$ du -sh *
<span class="hljs-number">4.0</span>K    android-info<span class="hljs-preprocessor">.txt</span>
<span class="hljs-number">8.8</span>M    boot<span class="hljs-preprocessor">.img</span>
<span class="hljs-number">4.0</span>K    cache
<span class="hljs-number">14</span>M cache<span class="hljs-preprocessor">.img</span>
<span class="hljs-number">64</span>K clean_steps<span class="hljs-preprocessor">.mk</span>
<span class="hljs-number">172</span>K    data
<span class="hljs-number">4.0</span>K    fake_packages
<span class="hljs-number">81</span>M gen
<span class="hljs-number">64</span>K installed-files<span class="hljs-preprocessor">.txt</span>
<span class="hljs-number">8.1</span>M    kernel
<span class="hljs-number">16</span>G obj
<span class="hljs-number">4.0</span>K    previous_build_config<span class="hljs-preprocessor">.mk</span>
<span class="hljs-number">700</span>K    ramdisk<span class="hljs-preprocessor">.img</span>
<span class="hljs-number">1.4</span>M    ramdisk-recovery<span class="hljs-preprocessor">.img</span>
<span class="hljs-number">2.2</span>M    recovery
<span class="hljs-number">9.5</span>M    recovery<span class="hljs-preprocessor">.img</span>
<span class="hljs-number">1.3</span>M    root
<span class="hljs-number">2.9</span>G    symbols
<span class="hljs-number">288</span>M    system
<span class="hljs-number">307</span>M    system<span class="hljs-preprocessor">.img</span>
<span class="hljs-number">135</span>M    userdata<span class="hljs-preprocessor">.img</span>
</code>

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

<code class="hljs ruby has-numbering"><span class="hljs-variable">$ </span>adb reboot bootloader</code>

Once the device is in fastboot mode, run

<code class="hljs ruby has-numbering"><span class="hljs-variable">$ </span>fastboot flashall -w</code>

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.啟動模擬器

<code class="hljs mel has-numbering">~/<span class="hljs-keyword">source</span>-android/android-<span class="hljs-keyword">source</span>/out/target/product/generic$ emulator -sysdir ~/<span class="hljs-keyword">source</span>-android/android-<span class="hljs-keyword">source</span>/out/target/product/generic -<span class="hljs-keyword">system</span> <span class="hljs-keyword">system</span>.img 
emulator: WARNING: <span class="hljs-keyword">system</span> <span class="hljs-keyword">partition</span> <span class="hljs-keyword">size</span> adjusted to <span class="hljs-keyword">match</span> <span class="hljs-keyword">image</span> <span class="hljs-keyword">file</span> (<span class="hljs-number">550</span> MB > <span class="hljs-number">200</span> MB)

emulator: WARNING: data <span class="hljs-keyword">partition</span> <span class="hljs-keyword">size</span> adjusted to <span class="hljs-keyword">match</span> <span class="hljs-keyword">image</span> <span class="hljs-keyword">file</span> (<span class="hljs-number">550</span> MB > <span class="hljs-number">200</span> MB)

Creating filesystem with parameters:
    Size: <span class="hljs-number">69206016</span>
    Block <span class="hljs-keyword">size</span>: <span class="hljs-number">4096</span>
    Blocks per <span class="hljs-keyword">group</span>: <span class="hljs-number">32768</span>
    Inodes per <span class="hljs-keyword">group</span>: <span class="hljs-number">4224</span>
    Inode <span class="hljs-keyword">size</span>: <span class="hljs-number">256</span>
    Journal blocks: <span class="hljs-number">1024</span>
    Label: 
    Blocks: <span class="hljs-number">16896</span>
    Block groups: <span class="hljs-number">1</span>
    Reserved block <span class="hljs-keyword">group</span> <span class="hljs-keyword">size</span>: <span class="hljs-number">7</span>
Created filesystem with <span class="hljs-number">11</span>/<span class="hljs-number">4224</span> inodes and <span class="hljs-number">1302</span>/<span class="hljs-number">16896</span> blocks</code>

模擬器順利啟動,與真機的區別有很多,比如開機畫面不同,Home的UI和操控都有所不同。具體原因敬請期待。

相關文章