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 下的註釋文檔。