简体   繁体   English

如何在 javadoc 中生成指向 android 类引用的链接?

[英]How to generate links to the android Classes' reference in javadoc?

When I generate javadoc for my Android project in Eclipse, there are lots of warnings like当我在 Eclipse 中为我的 Android 项目生成 javadoc 时,有很多警告,比如

cannot find symbol
symbol  : class TextView

and

warning - Tag @see: reference not found: android.app.Dialog

I also tried我也试过

-link http://developer.android.com/reference/
-link http://java.sun.com/j2se/1.4.2/docs/api/

in Extra javadoc options (path names with white spaces must be enclosed in quotes) tab in Configure Javadoc Arguments (3rd dialog of eclipse->project->Generate Javadoc).Configure Javadoc Arguments (eclipse->project->Generate Javadoc 的第三个对话框Extra javadoc options (path names with white spaces must be enclosed in quotes)中的Extra javadoc options (path names with white spaces must be enclosed in quotes)选项卡中。

But only -link http://java.sun.com/j2se/1.4.2/docs/api/ is working ie for String class link http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html?is-external=true is generated.但只有-link http://java.sun.com/j2se/1.4.2/docs/api/正在工作,即用于字符串类链接http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html?is-external=true生成。 but for android.app.Dialog , no link is generated.但是对于android.app.Dialog ,不会生成链接。

Edit编辑

I also tried selecting android.jar in Select referenced archives and projects to which links should be generated tab in Configure Javadoc arguments for standard doclet (2nd dialog of eclipse->project->Generate Javadoc), but this creates local links to docs in local android-sdk directory, NOT the online Android references like it does for Java APIs.我还尝试在Configure Javadoc arguments for standard doclet (eclipse->project->Generate Javadoc 的第二个对话框)中的Select referenced archives and projects to which links should be generated选项卡中选择android.jar ,但这会创建到本地文档的本地链接android-sdk 目录,而不是像 Java API 那样的在线 Android 参考。

Javadoc relies on a file called package-list to determine what Java packages are documented below a given directory. Javadoc 依赖于一个名为package-list的文件来确定在给定目录下记录了哪些 Java 包。 For some reason, such a file is missing for http://d.android.com/reference/ , therefore the "naive" approach with出于某种原因, http://d.android.com/reference/缺少这样的文件,因此“天真”的方法

-link http://d.android.com/reference/

doesn't work – you get a warning that the package-list could not be retrieved and no links are generated into your docs.不起作用 - 您收到一条警告,提示无法检索包列表,并且您的文档中未生成任何链接。 (Note: The checkboxes in that 2nd eclipse dialog just assemble -link parameters for you, so that doesn't really make any difference) (注意:第二个 Eclipse 对话框中的复选框只是为您组装-link参数,因此这并没有什么区别)

However, Javadoc offers the -linkoffline parameter to be able to adjust for precisely this situation: You want to link to some other Javadoc documentation online, but you cannot access it at the time of generating your own docs.但是,Javadoc 提供了-linkoffline参数,可以精确地针对这种情况进行调整:您希望链接到其他一些在线 Javadoc 文档,但在生成自己的文档时无法访问它。 Here's how it works: While -link takes only one parameter (the URL of the JavaDoc docs you want to link to), -linkoffline takes a second one.它的工作原理如下: -link只接受一个参数(您要链接到的 JavaDoc 文档的 URL),而-linkoffline接受第二个参数。 That one is the location of the package-list file!那个是package-list文件的位置!

So, to link to the online Android reference documentation, you should not select any checkboxes in the 2nd eclipse dialog, but instead add因此,要链接到网上Android的参考文档,你应该选择在第二日食对话框任何复选框,而是添加

-linkoffline http://d.android.com/reference file:/C:/pathtoyour/android-sdk-windows/docs/reference

in the Extra Javadoc options in the 3rd dialog.在第三个对话框的额外 Javadoc 选项中。 That way you use the package-list of your locally installed Android docs, but the links in your generated Javadoc will still point to the online version anyway.这样您就可以使用本地安装的 Android 文档的package-list ,但生成的 Javadoc 中的链接仍然指向在线版本。

Hope it helps!希望能帮助到你!

By now (August 2020), Google has published a package-list on its Android developer documentation.到目前为止(2020 年 8 月),Google 已在其 Android 开发人员文档中发布了一个package-list Hence, simply linking to https://developer.android.com/reference/ or https://d.android.com/reference/ will work (just tried this with success).因此,只需链接到https://developer.android.com/reference/https://d.android.com/reference/ 即可(刚刚尝试成功)。

Minor glitch: if you open the generated Javadoc and try to follow a link to an Android class (or one of the standard Java classes, which will also link to the Android documentation) and you are in frame mode, your browser might refuse to follow the link, as the Android site does not allow embedding in another page.小故障:如果您打开生成的 Javadoc 并尝试跟随指向 Android 类(或标准 Java 类之一,也将链接到 Android 文档)的链接,并且您处于框架模式,则您的浏览器可能会拒绝跟随链接,因为 Android 站点不允许嵌入到另一个页面中。 Firefox then gives you the alternative of opening the target document in a new tab. Firefox 然后为您提供了在新选项卡中打开目标文档的替代方法。

