天天看點

Android Studio導入Android 4.4.4r1的源碼

一、環境配置

1.ubuntu 14.04.5 x64bit

2.jdk-6u45-linux-x64.bin

3.Android Studio 2.3

4.Android 4.4.4r1

二、JDK的配置

根據google官方文檔 ​​https://source.android.com/source/requirements#jdk​​​ 的提示了解到,Android4.4.4r1源碼的編譯需要JDK1.6版本,這裡以 jdk-6u45-linux-x64 為例,進行Android源碼的編譯。Oracle

JDK所有版本的官方下載下傳位址為:​​​http://www.oracle.com/technetwork/java/javase/archive-139210.html​​。

Android Studio導入Android 4.4.4r1的源碼

對下載下傳的 jdk-6u45-linux-x64.bin 進行解壓:

$ chmod a+x jdk-6u45-linux-x64.bin
$ ./jdk-6u45-linux-x64.bin      

配置jdk的環境變量:

# 打開環境變量配置檔案
$ sudo gedit /etc/profile

# 配置jdk環境變量
export JAVA_HOME=/opt/jdk1.6.0_45 
export JRE_HOME=${JAVA_HOME}/jre  
export PATH=$PATH:${JAVA_HOME}/bin  
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib  

# 儲存、關閉/etc/profile檔案,更新使環境變量生效
$ source /etc/profile

# 檢視jdk的版本資訊
$ java -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)      

三、安裝依賴庫檔案

根據google官方文檔 ​​https://source.android.com/source/initializing#for-ubuntu-14-04​​ 知道:在ubuntu 14.04版本的系統上編譯Android源碼需要安裝下面的依賴庫檔案。

Android Studio導入Android 4.4.4r1的源碼

在ubuntu 14.04系統上編譯Android系統源碼需要安裝的依賴庫檔案:

$ sudo apt-get install git-core gnupg flex bison gperf build-essential \
  zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 \
  lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache \
  libgl1-mesa-dev libxml2-utils xsltproc unzip      

四、Android源碼的編譯

# Clean up
$ make clobber

# Set up environment
$ . build/envsetup.sh  
# or
$ source build/envsetup.sh  

# Choose a target
$ lunch

You're building on Linux

Lunch menu... pick a combo:
     1. aosp_arm-eng
     2. aosp_x86-eng
     3. aosp_mips-eng
     4. vbox_x86-eng
     5. aosp_grouper-userdebug
     6. aosp_flo-userdebug
     7. aosp_deb-userdebug
     8. aosp_tilapia-userdebug
     9. aosp_mako-userdebug
     10. aosp_hammerhead-userdebug
     11. aosp_manta-userdebug
     12. mini_armv7a_neon-userdebug
     13. mini_x86-userdebug
     14. mini_mips-userdebug

Which would you like? [aosp_arm-eng] 1

# Build the code
$ make -j16      

Android 4.4.4r1的源碼編譯成功的結果截圖:

Android Studio導入Android 4.4.4r1的源碼

五、Linux下Android Studio的下載下傳和安裝

根據google官方的文檔了解到,各版本的Android Studio的下載下傳位址為:​​https://developer.android.com/studio/index.html#tos-header​​。根據自己的實際需要選擇目标版本的Android

Studio進行下載下傳,這裡下載下傳Linux 版本的Android Studio,并且Android Studio的下載下傳具體位址為:​​https://dl.google.com/dl/android/studio/ide-zips/2.3.1.0/android-studio-ide-162.3871768-linux.zip​​。google官方還提供了各版本的Android

Studio安裝教程的說明,詳細的說明了各個版本的Android Studio安裝需要注意的細節,參考位址為:​​https://developer.android.com/studio/install.html​​。

Android Studio導入Android 4.4.4r1的源碼

Android Studio安裝指令示例如下:

# ubuntu 64bit需要安裝32bit的依賴庫檔案 
$ sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0 lib32stdc++6  

# 解壓下載下傳的android-studio檔案包
$ unzip android-studio-ide-162.3871768-linux.zip 

$ ls -l
# 拷貝android-studio到/opt目錄下
$ sudo mv android-studio /opt/

$ cd /opt/android-studio/bin/
$ ls -l
# 賦予可執行權限(可選)
$ sudo chmod +x ./studio.sh

# 啟動Android Studio
$ ./studio.sh      

Android Studio安裝和啟動成功以後,按照Android Studio的提示進行相關的配置即可。在配置的過程中Android Studio會提示下載下傳需要的sdk、emulator等檔案,保持網絡的暢通,等待Android Studio下載下傳完成就好。為了啟動 Android Studio 友善,需要将 android-studio/bin/ 添加至 PATH 環境變量中,這樣我們就可以從任何目錄啟動

Android Studio 程式。

