Search code examples
androidkotlinmvvmandroid-recyclerviewandroid-databinding

Databinding in recyclerview using kotlin causing issue


Here is my app-level gradle file

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'
android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.app.myapplication"
        minSdkVersion 19
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        dataBinding  = true
    }
}

kapt {
    generateStubs = true
    correctErrorTypes = true
}

sourceSets {
    main {
        java {
            srcDir "${buildDir}/generated/source/kapt/main"
        }
    }
    test {
        java {
            srcDir 'src/test/java'
        }
    }
}
dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    kapt "com.android.databinding:compiler:3.2.0-alpha10"
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.8.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
    //glide
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    kapt 'com.github.bumptech.glide:compiler:4.11.0'
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    implementation 'android.arch.paging:runtime:1.0.1'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

this is my main gradle file

buildscript {
    ext.kotlin_version = "1.4.10"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.3"
        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
    }
}

I am trying to implement databinding in recyclerview using kotlin. But it is giving me runtime error

Execution failed for task ':app:kaptDebugKotlin'.

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException (no error message)

Anyone has faced the same error? I have searched and gone through many links related to this error but all states the use of room library which I am not using and still getting this error. Any help is much appreicated. Thanks


Solution

  • I solved this error after updating my Android studio to

    Android Studio Arctic Fox 2020.3.1 Patch 3

    and also updated

    gradle version to 7.0.3

    and

    kotlin plugin update to 1.5.20.

    Basically I updated all my resources and the error was solved.

    Updated gradle files

    app-level gradle

    plugins {
        id 'com.android.application'
        id 'kotlin-android'
    }
    
    android {
        compileSdkVersion 31
        buildToolsVersion "30.0.3"
    
        defaultConfig {
            applicationId "com.app.navigationdemoapp"
            minSdkVersion 19
            targetSdkVersion 31
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
    
    dependencies {
    
        implementation "androidx.navigation:navigation-fragment-ktx:2.4.0-beta02"
        implementation "androidx.navigation:navigation-ui-ktx:2.4.0-beta02"
        implementation 'androidx.core:core-ktx:1.7.0'
        implementation 'androidx.appcompat:appcompat:1.4.0'
        implementation 'com.google.android.material:material:1.4.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        testImplementation 'junit:junit:4.13.2'
        androidTestImplementation 'androidx.test.ext:junit:1.1.3'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    }
    

    project level gradle

    buildscript {
        ext.kotlin_version = "1.5.20"
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath "com.android.tools.build:gradle:7.0.3"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
         
        }
    }
    
    allprojects {
        repositories {
            google()
            mavenCentral()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }