使用 Android Gradle 插件编译时如何添加 java 编译器选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27274526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add java compiler options when compiling with Android Gradle Plugin?
提问by Konrad Jamrozik
I have a build.gradle
file with dependencies { classpath 'com.android.tools.build:gradle:0.13.3'}
and apply plugin: 'com.android.application'
.
我有一个build.gradle
带有dependencies { classpath 'com.android.tools.build:gradle:0.13.3'}
和的文件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
How can I add the -Xlint:unchecked
to the underlying task? Gradle Plugin User Guide on Java compilation optionsis unhelpful.
我怎样才能将 加入-Xlint:unchecked
到基础任务中?关于 Java 编译选项的 Gradle 插件用户指南没有帮助。
采纳答案by Konrad Jamrozik
I found the following solution based on Gradle Plugin User Guide on Manipulating Tasksand Gradle DSL doc about JavaCompile:
我根据Gradle Plugin User Guide on Manipulating Tasks和Gradle DSL doc about JavaCompile找到了以下解决方案:
Add to build.gradle
:
添加到build.gradle
:
preBuild {
doFirst {
JavaCompile jc = android.applicationVariants.find { it.name == 'debug' }.javaCompile
jc.options.compilerArgs = ["-Xlint:unchecked"]
}
}
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.
应用程序变体处于null
Gradle 的配置阶段,所需的JavaCompile
任务也不存在,因此我在执行阶段进行修改。
回答by Andrew
I tried the solution posed by @Konrad Jamrozik but it didn't work with my project due to flavours in my project.
我尝试了@Konrad Jamrozik 提出的解决方案,但由于我的项目中的风格,它不适用于我的项目。
Given that we're just turning on additional warnings, not something that's significantly changing how the compiler operates, I don't see it being an issue that it will be added to both release and debug builds. As such, this answer has a cleaner method that works with flavours: How to add -Xlint:unchecked to my Android Gradle based project?
鉴于我们只是打开了额外的警告,而不是显着改变编译器运行方式的东西,我认为它不会被添加到发布和调试版本中。因此,这个答案有一个更简洁的方法,适用于不同的口味:How to add -Xlint:unchecked to my Android Gradle based project?
In my case, adding this to the build.gradle file of the affected module:
就我而言,将其添加到受影响模块的 build.gradle 文件中:
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
回答by Yousha Aleayoub
Add this to build.gradlefile:
将此添加到build.gradle文件:
android { // <---
tasks.withType(JavaCompile) {
configure(options) {
options.encoding = 'UTF-8'
options.debug = true
options.failOnError = true
options.warnings = true
options.compilerArgs << '-Xlint:deprecation' << '-Xlint:unchecked'
}
}