使用 Xlint:在 Android 中弃用

发布于 2024-12-08 17:41:50 字数 304 浏览 1 评论 0 原文

因此,当我编译 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 中编辑某些内容吗?

So I almost always get some message like this when I'm compiling my android app:

[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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

朦胧时间 2024-12-15 17:41:50

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

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

要启用所有 Lint 警告:

ant - djava.compilerargs=-Xlint 调试

These properties can also be defined on the Ant command line, avoiding edits:

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

To enable all Lint warnings:

ant -Djava.compilerargs=-Xlint debug

夏日浅笑〃 2024-12-15 17:41:50

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

java.compilerargs=-Xlint:unchecked

这样,它只需覆盖 Android SDK 默认构建配置中的 java.compilerargs 即可。 (顺便说一句,您可以自己检查默认情况下它是空的)。 SDK 更新不会造成混乱,可能会在不通知您的项目的情况下更改默认 javac 目标。

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

Even simpler and without the need to copy a complete javac target: put the following line in ant.properties file :

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.

Just a more granular way to do! :)

樱桃奶球 2024-12-15 17:41:50

是的,根据 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 文件并复制“编译”目标。

  2. 将其粘贴到 build.xml 文件中的 之前。

  3. 然后将以下元素添加到任务中:

    
    
  4. 同样,您可以添加其他编译器选项,例如未检查的操作:

    
    

Yes, per the statement below in the build.xml file, if you want to ...

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

That means:

  1. Go to the $ANDROID_SDK_ROOT/tools/ant/main_rules.xml file and copy the "compile" target.

  2. Paste it into your build.xml file before the <setup/> task.

  3. Then add the following element to the task:

    <compilerarg value="-Xlint:deprecation"/>
    
  4. Likewise, you can add other compiler options, such as for unchecked operations:

    <compilerarg value="-Xlint:unchecked"/>
    
天暗了我发光 2024-12-15 17:41:50

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

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

在文件底部的 import 行之前添加了:。

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:

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

just before the import line at the bottom of the file.

秋凉 2024-12-15 17:41:50

如果你想拥有一个良好的 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"

It's 2018,您可以依靠 Gradle 来设置它。我只添加了两个编译器参数选项,但还有更多。您可以在此处此处

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:

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:

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 here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文