java 使用 Xlint:deprecation 与 android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7682150/
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
Use Xlint:deprecation with android
提问by Kurtis Nusbaum
So I almost always get some message like this when I'm compiling my android app:
所以当我编译我的 android 应用程序时,我几乎总是收到这样的消息:
[javac] Note: /home/kurtis/sandbox/udj/androidApp/src/org/klnusbaum/udj/PlaylistFragment.java uses or overrides a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
How do I recompile with this option? Do I have to edit something in my build.xml?
如何使用此选项重新编译?我是否必须在 build.xml 中编辑某些内容?
采纳答案by SPitBalls.com
Yes, per the statement below in the build.xml file, if you want to ...
是的,根据 build.xml 文件中的以下语句,如果您想...
- Customize only one target: - copy/paste the target into this file, *before* the <setup/> task. - customize it to your needs.
That means:
这意味着:
Go to the $ANDROID_SDK_ROOT/tools/ant/main_rules.xml file and copy the "compile" target.
Paste it into your build.xml file beforethe <setup/> task.
Then add the following element to the task:
<compilerarg value="-Xlint:deprecation"/>
Likewise, you can add other compiler options, such as for unchecked operations:
<compilerarg value="-Xlint:unchecked"/>
转到 $ANDROID_SDK_ROOT/tools/ant/main_rules.xml 文件并复制“编译”目标。
在 <setup/> 任务之前将其粘贴到 build.xml 文件中。
然后将以下元素添加到任务中:
<compilerarg value="-Xlint:deprecation"/>
同样,您可以添加其他编译器选项,例如未检查的操作:
<compilerarg value="-Xlint:unchecked"/>
回答by Joe Bowbeer
These properties can also be defined on the Ant command line, avoiding edits:
这些属性也可以在 Ant 命令行上定义,避免编辑:
ant "-Djava.compilerargs=-Xlint:unchecked -Xlint:deprecation" debug
ant "-Djava.compilerargs=-Xlint:unchecked -Xlint:deprecation" debug
To enable all Lint warnings:
要启用所有 Lint 警告:
ant -Djava.compilerargs=-Xlint debug
ant -Djava.compilerargs=-Xlint debug
回答by Friedryk
Even simpler and without the need to copy a complete javac target: put the following line in ant.properties file :
更简单,无需复制完整的 javac 目标:将以下行放入 ant.properties 文件中:
java.compilerargs=-Xlint:unchecked
This way, it just overrides java.compilerargs from Android SDK default build configuration. (You can check for yourself that it is empty by default, btw). No mess with SDK update that could change the default javac target without your project being notified.
这样,它只是覆盖了 Android SDK 默认构建配置中的 java.compilerargs。(你可以自己检查默认情况下它是空的,顺便说一句)。没有任何 SDK 更新会在没有通知您的项目的情况下更改默认 javac 目标。
Just a more granular way to do! :)
只是一种更精细的方法!:)
回答by sk8geek
It looks like you should just be able to specify the option in either build.properties
or ant.properties
in the root of your project folder. I tried this and it didn't seem to work.
I wanted to avoid editing my build.xml
file as this adds complication later if you need to update your project. However, I was unable to find a way around it. However, rather than copying the whole compile
target I added:
看起来您应该能够在项目文件夹的任一目录build.properties
或ant.properties
根目录中指定该选项。我试过这个,它似乎没有用。我想避免编辑我的build.xml
文件,因为如果您需要更新项目,这会在以后增加复杂性。但是,我无法找到解决方法。但是,compile
我没有复制整个目标,而是添加了:
<property name="java.compilerargs" value="-Xlint:unchecked" />
just before the import
line at the bottom of the file.
就在import
文件底部的行之前。
回答by cesards
If you want to have a good CI + CD pipeline and you care about your code quality, a good option to show more information about lint complainings is by adding this to your top/root gradle.build:
如果你想拥有一个良好的 CI + CD 管道并且你关心你的代码质量,一个显示关于 lint 抱怨的更多信息的好选择是将它添加到你的 top/root gradle.build 中:
subprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs += [
'-Xlint:unchecked', // Shows information about unchecked or unsafe operations.
'-Xlint:deprecation', // Shows information about deprecated members.
]
}
}
}
or
或者
subprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
If you only want to add one option (you would normally add more), inside the task JavaCompile
you just need to add:
如果您只想添加一个选项(您通常会添加更多),则JavaCompile
只需在任务中添加:
options.compilerArgs << "-Xlint:unchecked"
It's 2018 and you can rely on Gradle to set this up. I only added two compiler argument options but there are much more. You can find more information hereand here.
现在是 2018 年,您可以依靠 Gradle 进行设置。我只添加了两个编译器参数选项,但还有更多。您可以在此处和此处找到更多信息。