Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataBinding not working after Upgrade Android Studio 2.3

DataBinding worked very well in my project, But after upgrade Android Studio 2.3 today . Run 'app' failed because following error :

Error:(15, 40) Error: package com.javan.myrecorder.databinding not exist.
import com.javan.myrecorder.databinding.FragmentEventsBinding;
:app:compileMockDebugJavaWithJavac FAILED

I just upgrade android studio and didn't change anything. all plugin is latest! Now my question is, why occurs this error and how could I solve it? any help is welcome!

English is not my mother tongue; please excuse any errors on my part.


EDIT1

Like android project googlesamples/android-architecture

  • git checkout todo-databinding
  • and then run ./gradlew assembleDebug to build, build failed because of following error:

complete log of build


EDIT2 I have fixed this problem by following Data Binding broke after upgrade to Gradle 2.3.

in build.gradle(app) add

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

..balabala

dependencies {
    apt 'com.android.databinding:compiler:2.3.0'
}

some file in my project:

gradle-wrapper.properties

#Mon Mar 06 10:59:04 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

@petrnohejl @George Mount @Sa-Zad Prasla, Thanks!

like image 402
Javan Avatar asked Mar 03 '17 16:03

Javan


People also ask

Is databinding better than findViewById method?

Databinding is really faster compared to findViewById and setText . Not only performance, It is also much faster and maintainable for mid, full-scale projects.

What is the difference between databinding and view binding?

View binding and data binding both generate binding classes that you can use to reference views directly. However, view binding is intended to handle simpler use cases and provides the following benefits over data binding: Faster compilation: View binding requires no annotation processing, so compile times are faster.

Is data binding necessary?

Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.


1 Answers

android-apt and hence using apt has been deprecated since Android Studio 2.2.
Following the android-apt migration guide, instead add the following to your build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:2.3.0' // use same gradle version!
    annotationProcessor 'com.android.databinding:compiler:2.3.0'
}

If you are using Kolin, instead use:

apply plugin: 'kotlin-kapt'

dependencies {
    classpath 'com.android.tools.build:gradle:2.3.0' // use same gradle version!
    kapt 'com.android.databinding:compiler:2.3.0'
}
like image 50
Josh Bowden Avatar answered Sep 20 '22 18:09

Josh Bowden