使用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: 21ms Traversed tree: 194799ms
通過如上操作我們就完成了基本的源碼配置工作。
2-2-2 導入Android Studio前的一些客戶化操作
大家都知道使用Eclipse倒入源碼很慢,Android Studio導入源碼時也慢,所以建議修改android.iml文件(將自己不用的代碼去掉),然后再導入Studio。
就像下面摘取的android.iml文件1887行開始的這些一樣:
<sourceFolder url="file://$MODULE_DIR$/./sdk/testapps/userLibTest/src" isTestSource="true"/> <sourceFolder url="file://$MODULE_DIR$/./tools/external/fat32lib/src/main/java" isTestSource="false"/> <excludeFolder url="file://$MODULE_DIR$/out/eclipse"/> <excludeFolder url="file://$MODULE_DIR$/.repo"/> <excludeFolder url="file://$MODULE_DIR$/external/bluetooth"/> <excludeFolder url="file://$MODULE_DIR$/external/chromium"/> <excludeFolder url="file://$MODULE_DIR$/external/icu4c"/> <excludeFolder url="file://$MODULE_DIR$/external/webkit"/> <excludeFolder url="file://$MODULE_DIR$/frameworks/base/docs"/> <excludeFolder url="file://$MODULE_DIR$/out/host"/> <excludeFolder url="file://$MODULE_DIR$/out/target/common/docs"/> <excludeFolder url="file://$MODULE_DIR$/out/target/common/obj/JAVA_LIBRARIES/android_stubs_current_intermediates"/> <excludeFolder url="file://$MODULE_DIR$/out/target/product"/> <excludeFolder url="file://$MODULE_DIR$/prebuilt"/>
我們可以仿照上面這段代碼的<excludeFolder url="file://$MODULE_DIR$/.repo"/>
寫法一樣過濾掉不需要的內容,這樣在導入時就會快很多。
也可以通過Android Studio的Project Stucture 打開左側Modules,然后將右側Sources中一些目錄Excluded掉。
2-2-3 使用Android Studio導入源碼工程
有了如上的這些操作以后,我們打開Android Studio,然后選擇打開一個現有的Android Studio項目,然后選擇打開源碼根目錄下的android.ipr文件,然后出現如圖一樣的索引即可等待導入就行:tu
到目前為止我們就可以方便的使用Android Studio查看源碼啦。如下圖所示:
吊不屌!!!
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 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/").