如何在使用gradle编译Android库时抑制警告?

25

我的应用程序中有一个由第三方开发的库,不幸的是其中包含了相当多的lint和javac警告。我想忽略这两种类型的警告,因为它们无法由我们的团队修复,并且它们正在污染我们的构建日志。我尝试将以下内容添加到库的build.gradle文件中:

在android块中

lintOptions {
    ignoreWarnings = true
}

我还在build.gradle文件末尾添加了以下内容:

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

不幸的是,每当“:compileDebugJavaWithJavac”运行时,它仍会输出来自该项目的警告。我做错了什么?

编辑这是完整的build.gradle文件

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"
    }
}
以下是我想要消除的警告示例:

警告:[unchecked]未经检查的调用isAssignableFrom(Class)作为 原始类型Class的成员 if (type.isAssignableFrom(throwables[i].getClass()))


哪些警告?Lint 还是 Javac?如果你忽略所有的警告,你就看不到自己的错误了。 - Jared Burrows
抱歉需要澄清,我想要抑制这个特定项目中所有警告(包括 lint 和 javac )。让我编辑一下问题以澄清这些事情,谢谢。另外,为了比我的原始问题更清晰一些,我只修改有问题的库的 build.gradle 文件。 - Justin Breitfeller
如果您将该库作为Gradle依赖项,那么在依赖项上方使用“//noinspection unchecked”是否有帮助? - Stas Parshin
1个回答

33

代码检查:

文档: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:

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

文档中列出的所有警告:

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

Java 版本:

$ 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)

同时,警告选项:

$ 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.

关闭所有警告:

// 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'
}

2
我已经编辑了问题并尝试了您的建议,但不幸的是它没有起作用。'-Xmaxwarns 0'标志表示这是一个无效选项。 - Justin Breitfeller
@JustinBreitfeller 这样就可以解决你的lint警告了。对于Java,你是否在使用jdk7?可以通过输入java -version进行检查。 - Jared Burrows
我正在使用1.8.0_66版本。我期望-nowarn标志可以消除编译器警告,但似乎并没有起作用。 - Justin Breitfeller
@JustinBreitfeller,您能否将afterEvaluate删除,并将tasks.withType(JavaCompile) {放在您的root项目中? - Jared Burrows
@JarredBarrows,我找到了问题的根源。一个根Gradle文件正在为我不知道的所有项目添加显式的“-Xlint:unchecked”参数到编译器参数中。似乎即使存在“Xlint:-unchecked”,它仍然会显示警告。无论如何,我感谢您的帮助,我将接受您的答案。 - Justin Breitfeller
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接