在Eclipse中开发android的应用程序时,有时想查看函数的内部实现,但是当在函数上点击ctrl和鼠标左键的时候,
往往出现如下提示:
Class File Editor
Source not found
The JAR of this class file belongs to container 'Android' which does not allow modifications to source attachments on its entries.
因此按照如下操作添加对应的源代码:
选择workspace 里面的项目,点击右键--->Build Path-->configure build path -->liberaries-->选择一个jar包--->点击左侧的展开三角按钮--->
展开后选择Source attachment --->点击右侧的 Edit 按钮--->弹出对话框,并选择External File---->找到指定的源文件压缩包。或者可使用外部源文件目录。点击OK。
进去以后发现还是不能查看,同样的提示!
仔细读英文的后半部分,在libs中直接引用的第三方jar包不让其做任何修改。包括添加源文件参看。
因此在android工程下新建一个lib目录,将jar包从libs转移到lib中。然后重复以上过程。
结果就可以查看了!不过一运行你的程序,马上报错无法找到class的错误。 = =! 死胡同了~
看来只能将第三方jar放到libs了。
有的牛人这样解决了:
In order to link the sources and javadoc to a .jar library that is automatically linked by Eclipse you have to do the following.
- Place all three .jar files in the libs folder.
- Create
.properties
file in the libs folder with the name of the library .jar (see example). - Specify the relative paths to the sources and javadoc .jar in the .properties file.
- Close and re-open the Eclipse project! Optionally refresh the project.
- Select an object of the linked library in the source code.
- Open the Javadoc view in Eclipse to check the documentation (see screenshot).
- Open the source code declaration (default shortcut: F3) of the selected object.
Example: GSON library
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span class="str" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">/libs/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">gson</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2.2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2.jar</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">libs</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">gson</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2.2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2.jar</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">properties </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">libs</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">gson</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2.2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">javadoc</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">jar </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">libs</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">gson</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2.2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">sources</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">jar</span></code>
Contents of gson-2.2.2.jar.properties
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">src</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">gson</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2.2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">sources</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">jar doc</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">gson</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2.2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">javadoc</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">jar</span></code>
You can of course move the javadoc and sources .jar into other folders and specify relative paths. That's up to you.
最终,我想到了一个解决办法:
新建一个lib目录
将需要的jar包全部拷贝到该目录下
依次对每个jar包执行 上面绿色 部分操作
完成后右键lib目录,点击build path--> use as source folder
即可解决查看源码的问题,也可使程序正常执行。
在Eclipse中开发android的应用程序时,有时想查看函数的内部实现,但是当在函数上点击ctrl和鼠标左键的时候,
往往出现如下提示:
Class File Editor
Source not found
The JAR of this class file belongs to container 'Android' which does not allow modifications to source attachments on its entries.
最普遍常见的是android-support-v4没法看到其中的注释和源代码,鼠标放到类上提示:
Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
一般来说如果环境配置好了,Android的所有类和所有方法都能看到其代码和注释,以下分两步,将注释和代码全部配置正确。
1.Android本身jar包的源代码
这里假设已经把需要下载的都下载下来了,右键项目====》build path====》configure build path
在libraries里选择Android x.x.x里面有Android.jar,这就是开发时所依赖的Android本身的sdk的jar包,点开,里面有两个需要配置,
source attachment和javadoc location,前者是源代码,配置好后可实现ctrl+鼠标左键直接点开想看的Android的类,
后者是源代码的注释文件,配置好后可实现鼠标放在想看到类上,会显示出来这个类或者方法的介绍
选择source attachment配置项,点击右边edit,选择external location===》external folder,然后找到正确的路径,我的是在E:/eclipse/sdk/sources/android-19
选择javadoc location配置项,edit,选择javadoc url===》browse,然后找到正确路径,我的是在file:/E:/eclipse/sdk/docs/reference
完成上面这些就可以正常查看Android内部常用的类注释和源代码了
2.查看外部依赖jar包的源代码
这里假设你已经把外部依赖的jar包正确导入并且编译通过了,但是ctrl+鼠标左键想看代码或者想看其注释却不可行的情况,
首先在libs下面找到引入的包,这里以android-support-v4.jar为例,
在libs下新建android-support-v4.jar.properties文件,即与引用的jar包同名最后加上.properties的文件
其内容如下:
src = E:\\eclipse\\sdk\\extras\\android\\support\\v4\\src
这里路径要根据你自己的路径进行调整,配置之后退出eclipse,在进入,就可以正常查看了。
作者:jason0539
微博:http://weibo.com/2553717707
博客:http://blog.csdn.net/jason0539(转载请说明出处)