Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

Get started

Learn how to get your development environment ready to work with the Data Binding Library, including support for data binding code in Android Studio.

The Data Binding Library offers both flexibility and broad compatibility—it's a support library, so you can use it with devices running Android 4.0 (API level 14) or higher.

It's recommended to use the latest Android Plugin for Gradle in your project. However, data binding is supported on version 1.5.0 and higher. For more information, see how to update the Android Plugin for Gradle.

Build environment

To get started with data binding, download the library from the Support Repository in the Android SDK manager. For more information, see Update the IDE and SDK Tools.

To configure your app to use data binding, add the dataBinding element to your build.gradle file in the app module, as shown in the following example:

android {
    ...
    dataBinding {
        enabled = true
    }
}

Android Studio support for data binding

Android Studio supports many of the editing features for data binding code. For example, it supports the following features for data binding expressions:

  • Syntax highlighting
  • Flagging of expression language syntax errors
  • XML code completion

The Preview pane in Layout Editor displays the default value of data binding expressions, if provided. For example, the Preview pane displays the my_default value on the TextView widget declared in the following example:

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{user.firstName, default=my_default}"/>

If you need to display a default value only during the design phase of your project, you can use tools attributes instead of default expression values, as described in Tools Attributes Reference.

New data binding compiler for binding classes

The Android Gradle plugin version 3.1.0-alpha06 includes a new data binding compiler that generates the binding classes. The new compiler incrementally creates the binding classes, which in most cases speeds up the build process. To learn more about the binding classes, see Generated binding classes.

Previous versions of the data binding compiler generated the binding classes in the same step that compiles your managed code. If your managed code fails to compile, you might get multiple errors reporting that the binding classes aren't found. The new data binding compiler prevents these errors by generating the binding classes before the managed compiler builds your app.

To enable the new data binding compiler, add the following option to your gradle.properties file:

android.databinding.enableV2=true

You can also enable the new compiler in your gradle command by adding the following parameter:

-Pandroid.databinding.enableV2=true

The following behavior changes apply when you enable the new data binding compiler:

  • The Android Gradle plugin generates binding classes for your layouts before compiling your managed code.
  • If a layout is included in more than one target resource configuration, the data binding library uses android.view.View as the default view type for all views that share the same resource id but not view type.
  • Binding classes for library modules are compiled and packaged into the corresponding Android Archive (AAR) file. App modules that depend on those library modules no longer need to regenerate the binding classes. For more information about AAR files, see Create an Android Library.
  • A module’s binding adapters can no longer change the behavior of adapters of the module’s dependencies. Binding adapters only affect code in its own module and consumers of the module.