Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 使用Android Studio導入安卓源碼

使用Android Studio導入安卓源碼

編輯:關於Android編程

1 背景

這篇文章主要說明的是用它來看源碼的相關配置(該實例依據官方源碼進行配置說明)。在android源碼中發現有一個development/tools/idegen目錄,查了一下發現是生成IDE的project文件(主要是intellij的project文件,其實也就是Android Studio的),所以說福利工具就這麼來了,欲知詳情,請看下文。

2 配置使用過程

2-1 基礎准備工作

首先你得安裝配置了Android Studio,接著你得下載好了源碼Code,至於如何下載這裡不再說明,比較簡單,上官網查看就行了。

其次你需要保證源碼已經被編譯生成了out相關目錄文件。

好了,如上就是一些基本准備工作,針對定制源碼或者原生源碼來說,這些准備步驟都是必須的,沒必要再強調了。

2-2 配置導入及使用

2-2-1 編譯源碼idegen模塊及生成AS配置文件(*.ipr)

在開始編譯idegen模塊前,你一定知道需要先全局編譯出out目錄及相關文件吧,這個不再過多說了,我們通過如下命令編譯idegen模塊:

mmm development/tools/idegen/

這行命令的意思是編譯idegen這個模塊項目,然後生成idegen.jar文件(不信你自己去查看這個模塊的Android.mk的目標吧,不多解釋)。運行完以後如果看到如下信息則說明編譯OK:

......
#### make completed successfully (7 seconds) ####

接著執行如下腳本:

development/tools/idegen/idegen.sh

這行命令的意思是在根目錄生成對應的android.ipr、android.iml IEDA工程配置文件。等待片刻得到類似如下信息說明OK:

Read excludes: 21ms
Traversed tree: 194799ms

通過如上操作我們就完成了基本的源碼配置工作。

2-2-2 導入Android Studio前的一些客戶化操作

大家都知道使用Eclipse倒入源碼很慢,Android Studio導入源碼時也慢,所以建議修改android.iml文件(將自己不用的代碼去掉),然後再導入Studio。

就像下面摘取的android.iml文件2237行開始的這些一樣:





      
      
      
      
      
      
      
      
      
      
      
      
      
      
      

我們可以仿照上面這段代碼的寫法一樣過濾掉不需要的內容,這樣在導入時就會快很多。

也可以通過Android Studio的Project Stucture 打開左側Modules,然後將右側Sources中一些目錄Excluded掉。

2-2-3 使用Android Studio導入源碼工程

有了如上的這些操作以後,我們打開Android Studio,然後選擇打開一個現有的Android Studio項目,然後選擇打開源碼 (File --> Open...) 根目錄下的android.ipr文件,然後出現如圖一樣的索引即可等待導入就行:

\

到目前為止我們就可以方便的使用Android Studio查看源碼啦(Ctrl+Shift+N 或者 Navigate --> File)。如下圖所示:

\

牛逼吧!!!

2-3 使用技巧

上圖我們看見了,可以通過Android Studio搜索整套源碼的代碼了。但是這時候如果你打開一個Launcher.java類,然後把鼠標放在其中任意方法的String參數上按住CTRL+鼠標左鍵跳轉到String類你會發現跳轉過去的是一個String.class文件,為啥呢?因為他跳轉的是你的默認SDK中的jar內部的class文件。既然要修改查看整套源碼,這麼跳轉得多蛋疼啊,所以我們需要配置讓其能跳轉到String.java文件,具體做法如下:

首先刪掉依賴中的所有依賴,只保留下圖中沒被選中的那兩個(當然你可以選擇保留一些你用到的其他jar),

接著點擊加號的JARs or directories... 將你源碼的frameworks及external和你用到的其他跳轉目錄添加到依賴中,然後apply即可,如下:

(這裡需要特別注意:要將Android API 23這一項移到最後,不然跳轉的時候仍然會優先跳轉到SDK中的String.class)

\

這時候我們在像上面一樣打開Launcher.java跳轉String,你會發現像下圖一樣直接跳轉到你源碼路徑下的String.java文件了,如下:

\

在android根目錄上按快捷鍵F4(如果已經設為Eclipse快捷鍵方式則為F12)或者File --> Project Structure打開Module Setting,

\

選中不需要掃描的目錄後點右上角的Excluded(其中.repo, out和prebuilts目錄較大,建議選擇),完成以後效果如左側,文件夾顏色區分,紅色的即為排除在外的文件夾,AS將不掃描其中內容。 重新啟動AS,會發現掃描源碼目錄的時間大大縮減了。

\

Tips: 因為Android Studio 的配置和緩存文件存在home/.AndroidStudio (Windows系統在C:\Users\Administrator\.AndroidStudio2.1 )文件夾中,時間長了可能會導致系統磁盤吃緊,若要修改默認存儲位置(比如改到其他掛在盤),需修改android-studio/bin/idea.properties文件中相關的配置信息,修改內容參考:

\

四、配置AS的JDK、SDK

到此對於平時只是查看源碼的人來說已經夠用了。

3 總結

俗話說工欲善其事必先利其器,你會發現使用Android Studio比Eclipse和Source Insight都好很多(當然了,SubText還是很強大的),無論是代碼提示還是跳轉、還是Python Shell等插件支持等都很強大,慢慢體驗吧。

順帶說一句,Google的野心真的很大,看來Android Studio即將要被他們打造為全能IDE了。

4 附加README

附上原版的英文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/").
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved