在Android Studio中用-Xlint重新编译
from--https://stackoverflow.com/questions/47740812/recompile-with-xlint-in-android-studio

 

 
已查看 39k次

102

当我在Android Studio中构建Android项目时,会收到消息:

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

我想按照消息的建议去做,但是如何做? 如何配置Android工作室以重新编译我的项目 -Xlint 如上述消息所建议? (我正在使用Android Studio 3.0.1 )

6个答案

                         最高分(默认值)                                              趋势(最近投票数更多)                                              修改日期(最新第一)                                              创建日期(最早的第一个)                     

174

该消息建议您使用args -Xlint重新编译以获取更多警告详细信息,并将这些代码添加到build.gradle:

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

然后,您可以通过详细消息修复警告。
例如,您可以用新方法(替换弃用的方法,因为旧方法已被弃用),所以总是有新方法 。

但是,有时出于某些原因我们不想更改代码,我们只是想摆脱编译警告,您可以添加 @SuppressWarnings("deprecation") 在弃用方法之前。

12

您需要在应用程序级别中添加以下内容 布拉德 文件

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

如果由于某些原因需要继续使用不推荐使用的API,则可以取消警告。您可以使用以下方法注释弃用方法:

@SuppressWarnings("deprecation")

注释。

帖子链接

6

如果您在生成签名的apk时遇到问题,可以尝试在 build.gradle(app)

android {
    lintOptions {
        checkReleaseBuilds false
    }
}

3

这是项目中的一些错误,可能是来自XML文件。禁用lintOptions不是正确的解决方案。找到错误并解决问题,以在Android Studio Terminal中的命令下运行

Windows

gradlew sembleDebug-stacktrace

MAC

./gradlew sembleDebug –stacktrace

0

You can manually run configured lint and other IDE inspections by selecting Analyze > Inspect Code. The results of the inspection appear in the Inspection Results window. see details here: https://developer.android.com/studio/write/lint#manuallyRunInspections

0

The message suggests to recompile with -Xlint flag in command-line, to get more issue details, but even if you do, the next log may ask for --stacktrace flag.

In build.gradle file, do something like:

import org.gradle.api.logging.configuration.ShowStacktrace
allprojects {
    // Same as passing --stacktrace option.
    gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS

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

Note that the import may not be required (just imported to be clear).

Also as y4n9b0 said; Then you can use warnings’ details to fix issues.
For example, you can replace deprecated method with new method (I mean, there must be a new-method, since old-method has been deprecated).

But sometimes for backward compatibility, we don’t want change our code, and just want to get rid of compile-time warnings; simply add in front of the deprecated method:

@SuppressWarnings("deprecation")

上一篇
下一篇