Alen
Alen

Reputation: 979

Kotlin-DataBinding-Error: Check your module classpath for missing or conflicting dependencies

How can I solve this problem. I can't make this data binding work, I have tried everything. Build.gradle(module app)

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

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.fusion.alen.ask"
        minSdkVersion 16
        targetSdkVersion 27
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    androidExtensions {
        experimental = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }

}

dependencies {
    implementation 'com.android.support:multidex:1.0.3'

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:animated-vector-drawable:27.1.1'
    implementation 'com.android.support:support-vector-drawable:27.1.1'
    implementation 'com.android.support:customtabs:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:recyclerview-v7:27.1.1'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android-extensions'
repositories {
    mavenCentral()
}

I tried adding kapt "com.android.databinding:compiler:$compiler_version" but it gives me an error. Build.gradle(Module project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext{
        kotlin_version = '1.2.71'
        compiler_version = '3.2.1'
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:4.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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

This is my profile_fragment.xml, I have followed documentation instructions

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

    <data>
        <variable
            name="user"
            type="com.alen.ask.UserModel.User" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteY="25dp">


        <LinearLayout
            android:id="@+id/profile_box"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@color/primaryColor"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/app_bar">

            <LinearLayout
                android:id="@+id/qff_box"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5"
                android:orientation="horizontal"
                android:baselineAligned="false">


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/questions_number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        **android:text="@{user.questionsNum}"************
                        tools:text="0" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/followers_number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        **android:text="@{user.followersNum}"************
                        tools:text="0" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/following_number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        **android:text="@{user.followingNum}"***********
                        tools:text="0" />

                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/app_bar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/primaryDarkColor"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/username_text"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_weight="1"
                **android:text="@{user.name}"**
                android:textColor="@color/primaryTextColor"
                android:textSize="18sp"
                android:layout_marginStart="8dp" />
        </LinearLayout>

    </android.support.constraint.ConstraintLayout>

</layout>

My User data class:

data class User(val name: String,
                val email: String,
                val uid: String,
                val photo: String,
                val postsList: ArrayList<String> = ArrayList(),
                val questionsNum: Int = 0,
                val followersNum: Int = 0,
                val followingNum: Int = 0
                ) 

This is inside ProfileFragmen class, in onCreateView method

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val binding = ProfileFragmentBinding.inflate(inflater, container, false)
        binding.user = user
}

That line binding.user = user gives me error: Cannot access class 'UserModel.User'. Check your module classpath for missing or conflicting dependencies How can I solve this, I have followed instructions from DataBinding documentation and I don't think I have made any mistake. I have tried cleaning and rebuilding the project, but it didn't work. I have tried adding those 'kapt' dependencies, didn't work.... File->ProjectStructure->Gradle version: 4.10.1, AndroidPluginVersion: 3.2.1, KotlinLanguageVersion: 1.2, KotlinPluginVersion: 1.2.71-release-Studio3.2-1

Thank you.

Upvotes: 24

Views: 48216

Answers (9)

bighugedev
bighugedev

Reputation: 1222

This happened to me when I tried referring an included element of a layout via view binding, whilst the included element itself referred to a layout named "content_main". Until I cleaned the project and then invalidated the caches, the problem had persisted.

Upvotes: 0

jenos kon
jenos kon

Reputation: 564

If someone still has this problem related to safe args just add this to your build.gradle

android {
...
sourceSets {
    main {
        kotlin {
            srcDirs += 'build/generated/source/navigation-args'
        }
    }
}
...
}

it works for me

Upvotes: 0

Michał Dobi Dobrzański
Michał Dobi Dobrzański

Reputation: 2000

Solution for multi-module Gradle projects if you build more than one app from the codebase:

The ViewBinding and DataBinding libraries have problems with its internal generated source sets while using many Gradle modules.

Issue:

You have 2 XML layout files which are named the same (like content_main.xml). Both are present in the :first-app module and the :second-app one in layout dirs. The IDE will render you red errors.

But, the app will compile and launch properly.

Solution:

Distinguish names for XML files - so the bindings will be generated as: FirstAppContentMainBinding (first_app_content_main.xml) and SecondAppContentMainBinding (second_app_content_main.xml)

Upvotes: 5

Pouya Danesh
Pouya Danesh

Reputation: 1627

you are missing 'fusion' in your variable tag, although you have it in your applicationId in gradle.

Upvotes: 0

Peter Gichia
Peter Gichia

Reputation: 261

Had the same issue, fixed it by changing build features in the build.gradle module from viewBinding to dataBinding

buildFeatures {
    dataBinding = true
}

Upvotes: 14

AndroidDev
AndroidDev

Reputation: 1625

The reason is because one of your package name begins with Capital.

The class name which uses databinding should begin with capital, and package name should start with lower case. [This is not just for databinding, this is the convention followed commonly]

[Internally when dataBinding generates bindingImplementation for your xml, it parses your package name to identify the class, if the package name begins with capital it is not able to separate the Class name from the package name, and it throws an error.]

Upvotes: 24

Zach Sao
Zach Sao

Reputation: 446

Rename your folder UserModel to userModel then clean/rebuild your project. I had the same problem and the error disapeared when set my folder's first letter to lower case. In the future use camelCase for your folders name just like for variables. Upper case on the first letter is usually for classes.

Upvotes: 31

Abraham Mathew
Abraham Mathew

Reputation: 2146

<data class="ProfileBinding">
    <variable
        name="user"
        type="com.alen.ask.UserModel.User" />
</data>

use the above mentioned ProfileBinding class inside Fragment class to inflate

Upvotes: 0

LiTTle
LiTTle

Reputation: 1881

At file profile_fragment.xml try to replace this

<data>
    <variable
        name="user"
        type="com.alen.ask.UserModel.User" />
</data>

with this

<data>
    <variable
        name="user"
        type="com.alen.ask.User" />
</data>

and give us a feedback.

Upvotes: 0

Related Questions