是否可以在gradle中为JavaCompile启用-Werror?

时间:2015-01-15 16:28:25

标签: java android android-gradle compiler-warnings

我使用com.android.application插件在gradle中设置了Android项目。我希望javac lint警告触发错误而不仅仅是警告。

以下情况适用于警告:

tasks.withType(JavaCompile).all {
    options.compilerArgs.add("-Xlint:all")
}

但是,添加-Werror:

tasks.withType(JavaCompile).all {
    options.compilerArgs.add("-Xlint:all")
    options.compilerArgs.add("-Werror")
}

导致构建完全失败(不是以正确的方式):

  

失败:构建因异常而失败。

     

*出了什么问题:任务执行失败':app:compileDebugJava'。

     
    

显示java.lang.NullPointerException

  
     

*尝试:使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获得更多日志输出。

     

建立失败

使用--stacktrace调用gradle显示:

  

*异常是:org.gradle.api.tasks.TaskExecutionException:任务':app:compileDebugJava'执行失败。在   org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)     在   org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)     在   org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)     在   org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)     在   org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)     在   org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)     在   org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)     在   org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)     在   org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)     在   org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:305)     在   org.gradle.execution.taskgraph.Abs​​tractTaskPlanExecutor $ TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)     在   org.gradle.execution.taskgraph.Abs​​tractTaskPlanExecutor $ TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)     在   org.gradle.execution.taskgraph.Abs​​tractTaskPlanExecutor $ TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)     在   org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)     在   org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:88)     在   org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:29)     在   org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)     在   org.gradle.execution.DefaultBuildExecuter.access $ 200(DefaultBuildExecuter.java:23)     在   org.gradle.execution.DefaultBuildExecuter $ 2.proceed(DefaultBuildExecuter.java:68)     在   org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)     在   org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)     在   org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:55)     在   org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:149)     在   org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)     在   org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)     在   org.gradle.launcher.exec.InProcessBuildActionExecuter $ DefaultBuildController.run(InProcessBuildActionExecuter.java:80)     在   org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:33)     在   org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:24)     在   org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:36)     在   org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26)     在org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:51)     在   org.gradle.internal.Actions $ RunnableActionAdapter.execute(Actions.java:171)     在   org.gradle.launcher.cli.CommandLineActionFactory $ ParseAndBuildAction.execute(CommandLineActionFactory.java:237)     在   org.gradle.launcher.cli.CommandLineActionFactory $ ParseAndBuildAction.execute(CommandLineActionFactory.java:210)     在   org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)     在   org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)     在   org.gradle.launcher.cli.CommandLineActionFactory $ WithLogging.execute(CommandLineActionFactory.java:206)     在   org.gradle.launcher.cli.CommandLineActionFactory $ WithLogging.execute(CommandLineActionFactory.java:169)     在   org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)     在   org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)     在org.gradle.launcher.Main.doAction(Main.java:33)at   org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)at   org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)     在   org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)     在org.gradle.launcher.GradleMain.main(GradleMain.java:23)引起:   java.lang.RuntimeException:java.lang.NullPointerException at   com.sun.tools.javac.main.Main.compile(Main.java:469)at   com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:132)at at   org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:42)     在   org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:35)     在   org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.delegateAndHandleErrors(NormalizingJavaCompiler.java:97)     在   org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:50)     在   org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:36)     在   org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:34)     在   org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:25)     在   org.gradle.api.tasks.compile.JavaCompile.performCompilation(JavaCompile.java:158)     在   org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:138)     在   org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:92)     在org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)     在   org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory $ IncrementalTask​​Action.doExecute(AnnotationProcessingTaskFactory.java:235)     在   org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory $ StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:211)     在   org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory $ IncrementalTask​​Action.execute(AnnotationProcessingTaskFactory.java:222)     在   org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory $ StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:200)     在   org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)     在   org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)     ... 44更多引起:java.lang.NullPointerException at   dagger.internal.codegen.Util.getAnnotation(Util.java:192)at   dagger.internal.codegen.GraphAnalysisProcessor.process(GraphAnalysisProcessor.java:105)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:793)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment.access $ 200(JavacProcessingEnvironment.java:97)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment $ DiscoveredProcessors $ ProcessorStateIterator.runContributingProcs(JavacProcessingEnvironment.java:644)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment $ Round.run(JavacProcessingEnvironment.java:1027)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1185)     在   com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1108)     在   com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:824)     在com.sun.tools.javac.main.Main.compile(Main.java:439)... 62更多

是否添加-Werror不支持?

1 个答案:

答案 0 :(得分:6)

javac lint

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:all" << "-Werror"
        }
    }
}

结果将是下一个

....
:app:compileDebugJava                 
D:\Android\stack-overflow\ui\app\src\main\java\com\tivogi\ui\activity\MainActivity.java:22: warning: [deprecation] getLastNonConfigurationInstance() in Activity has been deprecated
        getLastNonConfigurationInstance();
        ^                             
error: warnings found and -Werror specified
1 error                               
1 warning                             
:app:compileDebugJava FAILED          

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

* Try:        
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED  

Total time: 2.421 secs

Android Lint

来自Gradle插件用户指南中的Android Lint support

android {
    lintOptions {
        ...
        // if true, treat all warnings as errors
        warningsAsErrors true
        ...
    }
}