如何在使用Android Gradle Plugin进行编译时添加java编译器选项? [英] How to add java compiler options when compiling with Android Gradle Plugin?

查看:211
本文介绍了如何在使用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.

这篇关于如何在使用Android Gradle Plugin进行编译时添加java编译器选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