Android Studio 关联源码的方法和 Eclipse 不太一样:
Eclipse 所关联的 SDK 默认不包含源码文件,需要手动去找资源并下载,下载到 $USER_HOME$/charo/software/sdk/sdk/source/src 目录下,其中源码自带有文档注释,以 Android 中 Activity 的 findViewById 方法为例,如下所示(Activity.class):
/**
* Look for a child view with the given id. If this view has the given
* id, return this view.
*
* @param id The id to search for.
* @return The view that has the given id in the hierarchy or null
*/
@Nullable
public final View findViewById(@IdRes int id) {
if (id < 0) {
return null;
}
return findViewTraversal(id);
}
Android Studio 所关联的 SDK 默认包含了源码,源码附带有注释文档。同时官方还有独立的注释文档,需要通过 SDK Manager 下载,如图:
存放目录为:$USER_NAME$/charo/software/adt-bundle-linux-x86-20140321/sdk/docs,此时可以在代码中跳转到源码查看,如下(Activity.class)(该结果是已经正确配置了sourcePath节点后的结果):
/**
* Finds a view that was identified by the id attribute from the XML that
* was processed in {@link #onCreate}.
*
* @return The view if found or null otherwise.
*/
@Nullable
public View findViewById(@IdRes int id) {
return getWindow().findViewById(id);
}
异常:
当我们在代码中 Ctrl + Q 查看注释时,显示:Fecthing document...,或者只显示了方法本身而已, Ctrl + 左键 点进去看源码,屏幕上方会显示:Souces for android api 23 platform not found :
(导致该图所示结果原因是sourcePath配置了错误路径)
这时要手动关联源码及注释,vi $USER_HOME$/.AndroidStudio2.1/config/options/jdk.table.xml,
添加内容为 javadocPath 节点下的:
(文件位置为:$USER_NAME$/charo/software/adt-bundle-linux-x86-20140321/sdk/docs/reference)
<root type="simple" url="file://$USER_HOME$/charo/software/adt-bundle-linux-x86-20140321/sdk/docs/reference " />
以及 sourcePath 下的:
(文件位置为:$USER_NAME$/charo/software/adt-bundle-linux-x86-20140321/sdk/sources/android-23)
<root type="simple" url="file://$USER_HOME$/charo/software/adt-bundle-linux-x86-20140321/sdk/sources/androi d-23" />
jdk.table.xml 添加结果后,总体内容如下:
<application>
<component name="ProjectJdkTable">
<jdk version="2">
<name value="Android API 23 Platform" />
<type value="Android SDK" />
<homePath value="$USER_HOME$/charo/software/adt-bundle-linux-x86-20140321/sdk" />
<roots>
<annotationsPath>
<root type="composite">
<root type="simple" url="jar://$APPLICATION_HOME_DIR$/plugins/android/lib/androidAnnotations.jar!/" />
</root>
</annotationsPath>
<classPath>
<root type="composite">
<root type="simple" url="jar://$USER_HOME$/charo/software/adt-bundle-linux-x86-20140321/sdk/platforms/android-23/android.jar!/" />
<root type="simple" url="file://$USER_HOME$/charo/software/adt-bundle-linux-x86-20140321/sdk/platforms/android-23/data/res" />
</root>
</classPath>
<javadocPath>
<root type="composite">
<root type="simple" url="file://$USER_HOME$/charo/software/adt-bundle-linux-x86-20140321/sdk/docs/reference " />
</root>
</javadocPath>
<sourcePath>
<root type="composite">
<root type="simple" url="file://$USER_HOME$/charo/software/adt-bundle-linux-x86-20140321/sdk/sources/android-23" />
</root>
</sourcePath>
</roots>
<additional jdk="1.7" sdk="android-23" />
</jdk>
<jdk version="2">
<name value="1.7" />
<type value="JavaSDK" />
<version value="java version "1.7.0_55"" />
<homePath value="$USER_HOME$/charo/software/jdk1.7.0_55" />
<roots>
<annotationsPath>
<root type="composite">
<root type="simple" url="jar://$APPLICATION_HOME_DIR$/lib/jdkAnnotations.jar!/" />
</root>
</annotationsPath>
<classPath>
<root type="composite">
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/charsets.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/deploy.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/ext/dnsns.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/ext/localedata.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/ext/mysql-connector-java-5.1.16.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/ext/sunec.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/ext/sunjce_provider.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/ext/sunpkcs11.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/ext/zipfs.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/javaws.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/jce.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/jfr.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/jfxrt.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/jsse.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/management-agent.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/plugin.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/resources.jar!/" />
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/jre/lib/rt.jar!/" />
</root>
</classPath>
<javadocPath>
<root type="composite">
<root type="simple" url="http://docs.oracle.com/javafx/2/api/" />
</root>
</javadocPath>
<sourcePath>
<root type="composite">
<root type="simple" url="jar://$USER_HOME$/charo/software/jdk1.7.0_55/src.zip!/" />
</root>
</sourcePath>
</roots>
<additional />
</jdk>
</component>
</application>
其中,若只添加 sourcePath 节点内容,而不配置 javadocPath 节点,重启 Android Studio,按 Ctrl + Q,显示:
此时已经有了注释,此注释为源码本身所关联的注释。
添加了javadocPath节点内容后,重启 Android Studio,按 Ctrl + Q,显示:
此时显示的是目录 sdk/docs/reference 下的注释文档。