秃德666
秃德666

Reputation: 405

Android Studio Warn about application namespace

When I compile a project in which I use BindingAdapter, the Android Studio always show lots of warning and jump into the source file which I write code of BindingAdapter.How can I solve it, I never want to show it when I compile and also I don't want to remove the namespace app or other, can anyone help me. Thank you!!!
The Warning is below: The Warning
The example source code: The example code

Upvotes: 1

Views: 1065

Answers (1)

Lorne Laliberte
Lorne Laliberte

Reputation: 6311

As @Michael Spitsin pointed out in the comment, just remove the "app:" namespace from the annotation. You can keep the "app:" namespace in your layout XML. The namespace will be removed internally unless it is android:; all other namespaces are treated the same.

The warning serves to inform you that the namespace does not have any effect on the annotation. So, for example, you cannot have different methods for @BindingAdapter("app:src") and @BindingAdapter("foo:src") -- the namespace is removed before it's used as a key in the implementation of that annotation. The only exception is the android namespace; you can have @BindingAdapter("android:src") and also @BindingAdapter("app:src") as well.

If you look through the source code that implements the @BindingAdapter annotation, you'll see that the namespace is removed, which is the reason for the warning. For example, in this excerpt from android.databinding.tool.store.SetterStore.addBindingAdapter:

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

...you can see that the namespace is stripped, like so:

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

This is done because that the binding adapter needs to work across any and all source files in the project, regardless of what string was used for the namespace in a particular XML file.

Upvotes: 1

Related Questions