I'm having a lot of trouble adding databinding to my project. There is a complicated build.gradle. Unfortunately, I cannot post too much from it.
If I add databinding {enabled = true} to my android block in my app build.gradle file I get the following error -->

Error:Cannot change dependencies of configuration ':projectName:compile' after it has been resolved. Cannot get property 'javaCompile' on null object.

I've added the databinding library on my classpath. If I don't add the dataBinding {enabled = true} block the build succeeds with a warning that generated sources are in the wrong folder.

Any ideas?

1

There are 1 best solutions below

1
On

Here is my sample build.gradle with databinding enabled. I'm using this setup in my projects and databinding is working fine. Notice that you only need to specify databinding {enabled true}. No more setup is required. You don't have to add databinding to dependencies.

build.gradle from main folder

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
    }
}
subprojects {
    buildscript {
        repositories {
            jcenter()
            mavenCentral()
        }
    }
    repositories {
        jcenter()
        mavenCentral()       
    }
}

build.gradle from app

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "foo.bar"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    dataBinding {
        enabled = true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    //support library
    compile 'com.android.support:appcompat-v7:23.1.1'

}