4

I am using Kotlin and trying to use new Data Binding Library, but I get unresolved refernce error for xxxBinding classes.

Top level build.gradle

buildscript {
    ext.kotlin_version = '1.2.30'
    ext.gradle_version = '3.1.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:3.2.0'

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


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

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

App build.gradle

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

android {
    buildToolsVersion "27.0.3"
    compileSdkVersion 27
    defaultConfig {
        applicationId "ru.vstu.clustermonitor"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

repositories {
    maven {
        url 'https://dl.bintray.com/terrakok/terramaven/'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:support-vector-drawable:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'

    implementation 'com.android.support:recyclerview-v7:27.1.1'                 // RecycleView
    implementation 'com.android.support:cardview-v7:27.1.1'                     // CardView

    implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha01'  // Navigation Architecture Components (remove)

    kapt "com.android.databinding:compiler:$gradle_version"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

MainActivity.onCreate

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)        
    val binding : ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
}

ActivityMainBinding should be generated, but It didn't. Is there any way to fix this problem?

2 Answers 2

6

The problem was in using ConstrainLayout instead of layout.

When using Data Binding, root layout must be "layout" type

Do

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="MyVariable"
            type="com.mycompany.myproject.MyType"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Views.Fragments.QueueFragment"
        android:background="#fff">

        ...

    </android.support.constraint.ConstraintLayout>
</layout>

Don't

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <data>
        <variable
            name="MyVariable"
            type="com.mycompany.myproject.MyType"/>
    </data>

    ...        
</layout>
Sign up to request clarification or add additional context in comments.

1 Comment

I think it's a typo. android.support.constraint.ConstraintLayout is the open tag.
-1

There is no need to generate bindings while using 'kotlin-android-extensions'. It was needed when we used android data binding. Now you can simply access the views directly by using their view_id from your fragment/Activity

2 Comments

Data binding allows to use Observable and just change data instead direct manipulation with controls, like in WPF
Kotlin synthetics from android-extensions suck. They cause way too many runtime crashes due to namespace collisions and unresolved refs. DataBinding or ViewBinding is superior

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.