# 打開環境變量配置檔案
$ sudo gedit /etc/profile

# 添加Android Studio至環境變量
export ANDROID_STUDIO_HOME=/opt/android-studio
export PATH=$PATH:${ANDROID_STUDIO_HOME}/bin  

# 儲存、關閉/etc/profile檔案,更新使環境變量生效
$ source /etc/profile

# 任意目錄啟動Android Studio測試
$ studio.sh      

六、Android Studio導入Android源碼

在Android源碼的檔案路徑 /development/tools/idegen下,有關于怎麼導入Andriod源碼到Android Studio 或者Eclipse中的說明,具體的就是 /development/tools/idegen/README 檔案。導入Android源碼到Android

Studio中的說明如下:

Android Studio導入Android 4.4.4r1的源碼

完整的 /development/tools/idegen/README 檔案的說明,如下:

IDEGen automatically generates Android IDE configurations for IntelliJ IDEA
and Eclipse. Your IDE should be able to compile everything in a reasonable
amount of time with no errors.

If you're using IntelliJ...

    If this is your first time using IDEGen...

        IDEA needs a lot of memory. Add "-Xms748m -Xmx748m" to your VM options
        in "IDEA_HOME/bin/idea.vmoptions" on Linux or
        "IntelliJ IDEA.app/Contents/Info.plist" on OS X.

        Create a JDK configuration named "1.6 (No Libraries)" by adding a new
        JDK like you normally would and then removing all of the jar entries
        under the "Classpath" tab. This will ensure that you only get access to
        Android's core libraries and not those from your desktop VM.

    From the project's root directory...

        Repeat these steps after each sync...

        1) make (to produce generated .java source)
        2) development/tools/idegen/idegen.sh
        3) Open android.ipr in IntelliJ. If you already have the project open,
           hit the sync button in IntelliJ, and it will automatically detect the
           updated configuration.

        If you get unexpected compilation errors from IntelliJ, try running
        "Build -> Rebuild Project". Sometimes IntelliJ gets confused after the
        project changes significantly.

If you're using Eclipse...

    If this is your first time using IDEGen...

        Edit eclipse.ini ("Eclipse.app/Contents/MacOS/eclipse.ini" on OS X) and
        add "-Xms748m -Xmx748m" to your VM options.

        Configure a JRE named "1.5 (No Libraries)" under "Preferences -> Java ->
        Installed JREs". Remove all of the jar entries underneath "JRE system
        libraries". Eclipse will not let you save your configuration unless at
        least one jar is present, so include a random jar that won't get in the
        way.

    From the project's root directory...

        Repeat these steps after each sync...

        1) make (to produce generated .java source)
        2) development/tools/idegen/idegen.sh
        3) Import the project root directory into your Eclipse workspace. If you
           already have the project open, simply refresh it (F5).

Excluding source roots and jars

    IDEGen keeps an exclusion list in the "excluded-paths" file. This file
    has one regular expression per line that matches paths (relative to the
    project root) that should be excluded from the IDE configuration. We
    use Java's regular expression parser (see java.util.regex.Parser).

    You can create your own additional exclusion list by creating an
    "excluded-paths" file in the project's root directory. For example, you
    might exclude all apps except the Browser in your IDE configuration with
    this regular expression: "^packages/apps/(?!Browser)".

