25

In my application I have a library developed by a 3rd party that unfortunately contains quite a few lint and javac warnings. I'd like to ignore both these types of warnings since they can't be fixed by our team and they are polluting our build logs. I have tried adding the following to the libraries build.gradle file:

In the android block

lintOptions {
    ignoreWarnings = true
}

I also added the following to the end of the build.gradle file:

afterEvaluate {
    tasks.withType(JavaCompile) {
         it.options.compilerArgs << "-Xlint:none" << "-nowarn"
    }
}

Unfortunately, whenever ":compileDebugJavaWithJavac" runs, it still outputs the warnings from this project. What am I doing wrong?

EDIT Here is the build.gradle file in its entirety

apply plugin: 'com.android.library'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 21
    buildToolsVersion "23.0.2"

    lintOptions {
       abortOnError false        // true by default
       checkAllWarnings false
       checkReleaseBuilds false
       ignoreWarnings true       // false by default
       quiet true                // false by default
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

afterEvaluate {
    tasks.withType(JavaCompile) {
         it.options.compilerArgs << "-Xlint:none" << "-nowarn"
    }
}

The following is an example of the warnings I am getting that I want to eliminate:

warning: [unchecked] unchecked call to isAssignableFrom(Class) as a member of the raw type Class if (type.isAssignableFrom(throwables[i].getClass()))

Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
  • Which warnings? Lint or Javac? If you ignore all warnings, you won't see your own. – Jared Burrows Dec 08 '15 at 05:19
  • Sorry to clarify, I'd like to suppress all warnings from this particular project (both lint and javac). Let me edit the question to clarify those things, thanks. Also, just to be a little clearer than my original question, I am only modifying the build.gradle file for the offending library. – Justin Breitfeller Dec 08 '15 at 15:23
  • if you use that library as gradle dependency, does `//noinspection unchecked` above dependency help you? – Stas Parshin Dec 13 '15 at 19:20

1 Answers1

33

Lint:

Docs: http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.LintOptions.html#com.android.build.gradle.internal.dsl.LintOptions:ignoreWarnings

android {
   lintOptions {
      abortOnError false        // true by default
      checkAllWarnings false
      checkReleaseBuilds false
      ignoreWarnings true       // false by default
      quiet true                // false by default
   }
}

Javac:

Docs: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html

From the docs, list of all warnings:

Docs: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html#BHCJCABJ

Java version:

$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

Also, warning options:

$ javac -X
  -Xlint                     Enable recommended warnings
  -Xlint:{all,auxiliaryclass,cast,classfile,deprecation,dep-ann,divzero,empty,fallthrough,finally,options,overloads,overrides,path,processing,rawtypes,serial,static,try,unchecked,varargs,-auxiliaryclass,-cast,-classfile,-deprecation,-dep-ann,-divzero,-empty,-fallthrough,-finally,-options,-overloads,-overrides,-path,-processing,-rawtypes,-serial,-static,-try,-unchecked,-varargs,none} Enable or disable specific warnings
  -Xdoclint                  Enable recommended checks for problems in javadoc comments
  -Xdoclint:(all|none|[-]<group>)[/<access>] 
        Enable or disable specific checks for problems in javadoc comments,
        where <group> is one of accessibility, html, missing, reference, or syntax,
        and <access> is one of public, protected, package, or private.
  -Xbootclasspath/p:<path>   Prepend to the bootstrap class path
  -Xbootclasspath/a:<path>   Append to the bootstrap class path
  -Xbootclasspath:<path>     Override location of bootstrap class files
  -Djava.ext.dirs=<dirs>     Override location of installed extensions
  -Djava.endorsed.dirs=<dirs> Override location of endorsed standards path
  -Xmaxerrs <number>         Set the maximum number of errors to print
  -Xmaxwarns <number>        Set the maximum number of warnings to print
  -Xstdout <filename>        Redirect standard output
  -Xprint                    Print out a textual representation of specified types
  -XprintRounds              Print information about rounds of annotation processing
  -XprintProcessorInfo       Print information about which annotations a processor is asked to process
  -Xprefer:{source,newer}    Specify which file to read when both a source file and class file are found for an implicitly compiled class
  -Xpkginfo:{always,legacy,nonempty} Specify handling of package-info files
  -Xplugin:"name args"       Name and optional arguments for a plug-in to be run
  -Xdiags:{compact,verbose}  Select a diagnostic mode

These options are non-standard and subject to change without notice.

Turn all warnings off:

// Put this in 'root' `build.gradle`, in allprojects or subprojects
tasks.withType(JavaCompile) {
     // Try to turn them all off automatically
     options.compilerArgs << '-Xlint:none'
     options.compilerArgs << '-nowarn' // same as '-Xlint:none'

     // Turn them off manually
     options.compilerArgs << '-Xlint:-auxiliaryclass'
     options.compilerArgs << '-Xlint:-cast'
     options.compilerArgs << '-Xlint:-classfile'
     options.compilerArgs << '-Xlint:-deprecation'
     options.compilerArgs << '-Xlint:-dep-ann'
     options.compilerArgs << '-Xlint:-divzero'
     options.compilerArgs << '-Xlint:-empty'
     options.compilerArgs << '-Xlint:-fallthrough'
     options.compilerArgs << '-Xlint:-finally'
     options.compilerArgs << '-Xlint:-options'
     options.compilerArgs << '-Xlint:-overloads'
     options.compilerArgs << '-Xlint:-overrides'
     options.compilerArgs << '-Xlint:-path'
     options.compilerArgs << '-Xlint:-processing'
     options.compilerArgs << '-Xlint:-rawtypes'
     options.compilerArgs << '-Xlint:-serial'
     options.compilerArgs << '-Xlint:-static'
     options.compilerArgs << '-Xlint:-try'
     options.compilerArgs << '-Xlint:-unchecked'
     options.compilerArgs << '-Xlint:-varargs'
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • 2
    I have edited the question and tried your suggestions but unfortunately it doesn't work. The '-Xmaxwarns 0' flag said it was an invalid option. – Justin Breitfeller Dec 09 '15 at 14:42
  • @JustinBreitfeller That fixed your `lint` warnings. For the Java, are you using jdk7? `java -version` to check. – Jared Burrows Dec 09 '15 at 16:14
  • I'm using 1.8.0_66. I would expect the -nowarn flag to remove the compiler warnings but it doesnt seem to work. – Justin Breitfeller Dec 09 '15 at 16:44
  • @JustinBreitfeller Can you please remove `afterEvaluate` and put the `tasks.withType(JavaCompile) {` in your `root` project? – Jared Burrows Dec 11 '15 at 08:07
  • @JaredBarrows I removed the surrounding `afterEvaluate` block and I'm still getting the warnings. – Justin Breitfeller Dec 11 '15 at 15:57
  • @JarredBarrows I tracked down what was causing the problem. A root gradle file was adding explicit `-Xlint:unchecked` args to the compiler arguments for all our projects that I was not aware of. It seems that even if `Xlint:-unchecked' is present, it will still show the warnings. Either way, I appreciate all your help so Ill accept your answer. – Justin Breitfeller Dec 14 '15 at 04:37
  • @JustinBreitfeller Thanks for getting back to me. – Jared Burrows Dec 14 '15 at 06:44
  • I added all of these and removed the ones that it complained about (JDK7) and I still get this horrible warnings output. – Sridhar Sarnobat Oct 05 '20 at 23:37