I recently updated Android Studio to 2.3, as well as gradle to 2.3.0. Since the last time my application was working properly, I've made no other changes, but now whenever executing this line of code:
ViewDataBinding viewDataBinding = DataBindingUtil.setContentView(this,R.layout.main_activity);
I get this error:
java.lang.NoClassDefFoundError: android.databinding.DataBinderMapper
at android.databinding.DataBindingUtil.<clinit>(DataBindingUtil.java:31)
at com.app.MainActivity.onCreate(MainActivitiy.java:108)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
According to this SO answer, gradle dependencies that are including support-v4 r21 is a problem. It turns out that my dependencies include that, but not because of a dependency that I added, but because I added:
dataBinding {
enabled = true
}
Because of this, I see that these dependencies are added to my project:
+--- com.android.databinding:library:1.3.1
| +--- com.android.support:support-v4:21.0.3 -> 24.0.0 (*)
| \--- com.android.databinding:baseLibrary:2.3.0-dev -> 2.3.0
+--- com.android.databinding:baseLibrary:2.3.0
\--- com.android.databinding:adapters:1.3.1
+--- com.android.databinding:library:1.3 -> 1.3.1 (*)
\--- com.android.databinding:baseLibrary:2.3.0-dev -> 2.3.0
And as you can see, com.android.databinding:library:1.3.1
includes support-v4:21.0.3
.
This is my full module build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
compileSdkVersion 23
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "com.app"
minSdkVersion 16
targetSdkVersion 23
versionCode 12
versionName "0.2.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
dataBinding {
enabled = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.2.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:support-v4:23.2.1'
compile 'com.android.support:recyclerview-v7:23.2.1'
compile 'com.android.volley:volley:1.0.0'
compile 'io.realm:android-adapters:1.3.0'
compile 'joda-time:joda-time:2.9.4'
compile 'com.android.volley:volley:1.0.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.google.android.gms:play-services-ads:9.8.0'
}
Any idea how I can fix this issue?