After a bit of trial and error (And plenty of suggestions gleaned from multiple web searches), I was able to get this working with a specific ANT script, which can be run in Eclipse by "Run As -> Ant Build".经过一些试验和错误(以及从多个网络搜索中收集到的大量建议),我能够使用特定的 ANT 脚本来实现它,该脚本可以通过“Run As -> Ant Build”在 Eclipse 中运行。

I saved this file, "javadoc.xml", in the directory of my project, in parallel with the AndroidManifest.xml file.我将这个文件“javadoc.xml”保存在我的项目目录中,与 AndroidManifest.xml 文件并行。

Here is the content of the file:这是文件的内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="doc" name="api docs">
<target name="doc" description="my docs">
<javadoc destdir="docs" doctitle="Testing the Title" verbose="on" 
    use="true" 
    classpath="C:\Android\android-sdk_r04-windows\android-sdk-windows\platforms\android-2.1\android.jar;.\libs\admob-sdk-android.jar"
    sourcepath="gen;src"
    linkoffline="http://d.android.com/reference C:\Android\android-sdk_r04-windows\android-sdk-windows\docs\reference"
    stylesheetfile="C:\Android\android-sdk_r04-windows\android-sdk-windows\docs\assets\android-developer-docs.css"
    >
</javadoc>
</target>
</project>

Thanks @Henning谢谢@Henning

Here is what I needed in ant build.xml file:这是我在 ant build.xml 文件中需要的内容:

<link offline="true" href="http://d.android.com/reference" packagelistLoc="${android.home}/docs/reference"/>

Although I followed to top answer here, I found I could only get it to work if I exported an ant build file (javadoc.xml), and manually added the android.jar file to the classpath.尽管我遵循了此处的最佳答案,但我发现只有导出 ant 构建文件 (javadoc.xml) 并手动将 android.jar 文件添加到类路径中才能使其工作。 My javadoc.xml looks like:我的 javadoc.xml 看起来像:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="javadoc">
    <target name="javadoc">
        <javadoc access="private" additionalparam=" -linkoffline http://developer.android.com/reference file:/opt/android-sdk-linux_x86/docs/reference" author="true" classpath=".:/opt/android-sdk-linux_x86/platforms/android-8/android.jar" destdir="doc" nodeprecated="false" nodeprecatedlist="false" noindex="false" nonavbar="false" notree="false" packagenames="com.example.mypackagename" source="1.5" sourcepath="gen:src" splitindex="true" use="true" version="true"/>
   </target>
</project>

I could then generate the document using ant -f javadoc.xml .然后我可以使用ant -f javadoc.xml生成文档。 I couldn't figure out a way to do it properly from the Eclipse GUI, as even selecting the correct referenced archive did not cause Eclipse to add android.jar to the classpath.我无法从 Eclipse GUI 中找到正确执行此操作的方法,因为即使选择正确的引用存档也不会导致 Eclipse 将 android.jar 添加到类路径中。

If you have problems with the path to the SDK you can manually create a package-list file (or copy the one under $ANDROID_HOME/docs/reference/package-list ) to your project.如果您对 SDK 的路径有疑问,您可以手动创建一个package-list文件(或将$ANDROID_HOME/docs/reference/package-list下的文件复制到您的项目中)。

To use a relative path you shouldn't use the file:/ prefix.要使用相对路径,您不应使用file:/前缀。 If you put the file in sub-directory/package-list the argument would be -linkoffline http://d.android.com/reference sub-directory如果您将文件放在sub-directory/package-list则参数将为-linkoffline http://d.android.com/reference sub-directory

For those of us daring enough to have switched over to the ever-evolving Android Studio & Gradle, one of the things that actually works and makes things a little easier is referring to a package-list location either locally or on the web.对于我们这些敢于切换到不断发展的 Android Studio 和 Gradle 的人来说,真正有效并使事情变得更容易的事情之一是引用本地或网络上的包列表位置。 For example, the following code from Julian generates javadoc files:例如,来自Julian的以下代码生成 javadoc 文件:

task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
    title = "Documentation for Android $android.defaultConfig.versionName b$android.defaultConfig.versionCode"
    destinationDir = new File("${project.getProjectDir()}/doc/compiled/", variant.baseName)
    source = variant.javaCompile.source
    ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
    classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)

    description "Generates Javadoc for $variant.name."

    options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PRIVATE
    options.links("http://docs.oracle.com/javase/7/docs/api/");
    options.links("http://developer.android.com/reference/reference/");
    exclude '**/BuildConfig.java'
    exclude '**/R.java'
}

But this resulted in a warning during the build because the package-list isn't available anymore (if it ever was) at但这导致在构建过程中出现警告,因为包列表不再可用(如果曾经可用)在

options.links("http://developer.android.com/reference/reference/");

At first I thought maybe the redundant reference/reference was the issue, but the package-list was also unavailable at起初我认为可能是冗余引用/引用是问题,但包列表也无法在

options.links("http://developer.android.com/reference/");

So, after reading the discussion on this thread, that one line can be changed to refer to the local reference directory within the SDK:因此,在阅读有关此线程的讨论后,可以将这一行更改为引用 SDK 中的本地参考目录:

options.links("c:\\path-to-sdk-directory\\docs\\reference");

It finds the package-list locally and doesn't display the warning message anymore.它在本地找到包列表并且不再显示警告消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM