当前位置: 首页 > 知识库问答 >
问题:

用gradle编译android库时如何抑制警告?

蓟辰沛
2023-03-14

在我的应用程序中,我有一个由第三方开发的库,不幸的是,它包含相当多的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)的未检查调用if(type . isAssignableFrom(throwables[I])。getClass()))

共有1个答案

万高洁
2023-03-14

docs:http://Google . github . io/Android-gradle-DSL/current/com . Android . build . gradle . internal . DSL . lint options . html # com . Android . build . gradle . internal . DSL . lint options:ignore warnings

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

文档: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'
}
 类似资料:
  • 我正在开发一个使用OpenCV和加速的C项目。不幸的是,在编译时,我的编译器从这些库的包含文件中给了我数百个警告。即使有一个空的主函数,没有其他代码,我仍然从包含语句中得到这些警告。我听说这是Qt等其他第三方库的问题。所有伟大的图书馆。如何抑制 MSVC 中的所有第三方警告。 我知道这些解决方案: 在GCC中:#pragma GCC system_header#include“real_3rd_p

  • 问题内容: 我们总是被教导要确保在switch语句中使用 break 来避免掉线。 Java编译器会针对这些情况发出警告,以帮助我们避免犯小错误(而是严重错误)。 但是,我将案例检查作为功能使用(我们不必在这里进行介绍,但是它提供了一种非常优雅的解决方案)。 但是,编译器会吐出大量警告,这些警告可能会掩盖我需要了解的警告。我知道如何更改编译器以忽略所有掉线警告,但是我想在逐个方法的基础上实施此操作

  • 我应该启用哪些确切的gcc标志来获得此警告? 为什么将声明为volatile会抑制该警告?

  • 问题内容: 每次我都会收到警告: 抑制它的最佳方法是什么?所有软件包都是最新的。 Conf: OSX带有Brew Python 2.7.10(默认,2015年7月13日,12:05:58),pandas == 0.17.0和matplotlib == 1.5.0 问题答案: 您可以禁止所有警告:

  • 我最近开始尝试llvm的< code>clang-tidy工具。现在我正试图抑制来自第三方库代码的错误警告。为此,我想使用命令行选项

  • 我们的开源应用程序以Java 6平台为目标,因此我们使用-source 1.6和-target 1.6选项进行编译,但当使用JDK 7时,我们会收到以下警告消息,例如:。 例如:$javac-source 1.6-target 1.6-test。JAVA 警告:[选项]引导类路径未与-source 1.6一起设置 我们可以通过为我们的机器使用-bootclasspath来解决这个问题,但是我们将我