【问题标题】:Android Studio Warn about application namespaceAndroid Studio 警告应用程序命名空间
【发布时间】:2017-01-30 15:12:43
【问题描述】:

当我编译一个使用BindingAdapter的项目时,Android Studio总是显示很多警告并跳转到我编写BindingAdapter代码的源文件。我该如何解决它,我不想显示当我编译时,我也不想删除命名空间应用程序或其他,任何人都可以帮助我。谢谢!!!
警告如下:
示例源代码:

【问题讨论】:

  • 根据this answer,您只需要删除“app:”命名空间。
  • 我以前知道这一点,但我不想删除这个应用程序命名空间。其他同事的android studio没有出现这种问题,请问有没有其他方法可以解决。

标签: android android-studio


【解决方案1】:

正如@Michael Spitsin 在评论中指出的那样,只需从注释中删除“app:”命名空间即可。您可以在布局 XML 中保留“app:”命名空间。命名空间将在内部被删除,除非它是android:;所有其他命名空间都被同等对待。

警告用于通知您命名空间对注释没有任何影响。因此,例如,@BindingAdapter("app:src")@BindingAdapter("foo:src") 不能有不同的方法——命名空间在用作该注释的实现中的键之前被删除。唯一的例外是android 命名空间;你可以有@BindingAdapter("android:src"),也可以有@BindingAdapter("app:src")

如果您查看实现@BindingAdapter 注释的源代码,您会看到命名空间已被删除,这就是警告的原因。例如,在这段摘自android.databinding.tool.store.SetterStore.addBindingAdapter

public void addBindingAdapter(ProcessingEnvironment processingEnv, String attribute,
        ExecutableElement bindingMethod, boolean takesComponent) {
    attribute = stripNamespace(attribute);

...您可以看到命名空间被剥离,如下所示:

private static String stripNamespace(String attribute) {
    if (!attribute.startsWith("android:")) {
        int colon = attribute.indexOf(':');
        if (colon >= 0) {
            attribute = attribute.substring(colon + 1);
        }
    }
    return attribute;
}

这样做是因为绑定适配器需要跨项目中的所有源文件工作,而不管特定 XML 文件中的命名空间使用什么字符串。

【讨论】:

    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    相关资源
    最近更新 更多