gpt4 book ai didi

android - 使用数据绑定(bind)生成的代码使用的布局被 lint 错误地报告为 UnusedResource

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:51 25 4
gpt4 key购买 nike

Lint 正在将使用 Data Binding 生成的类使用的布局视为未使用,因此触发相应的 UnusedResource 警告。

例如:

../../src/main/res/layout/activity_start.xml:2:资源 R.layout.activity_start 似乎未使用

尽管在使用数据绑定(bind)生成的类之一中有引用:

public static ActivityStartBinding inflate(android.view.LayoutInflater inflater, android.databinding.DataBindingComponent bindingComponent) {
return bind(inflater.inflate(com.tuenti.messenger.R.layout.activity_start, null, false), bindingComponent);
}

有没有办法让 lint 考虑那些生成的类以避免这些误报?

最佳答案

我一直在研究这个问题并找到了一个允许使用生成的类的解决方案。

首先,您必须将 checkGeneratedSources = true 添加到您的 lintOptions block 中。例如,在您的应用模块 build.gradle 中:

android {
lintOptions {
checkDependencies = true
checkGeneratedSources = true
}
}

Lint 现在应该包括生成的源文件,并且不会再给出误报。您可能会遇到生成的文件引发不同错误的其他问题,在我的例子中,一些与 Room 相关的生成文件违反了 RestrictedApi 规则。如果是这种情况,只需将规则添加到 lint.xml 以排除该规则在生成的代码上运行,如下所示:

<issue id="RestrictedApi" severity="error">
<ignore path="build" />
</issue>

关于android - 使用数据绑定(bind)生成的代码使用的布局被 lint 错误地报告为 UnusedResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40527624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com