Search code examples
javaandroidgradlejavadoclint

Suppress Javadoc Warnings in Android project with Gradle


I got some troubles with javadoc warnings. When I push my commits and create a pull request in my GitHub repository a Lint Code Base test runs and then shows in the console that a lot of methods and classes do not have a Javadoc comment like below:

Missing a Javadoc comment. [JavadocVariable]

I need to suppress all the Javadoc warnings, but I can't and I tried different ways. My research took me to many threads about this topic, but I did not find any solution to solved my problem.

I tried adding:

tasks.withType(Javadoc) {
    failOnError false
    options.addStringOption('Xdoclint:none', '-quiet')
    options.addBooleanOption('Xdoclint:none', true)
    options.compilerArgs << "-Xdoclint:none"
}
tasks.withType(Javadoc).all { enabled = false }
subprojects {
    tasks.withType(Javadoc).all { enabled = false }
}
subprojects {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xdoclint:none"
    }
}

But no one couldn't suppress the javadoc warnings.

All of these lines of code where inside allprojects method.

At this point my build.gradle looks like this:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.1'
        classpath 'com.google.gms:google-services:4.3.10'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'com.android.library' version '7.3.0' apply false
}

allprojects {
    repositories {
        // Check that you have the following line (if not, add it):
        google() // Google's Maven repository
        jcenter()
    }

    //
    tasks.withType(Javadoc) {
        failOnError false
        options.addStringOption('Xdoclint:none', '-quiet')
        options.addBooleanOption('Xdoclint:none', true)
        options.compilerArgs << "-Xdoclint:none"
    }

    tasks.withType(Javadoc).all { enabled = false }

    subprojects {
        tasks.withType(Javadoc).all { enabled = false }
    }

    subprojects {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xdoclint:none"
        }
    }
}

=== UPDATE ===

As the first answer says and recommends I created a checkstyle.xml file in the root of my project. The content of checkstyle.xml is the following:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
    "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
    <module name="TreeWalker">
        <!-- Javadoc -->
        <!-- Configuración para permitir comentarios Javadoc faltantes en JavadocVariable -->
        <module name="JavadocVariable">
            <property name="allowMissingJavadoc" value="true" />
        </module>

        <!-- Configuración para permitir comentarios Javadoc faltantes en JavadocMethod -->
        <module name="JavadocMethod">
            <property name="allowMissingJavadoc" value="true" />
        </module>

        <!-- Configuración para permitir comentarios Javadoc faltantes en JavadocPackage -->
        <module name="JavadocPackage">
            <property name="allowMissingJavadoc" value="true" />
        </module>

        <!-- Formatting -->
        <module name="LineLength">
            <property name="max" value="160" />
        </module>
    </module>
</module>

and in gradle added this:

The plugin:

plugins {
    id 'checkstyle'
}

The Task:

checkstyle {
    toolVersion = '8.42'
    configFile = file("${rootDir}/config/checkstyle/checkstyle.xml")
}
checkstyleMain {
    source = 'src/main/java'
}
checkstyleTest {
    source = 'src/test/java'
}

But the warnings still showing after the Lint Code Base of GitHub Actions runs.


Solution

  • The problem is that these aren't warnings from the javadoc linter, so configuring or disabling the javadoc tool is not going to help you. The message "Missing a Javadoc comment. [JavadocVariable]" is a CheckStyle warning, so you have CheckStyle running, and its profile is configured to require javadoc on all variables (actually, fields).

    Given CheckStyle isn't listed in your Gradle file, you probably run it from a GitHub Action (or your build.gradle is not complete).

    As far as I'm aware, the JavadocVariable rule is not enabled by default, so to disable it, remove it from your CheckStyle XML config.

    Based on the CheckStyle documentation, the rule looks something like this:

    <module name="Checker">
      <module name="TreeWalker">
        <module name="JavadocVariable"/>
      </module>
    </module>
    

    Searching for name="JavadocVariable" and removing the specific module is likely the best way to deal with this.