Configuring Android Data Binding Just Got Easier

The Android Data Binding Guide was recently updated with a new way to configure Data Binding in Gradle. With this update, including Data Binding just got a little simpler. But there are a few things you should know.

Android Fundamentals: Data Binding

A soup-to-nuts exploration of the Android Data Binding library.

In the Data Binding Guide, you just add the following to the application build.gradle file:

// app/build.gradle

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

In order to make this work, you need to update the Android gradle plugin to at least 1.5.0-alpha1. Unfortunately, there is no 1.5.0-alpha1 available on jCenter. Use 1.5.0-beta1 or later. If you want to see which versions are available, look in the jCenter website. The gradle plugin version are located at http://jcenter.bintray.com/com/android/tools/build/gradle/. The latest version at the time of this post is 1.5.0-beta2. Given that, our base-level build.gradle file will look like the following.

// build.gradle

buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:1.5.0-beta2"
}
}

allprojects {
repositories {
jcenter()
}
}

The new dataBinding closure has three configuration options available.

The first option is enabled. as we saw above.

The second option is version, where you are able to specify which data-binding version you want to build against. If you have not specified the version, the build processor will download and utilize the latest. But, if you need to be strict about your versioning, you can define that specific version you need.

The final option is addDefaultAdapters. This option is enabled by default. If you want to disable those default adapters, you can set the “addDefaultAdapters” to false.

// app/build.gradle

android {
...
dataBinding{
enabled = true
version = "1.0-rc5"
addDefaultAdapters = true
}
}

For those who are using the android-apt plugin, make sure you are using version 1.7 or later.

// build.gradle

buildscript{
...
dependencies {
classpath "com.android.tools.build:gradle:1.5.0-beta2"
classpath "com.neenbedankt.gradle.plugins:android-apt:1.7"
}
}

I’m excited about the change. This makes Data Binding feel much more like a built-in piece of the Android ecosystem.

comments powered by Disqus