Marcel50506 Marcel50506 - 14 days ago 9x
Android Question

Android Databinding: "Method references using '.' is deprecated"

When using databinding in my app, I get the following warning when compiling:

Warning:Method references using '.' is deprecated. Instead of 'handler.onItemClick', use 'handler::onItemClick'

Please see my XML below.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android=""
<variable name="handler" type="ClickHandler"/>
<variable name="active" type="boolean"/>

android:onClick="@{!active ? handler.onItemClick : null}"

Please note the : from the conditional statement

Pretty straightforward message, until I change the '.' to '::'.

android:onClick="@{!active ? handler::onItemClick : null}"

Since the onItemClick is inside a conditional statement, it seems to interpret the first of the two ::'s as the 'else' statement of the condition. On the second ':', I get the error:

<expr> expected, got ':'

EDIT: As @CommonsWare suggested in the comments, inverting the statement to
"@{active ? null : handler::onItemClick}"

doesn't help either, a similar error is shown (see comments)

EDIT2: Apparently, when stripping the conditional statement away, being left with
, it still gives an error:
'!=', '%', '*', '+', ',', '-', '.', '/', <, <<, <=, '==', '>', '>=', '>>', '>>>' or '[' expected, got ':'
Using the dot-notation, still gives a warning when compiling

Is there any way to escape these ::'s, so it is interpreted correctly?


My guess is that the deprecation warning is shown because of Android Data Binding is currently not being fully compatible with Java 8. Putting the following into your project's build.gradle file should hide mentioned warning.

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7

Unless you are using Java 8 features in your project.