使用Xlint:与android弃用

问题描述 投票:13回答:5

所以当我编译我的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.

如何使用此选项重新编译?我是否必须在build.xml中编辑某些内容?

java android build deprecated
5个回答
11
投票

是的,根据build.xml文件中的以下语句,如果你想......

         - Customize only one target:
             - copy/paste the target into this file, *before* the
               <setup/> task.
             - customize it to your needs.

这意味着:

  1. 转到$ ANDROID_SDK_ROOT / tools / ant / main_rules.xml文件并复制“compile”目标。
  2. 在<setup />任务之前将其粘贴到build.xml文件中。
  3. 然后将以下元素添加到任务中: <compilerarg value="-Xlint:deprecation"/>
  4. 同样,您可以添加其他编译器选项,例如用于未选中的操作: <compilerarg value="-Xlint:unchecked"/>

23
投票

也可以在Ant命令行上定义这些属性,避免编辑:

ant "-Djava.compilerargs=-Xlint:unchecked -Xlint:deprecation" debug

要启用所有Lint警告:

ant -Djava.compilerargs=-Xlint debug


14
投票

更简单,无需复制完整的javac目标:将以下行放在ant.properties文件中:

java.compilerargs=-Xlint:unchecked

这样,它就会覆盖Android SDK默认构建配置中的java.compilerargs。 (你可以自己检查一下它默认是空的,顺便说一下)。如果没有通知您的项目,SDK更新可能会更改默认的javac目标。

只是一个更细粒度的方法! :)


2
投票

看起来你应该能够在项目文件夹的根目录中的build.propertiesant.properties中指定选项。我尝试过这个似乎没有用。我想避免编辑我的build.xml文件,因为如果你需要更新项目,这会增加复杂性。但是,我无法找到解决办法。然而,我补充说:而不是复制整个compile目标:

<property name="java.compilerargs" value="-Xlint:unchecked" />

就在文件底部的import行之前。


0
投票

如果你想拥有一个好的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.
      ]
    }
  }
}

要么

subprojects {
  gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
      options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    }
  }
}

如果你只想添加一个选项(通常会添加更多),在任务JavaCompile中你只需要添加:

options.compilerArgs << "-Xlint:unchecked"

这是2018年,您可以依靠Gradle进行设置。我只添加了两个编译器参数选项,但还有更多。你可以找到更多信息herehere

© www.soinside.com 2019 - 2024. All rights reserved.