Controlling source root ordering (Eclipse)

    You may want some source roots to come before others in Eclipse. Simply
    create a file named "path-precedence" in your project's root directory.
    Each line in the file is a regular expression that matches a source root
    path (relative to the project's root directory). If a given source root's
    path matches a regular expression that comes earlier in the file, that
    source root will come earlier in the generated configuration. If a source
    root doesn't match any of the expressions in the file, it will come last,
    so you effectively have an implicit ".*" rule at the end of the file.

    For example, if you want your applications's source root to come first,
    you might add an expression like "^packages/apps/MyApp/src$" to the top
    of the "path-precedence" file.  To make source roots under ./out come last,
    add "^(?!out/)" (which matches all paths that don't start with "out/").      

按照Android源碼提供的幫助說明導入Android源碼到Android Studio中的步驟:

1.在導入Android源碼到Android Studio中之前一定要保證已經成功編譯過Android源碼。由于在執行 /development/tools/idegen/idegen.sh 腳本時會使用到 /out/host/linux-x86/framework/idegen.jar工具(如下圖所示),是以在執行 /development/tools/idegen/idegen.sh 腳本之前需要確定 idegen.jar

工具已經存在。

Android Studio導入Android 4.4.4r1的源碼

2.如果在 /out/host/linux-x86/framework/ 目錄下,不存在 idegen.jar ,則可以根據 /development/tools/idegen/Android.mk 進行編譯生成。

Android Studio導入Android 4.4.4r1的源碼

3.在Android源碼的根目錄下,執行下面的指令先生成 idegen.jar 檔案,然後生成導入Android源碼到Android Studio中的配置檔案。

# 導入預設腳本
$ source build/envsetup.sh

# 編譯生成idegen.jar工具
$ mmm ./development/tools/idegen/

# 生成導入Android源碼到Android Studio中的配置檔案
$ ./development/tools/idegen/idegen.sh      

執行上面的指令成功以後會在Android源碼的根目錄下生成 android.iml、android.ipr

和 android.iws 檔案,結果截圖如下:

Android Studio導入Android 4.4.4r1的源碼

其實,在Android源碼的根目錄下也生成了Eclipse導入Android源碼的配置檔案.classpath ,結果如下圖所示:

Android Studio導入Android 4.4.4r1的源碼

4.在将Android源碼導入到Android Studio中需要消耗很多的記憶體,是以需要修改Android Studio的配置檔案,将配置設定的記憶體空間改大一點。Linux 32位系統下修改android-studio/bin/studio.vmoptions ,Linux 64位系統下修改 android-studio/bin/studio64.vmoptions 調整其中-Xms和-Xmx的參數值,推薦至少在748m以上。這裡是

ubuntu 14.04 64bit 的系統,是以修改後的 android-studio/bin/studio64.vmoptions 檔案如下:

Android Studio導入Android 4.4.4r1的源碼

5.執行 studio.sh 啟動Android Studio,點選 File->Open,選擇Android源碼根目錄下剛才生成的android.ipr檔案 即可,然後等待Android Studio導入Android源碼。下面是Android Studio導入Android源碼後的結果截圖:

Android Studio導入Android 4.4.4r1的源碼

6.在Android Studio中根據目前編譯的Android源碼的版本即 Android 4.4.4r1 ,保持網絡暢通下載下傳Android 4.4.4r1對應的Android SDK的版本Android API 19,如下圖所示:

Android Studio導入Android 4.4.4r1的源碼

點選 File->Project Structure->SDKs-> "+" 建立一個新的JDK命名為jdk1.6 (No Libraries),然後删除掉classpath标簽頁下面所有的jar檔案(也就是需要删掉Classpath标簽頁下的所有jar包)。這樣可以確定使用Android源碼裡的庫檔案即使Android源碼不使用本地安裝的jdk的jar包而是使用Android源碼的核心jar庫檔案。

Android Studio導入Android 4.4.4r1的源碼

并将 jdk1.6 (No Libraries) 作為Android SDK(Android API 19)版本要使用的jar包,設定如下圖所示:

Android Studio導入Android 4.4.4r1的源碼

然後在 Project标簽 中的 Project SDK 中選擇對應的Android SDK(Android API 19)版本,如下圖所示:

Android Studio導入Android 4.4.4r1的源碼

7.確定在上面的操作步驟中已為目前Android源碼工程正确的設定了Android SDK和JDK,然後打開 Projects Stucture, 選擇Modules将所有的預設的依賴jar庫檔案全部删除(為了友善起見,後邊如果缺少什麼jar庫檔案,可以自行添加進來)。

Android Studio導入Android 4.4.4r1的源碼

預設依賴jar檔案全部删除完成之後,保留如下圖所示的兩項,然後點選右邊的 "+" 添加Android源碼的 frameworks 和external 目錄中的jar封包件,如下圖所示:

Android Studio導入Android 4.4.4r1的源碼

将Android源碼的 frameworks 和 external 目錄中的jar封包件添加成功以後的截圖,如下:

Android Studio導入Android 4.4.4r1的源碼

在以後閱讀Android源碼的過程中,如果出現代碼跳轉錯誤,請仿照上面的步驟将相應代碼的路徑或jar檔案添加到Moduled的Dependencies标簽頁下即可。

七.總結

關于向Android Studio中導入Android源碼,參考了一些部落格但是很多都說的不是很清楚,這裡也是稀裡糊塗的實踐了一遍,可能是由于我選的Android源碼版本比較低而且Android Studio的版本比較高,最後導緻實踐出來的結果和其他部落客操作的結果有所不同。《​​Android

Studio導入Android源碼​​》這篇博文寫的還不錯,其中有些細節的地方是參考的這篇博文。

參考文章連結:

《​​Android Studio導入Android源碼​​》

《​​自己動手調試Android源碼(超簡單)​​》

《​​idegen:一個角落裡的法器​​》

《​​Macbook Pro下載下傳并編譯Android源碼并導入Android Studio​​》

八.其他的參考資料

《​​Introduction to AOSP​​》這篇文章也有一些介紹,不過實踐了一下,有些步驟也過時了。具體的步驟詳情如下,僅供參考:

Android Studio導入Android 4.4.4r1的源碼
Android Studio導入Android 4.4.4r1的源碼