如何在使用Android Gradle Plugin进行编译时添加java编译器选项?

如何在使用Android Gradle Plugin进行编译时添加java编译器选项?

问题描述:

我有一个带依赖项的 build.gradle 文件{classpath'com.android.tools.build:grad:0.13.3'} 申请插件:'com.android.application'

I have a build.gradle file with dependencies { classpath 'com.android.tools.build:gradle:0.13.3'} and apply plugin: 'com.android.application'.

当我这样做时调试构建我得到:

When I do a debug build I get:

gradle clean assembleDebug
:myapp:preBuild
(...)
:myapp:compileDebugJava
Note: C:\path\to\MyClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

:myapp:preDexDebug
(...)
:myapp:assembleDebug

BUILD SUCCESSFUL

如何将 -Xlint:unchecked 添加到基础任务? 有关Java编译选项的Gradle插件用户指南是无益的。

How can I add the -Xlint:unchecked to the underlying task? Gradle Plugin User Guide on Java compilation options is unhelpful.

我找到了基于 Gradle Plugin操作任务用户指南 Gradle关于JavaCompile的DSL文档

添加到 build.gradle

preBuild {
    doFirst {
        JavaCompile jc = android.applicationVariants.find { it.name == 'debug' }.javaCompile
        jc.options.compilerArgs = ["-Xlint:unchecked"]
    }
}

应用程序变体 null 在Gradle的配置阶段,所需的 JavaCompile 任务也不存在,因此我在执行阶段进行修改。

The application variants are null during Gradle's configuration phase and the required JavaCompile task also doesn't exist, thus I do the modification in the execution phase instead.