使用Android Studio匯入源碼
1 背景
上路了,準備開始重操那兩年的舊業,後悔之前在上家公司做Amlogic和Realtak方案盒子時沒有養成寫部落格的習慣,這算是開篇之作吧,順帶也算是給一個網友的答疑吧。不多說,該篇只是工具講解而已。
關於Android Studio對於應用程式層App開發的技巧和配置前面我寫過文章分析的,這裡不再解釋說明,具體查看我的這一片博文《Android Studio入門到精通 》。這裡我們主要說明的是用它來看源碼的相關配置(該執行個體依據官方源碼android-5.1.1_r14進行配置說明)。
以前在上家公司查看修改源碼時還是使用的Eclipse匯入部分代碼的方式,而現在在android源碼中發現有一個development/tools/idegen目錄,查了一下發現是產生IDE的project檔案(主要是intellij的project檔案,其實也就是Android Studio的),所以說福利工具就這麼來了,欲知詳情,請看下文。
2 配置使用過程
2-1 基礎準備工作
首先你得安裝配置了Android Studio,具體不明白的參考《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: 21msTraversed tree: 194799ms
通過如上操作我們就完成了基本的源碼配置工作。
2-2-2 匯入Android Studio前的一些客戶化操作
大家都知道使用Eclipse倒入源碼很慢,Android Studio匯入源碼時也慢,所以建議修改android.iml檔案(將自己不用的代碼去掉),然後再匯入Studio。
就像下面摘取的android.iml檔案1887行開始的這些一樣:
我們可以仿照上面這段代碼的
寫法一樣過濾掉不需要的內容,這樣在匯入時就會快很多。
也可以通過Android Studio的Project Stucture 開啟左側Modules,然後將右側Sources中一些目錄Excluded掉。
2-2-3 使用Android Studio匯入源碼工程
有了如上的這些操作以後,我們開啟Android Studio,然後選擇開啟一個現有的Android Studio項目,然後選擇開啟源碼根目錄下的android.ipr檔案,然後出現一樣的索引即可等待匯入就行:tu
vcS/x7DOqta5ztLDx77Nv8nS1Le9seO1xMq508NBbmRyb2lkIFN0dWRpb7Lpv7TUtMLrwLKho8jnz8LNvMv5yr6jujwvcD4NCjxwPjxpbWcgYWx0PQ=="這裡寫圖片描述" src="http://www.bkjia.com/uploads/allimg/151012/0502514Z5-1.png" title="\" />
吊不屌!!!
2-3 提示
我們看見了,可以通過Android Studio搜尋整套源碼的代碼了。但是這時候如果你開啟一個Service.java類,然後把滑鼠放在其中任意方法的Intent參數上按住CTRL+滑鼠左鍵跳轉到Intent類你會發現跳轉過去的是一個Intent.class檔案,為啥呢?因為他跳轉的是你的預設SDK中的jar內部的class檔案。既然要修改查看整套源碼,這麼跳轉得多蛋疼啊,所以我們需要配置讓其能跳轉到Intent.java檔案,具體做法如下:
首先刪掉依賴中的所有依賴,只保留中沒被選中的那兩個(當然你可以選擇保留一些你用到的其他jar),如下:
接著點擊加號的JARs or directories將你源碼的frameworks及external和你用到的其他跳轉目錄添加到依賴中,然後apply即可。
這時候我們在像上面一樣開啟Service.java跳轉Intent,你會發現像一樣直接跳轉到你源碼路徑下的Intent.java檔案了,如下:
到此對於平時只是查看源碼的人來說已經夠用了。
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 filehas one regular expression per line that matches paths (relative to theproject root) that should be excluded from the IDE configuration. Weuse Java's regular expression parser (see java.util.regex.Parser).You can create your own additional exclusion list by creating anexcluded-paths file in the project's root directory. For example, youmight exclude all apps except the Browser in your IDE configuration withthis regular expression: ^packages/apps/(?!Browser).
Controlling source root ordering (Eclipse)
You may want some source roots to come before others in Eclipse. Simplycreate a file named path-precedence in your project's root directory.Each line in the file is a regular expression that matches a source rootpath (relative to the project's root directory). If a given source root'spath matches a regular expression that comes earlier in the file, thatsource root will come earlier in the generated configuration. If a sourceroot 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 topof the path-precedence file. To make source roots under ./out come last,add ^(?!out/) (which matches all paths that don't start with out/).