5

I am using Android Studio 3.0 Canary 6 version. I've enabled dataBinding in my class and the code doesn't show any error. But, when I build an APK, the build fails and shows the following error:

Error:(8, 37) Unresolved reference: databinding  
Error:(22, 26) Unresolved reference: ActivityMainBinding  
Error:(38, 50) Unresolved reference: ActivityMainBinding  
Error:(43, 52) Unresolved reference: ActivityMainBinding  
Error:(46, 52) Unresolved reference: ActivityMainBinding  
Error:(49, 52) Unresolved reference: ActivityMainBinding  
Error:(52, 52) Unresolved reference: ActivityMainBinding  
Error:(55, 52) Unresolved reference: ActivityMainBinding  
Error:(58, 52) Unresolved reference: ActivityMainBinding  
Error:(61, 52) Unresolved reference: ActivityMainBinding  
Error:(64, 52) Unresolved reference: ActivityMainBinding  
Error:(67, 52) Unresolved reference: ActivityMainBinding  
Error:(70, 52) Unresolved reference: ActivityMainBinding  
Error:(73, 52) Unresolved reference: ActivityMainBinding  
Error:Execution failed for task ':app:compileDebugKotlin'.  
Compilation error. See log for more details

The top level build.gradle file is as follows :

buildscript {
ext.kotlin_version = '1.1.3-2'
repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0-alpha6'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

The module build.gradle file is as follows :

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.jimil.calculator"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner 
            "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 
                'proguard-rules.pro'
        }
    }
    dataBinding{
        enabled =  true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation ('com.android.support.test.espresso:espresso-
        core:3.0.1', {
            exclude group: 'com.android.support', module: 'support-
                annotations'
    })
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    kapt 'com.android.databinding:compiler:3.0.0-alpha6'
}

kapt {
    generateStubs = true
}

The MainActivity.kt class is :

import com.example.jimil.calculator.databinding.ActivityMainBinding  
...  
class MainActivity : AppCompatActivity() {  
    private var binding: ActivityMainBinding? = null  
    ...  
}

Please help me solve the build error.

6 Answers 6

9

There are several errors in your gradle.

The current Gradle-Version is 3.0.0-beta 3. That means that you need to change your classpath to

classpath 'com.android.tools.build:gradle:3.0.0-beta6'

Same for your dependencies

kapt "com.android.databinding:compiler:3.0.0-beta6"

You should also provide your plugins in the proper folder.

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

You should also enable compile options to have your annotation processors use Java8-dependencies (which may be required for Kotlin to target 1.8).

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

After this is done invalidate your cache and restart.

If the error still persists check the Gradle Console. There may be an error in your XML File which stops your Annotation processor to generate the databinding classes.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried changing from alpha6 to beta6, but then, my build.gradle file won't sync with my project and so, I've added the compileOptions, but I went back to alpha6 instead of beta6, and then I'm back to the same problem; it won't allow me to build the apk showing the error : Unresolved reference databinding and Unresolved reference : ActivityMainBinding
Can you please tell me how the XML file should be? Or what possible code and in which particular XML file stops generating of databinding classes
you havent posted your XML. Do that and i may help :)
The site isn't allowing me to paste the XML file in the comments. Can I get an email-ID of yours so that I can show you the XML file?
In my case I removed the DataBindingUtil.setContentView(this, R.layout.activity_main). Run the app with old setContentView call without using DataBindingUtil. Then I add again binding = DataBindingUtil.setContentView(this, R.layout.activity_main). Now it was able to resolve private lateinit var binding: ActivityMainBinding
3

In my case, cleaning and then rebuilding the project solved the problem. The problem was that the necessary classes haven't been built until then.

Once data binding is integrated in layout file, goto Build -> Clean Project and Build -> Rebuild Project. This will generate necessary binding classes.

source: link

1 Comment

Rebuilding also cleans the project.
2

I had this issue (Unresolved reference: ActivityMainBinding ) Tweaking dependencies did not work. The issue was I was using a DrawerLayout instead of layout as the root element of the activity layout which I want to generate the data binding for.

So place your other layout(ex: constraint, linear relative layouts) inside following tag

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">

<!-- other layout here -->

</layout>

source: https://stackoverflow.com/a/50634021/7356355

Comments

2

Another thing that can trigger this error is improper use of fragments. For example, in my case, I had

<layout>
  <data></data>
  <fragment></fragment>
</layout>

When I should have nested the fragment under another view:

<layout>
  <data></data>
  <LinearLayout>
    <fragment></fragment>
  </LinearLayout>
</layout>

That change fixed it for me. I'm guessing the implication is that any error in your xml layout file that doesn't directly trigger an error on its own may manifest itself as the error presented in this question.

Comments

0

I forgot to add kotlin-kapt. After adding it, it worked!

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'


dependencies {
    kapt "com.android.databinding:compiler:3.1.3"
}

Comments

0

In my case my project was too nested inside other folders in my computer. Move it straight to the desktop, avoid long name folders and try again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.