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模块:
<code class="language-shell hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">mmm development<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/tools/idegen/</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
这行命令的意思是编译idegen这个模块项目,然后生成idegen.jar文件(不信你自己去查看这个模块的Android.mk的目标吧,不多解释)。运行完以后如果看到如下信息则说明编译OK:
<code class="language-txt hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">......
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#### make completed successfully (7 seconds) ####</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
接着执行如下脚本:
<code class="language-txt hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">development/tools/idegen/idegen<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sh</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
这行命令的意思是在根目录生成对应的android.ipr、android.iml IEDA工程配置文件。等待片刻得到类似如下信息说明OK:
<code class="language-txt hljs mathematica has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Read</span> excludes: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span>ms
Traversed tree: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">194799</span>ms</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
通过如上操作我们就完成了基本的源码配置工作。
2-2-2 导入Android Studio前的一些客户化操作
大家都知道使用Eclipse倒入源码很慢,Android Studio导入源码时也慢,所以建议修改android.iml文件(将自己不用的代码去掉),然后再导入Studio。
就像下面摘取的android.iml文件1887行开始的这些一样:
<code class="language-xml hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">sourceFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/./sdk/testapps/userLibTest/src"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">isTestSource</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"true"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">sourceFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/./tools/external/fat32lib/src/main/java"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">isTestSource</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"false"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/out/eclipse"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/.repo"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/external/bluetooth"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/external/chromium"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/external/icu4c"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/external/webkit"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/frameworks/base/docs"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/out/host"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/out/target/common/docs"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/out/target/common/obj/JAVA_LIBRARIES/android_stubs_current_intermediates"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/out/target/product"</span>/></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">excludeFolder</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"file://$MODULE_DIR$/prebuilt"</span>/></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
我们可以仿照上面这段代码的<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/").