简体   繁体   English

使用 gradle 生成 android 库的 javadoc 时排除内部 java 类

[英]Exclude internal java classes when generating the javadoc of an android library using gradle

I'm trying to build the javadoc for a library which contains some internal classes which are not relevant for the developers.我正在尝试为包含一些与开发人员无关的内部类的库构建 javadoc。 Previously with java 1.6 I was able to exclude the classes I don't want to appear in the javadoc using the exclude option, But since java 8 this is not the case anymore.以前使用 java 1.6 我可以使用exclude选项排除我不想出现在 javadoc 中的类,但自从 java 8 以来,情况不再如此。 When I use the exclude option the classes are just removed and the javadoc lint throws an error because other classes need the internal classes which I want to exclude.当我使用exclude选项时,类被删除并且 javadoc lint 抛出错误,因为其他类需要我想要排除的内部类。 Here the example:这里的例子:

task javadoc(type: Javadoc) {
    // failOnError false -> This will tell gradle the job was successful but nothing is generated
    source = android.sourceSets.main.java.sourceFiles
    exclude 'com/example/customlib/internal/Utils.java'
}

Running the task throws the following error:运行任务会抛出以下错误:

[..]/customlib/src/main/java/com/example/customlib/Foo.java:3: error: package com.example.customlib.internal does not exist

After searching a solution I came across many proposals but non of them worked.在搜索解决方案后,我遇到了很多建议,但没有一个有效。 It appears the javadoc lint is firing the error when it generates the docs.似乎 javadoc lint 在生成文档时触发了错误。 Then I've tried disabling the lint for the specific file as follows:然后我尝试禁用特定文件的 lint,如下所示:

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.sourceFiles
    exclude 'com/example/customlib/internal/Utils.java'
    options.addStringOption('Xdoclint:none src/main/java/com/example/customlib/internal/Utils.java', '-quiet')
}

Now the tasks succeed but the classes I want to exclude are present in the generated javadoc.现在任务成功了,但我想排除的类出现在生成的 javadoc 中。

Does anybody knows how to remove specific classes/packages from the generated javadoc?有人知道如何从生成的 javadoc 中删除特定的类/包吗?

After more investigation I found the way to exclude files from the javadoc.经过更多调查后,我找到了从 javadoc 中排除文件的方法。 Apparently there is a bug in the gradle task and the following workaround will allow the javadoc task to finish gratefully显然 gradle 任务中存在一个错误,以下解决方法将使 javadoc 任务顺利完成

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.sourceFiles
    exclude 'com/example/customlib/internal/Utils.java'

    // The following lines will workaround the problem with the exclude
    var sourceSetDirectories = android.sourceSets.main.java.srcDirs.join(":")
    options.addStringOption("-source-path", sourceSetDirectories)
}

If the library has references to other libs like androidx or android classes, add the classpath parameter and option.links :如果库引用了其他库,如 androidx 或 android 类,请添加classpath参数和option.links

task javadoc(type: Javadoc) {
    // failOnError false -> This will tell gradle the job was successful but nothing is generated
    source = android.sourceSets.main.java.sourceFiles
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    options {
        links "http://docs.oracle.com/javase/7/docs/api/"
        linksOffline('https://d.android.com/reference/', 'https://d.android.com/reference/androidx/')
    }

    exclude 'com/example/customlib/internal/Utils.java'

    // The following lines will workaround the problem with the exclude
    var sourceSetDirectories = android.sourceSets.main.java.srcDirs.join(":")
    options.addStringOption("-source-path", sourceSetDirectories)
}

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

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