leggo
leggo

Reputation: 917

Error:Execution failed for task ':app:kaptDebugKotlin'

I'm new to using Kotlin and trying to set it up with Dagger2, I've seen some few examples but none of them seem to work for me.

I keep getting this

Error:Execution failed for task ':app:kaptDebugKotlin'.

Internal compiler error. See log for more details

I have my build.gradle (Module: app)

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

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.exampleapp"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    kapt {
        generateStubs = true
    }
    dexOptions {
        javaMaxHeapSize "2048M"
    }
}

ext {
    supportLibVer = '25.0.0'
    daggerVer = '2.8'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Support lib
    compile "com.android.support:appcompat-v7:${supportLibVer}"

    kapt "com.google.dagger:dagger-compiler:${daggerVer}"
    compile "com.google.dagger:dagger:${daggerVer}"
    provided "javax.annotation:jsr250-api:${javaxVer}"

    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"


}
repositories {
    mavenCentral()
}

Upvotes: 90

Views: 171705

Answers (30)

developer_hatch
developer_hatch

Reputation: 16214

In my case I remove the whole git project folder and clone it again and invalidate caches and restart before in IntelliJ. That was the only way for me.

Upvotes: 0

Mohammad Reza
Mohammad Reza

Reputation: 95

I solved this problem by replacing kapt with annotationProcessor with one of the dependencies.

// dagger2
implementation 'com.google.dagger:dagger-android-support:2.41'
annotationProcessor 'com.google.dagger:dagger-compiler:2.41' //this one
kapt "com.google.dagger:dagger-android-processor:2.41"

Upvotes: 1

Gulab Sagevadiya
Gulab Sagevadiya

Reputation: 991

If You are using Hilt and Field Injection Then Remove Private From Field Injected Object this worked For me

@Inject
private lateinit var helper: Helper

to

@Inject
lateinit var helper: Helper

Upvotes: 6

Ali Najaphi
Ali Najaphi

Reputation: 151

remove this line :

implementation 'androidx.hilt:hilt-lifecycle-viewmodel:x.x.x'

Upvotes: 0

Sam
Sam

Reputation: 140

What worked for me was to add all of these dependencies:

    room_version = '2.4.3'

    // Room
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-rxjava2:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

Upvotes: 0

Canturk Karabulut
Canturk Karabulut

Reputation: 136

//implementation "androidx.room:room-runtime:2.4.2"
kapt "androidx.room:room-compiler:2.2.1"
//kapt "android.arch.persistence.room:compiler:2.4.2"
implementation "androidx.room:room-ktx:2.2.1"
//annotationProcessor "androidx.room:room-compiler:2.4.2"

I didn't use the ones I used. My problem was solved when I used these two. We get a kaptDebug error because there is no match with the Room. My problem was related to the implement.

Upvotes: 2

Marawan Mamdouh
Marawan Mamdouh

Reputation: 1015

In my case, I was using

kapt "com.github.bumptech.glide:compiler:$version_glide"

without applying the implementation.

implementation "com.github.bumptech.glide:glide:$version_glide"

You should check all kapt dependencies in the build.gradle (Module), not just glide

Upvotes: 0

Saif Jaradat
Saif Jaradat

Reputation: 171

Just Replace kept Keyword to annotationProcessor and everything works fine.

Upvotes: 8

Shubham Sharma
Shubham Sharma

Reputation: 41

In my case, i just updated the recently added dependencies(a newer version was available), and it worked for me.

Upvotes: 0

Kartik Setia
Kartik Setia

Reputation: 31

In some of the Cases if the id of the View is incorrect it always shows this error.Recheck if the id of the view is correct.

Upvotes: 0

Vinithius
Vinithius

Reputation: 129

I faced a similar problem. It was resolved when I changed this line:

ext.kotlin_version = "1.5.10"

to

ext.kotlin_version = "1.4.10"

Upvotes: 0

Quick learner
Quick learner

Reputation: 11467

For me I deleted these folders.

  1. .gradle
  2. .idea

Close android studio , delete the folders and reopen the project

enter image description here

Upvotes: 3

Wad Hannouch
Wad Hannouch

Reputation: 301

In my case I replaced this

implementation 'com.google.dagger:dagger:2.27'
kapt 'com.google.dagger:dagger-compiler:2.27'

by

implementation 'com.google.dagger:dagger:2.27'
annotationProcessor "com.google.dagger:dagger-compiler:2.27"

and solved the problem

Upvotes: 30

Yıldırım
Yıldırım

Reputation: 787

if all that tasks are not work, just run your app. You will see error log clearly.

Upvotes: 1

Katarina Vuknic
Katarina Vuknic

Reputation: 1

I wrote accidentally @EntryPoint instead of @AndroidEntryPoint. Changing that error was fixed.

Upvotes: 0

fazal ur Rehman
fazal ur Rehman

Reputation: 404

I have solved this problem. In my case, there were irrelevant dagger 
dependencies 
that the IDE did not notify me about:

   implementation 'com.google.dagger:dagger:2.35.1'
   kapt 'com.google.dagger:dagger-compiler:2.28'

After updating them, the problem disappeared and it became possible to use the 
latest version of Kotlin!

   implementation 'com.google.dagger:dagger:2.37'
   kapt 'com.google.dagger:dagger-compiler:2.37'

Upvotes: 3

Jatin Sachdeva
Jatin Sachdeva

Reputation: 1189

I faced this problem for a while. My mistake was using private access specifier with @Inject field.

If you are using Dagger then check for @Inject private fields or to know the exact cause add this as Command-line options:

--stacktrace --info --scan

On Mac, go to Android Studio > Preferences > Build, Execution, Deployment > Compiler

On Windows, go to File > Settings > Build, Execution, Deployment > Compiler

Upvotes: 10

amiron
amiron

Reputation: 731

try add to gradle.properties:

kapt.use.worker.api=false
kapt.incremental.apt=false

Upvotes: 0

Austin Chang
Austin Chang

Reputation: 89

I faced similar problem when setting up dagger2. It was finally resolved when I changed this line:

kapt "com.google.dagger:dagger-compiler:${daggerVer}"

to this

annotationProcessor "com.google.dagger:dagger-compiler:${daggerVer}"

Upvotes: 6

Droid Chief
Droid Chief

Reputation: 316

I just faced a similar bug. If you're using an old dependency for Room, update and rebuild your project.

Upvotes: 2

maksonnie
maksonnie

Reputation: 743

In my case I'm using ViewBinding instead of DataBinding. And when I got the same problem I solved it with adding plugin apply plugin: 'kotlin-parcelize' to gradle.

Upvotes: 0

MohK
MohK

Reputation: 1933

In my case, I forgot to add newly created entities into "entities" section of @Database declaration using Room library.

--stacktrace --info --scan commandline options are a great help to find the exact cause.

Upvotes: 2

Samir
Samir

Reputation: 6615

In my case build.gradle replaced
id 'kotlin-android-extensions' to id 'kotlin-parcelize' as it said on build

added

buildFeatures {
        viewBinding true
    }

also had a few syntax mistakes like forgetting : at Dao

@Query("SELECT * FROM table_satis WHERE satisId ==:satisID")

Upvotes: 0

Iván Ferrant
Iván Ferrant

Reputation: 171

The issue is probably related to the use of Room. I used the command Łukasz Kobyliński suggested in his comment

./gradlew clean build

and in my case I had to add a converter for Date type.

You can find the converter in the official docs: https://developer.android.com/training/data-storage/room/referencing-data#type-converters

Upvotes: 3

miel3k
miel3k

Reputation: 231

Issue can be connected with Room and Kotlin 1.4.10.

Try to change android.arch.persistence to androidx.room for Room dependencies:

Use

kapt "androidx.room:room-compiler:$roomVersion"

instead of

kapt "android.arch.persistence.room:compiler:$roomVersion"

Upvotes: 6

Willey Hute
Willey Hute

Reputation: 1088

If you are using the Room database and getting a KAPT error, just check your

  1. Database declarations
  2. Data Access Object declarations
  3. Data class fields

It's a problem arising due to improper usage of annotations of Room. For more information use your build logs.

You can see here in this picture before expanding the error log, I can see the annotation missing error.

Upvotes: 27

mhKarami
mhKarami

Reputation: 1014

my mistake was using suspend when then function returns LiveData.Room's coroutines integration brings ability to return suspend values but when the value itself is asnyc, there is no reason to use it. i changed :

@Delete
suspend fun Delete(premiumPackageDBEntity: PremiumPackageDBEntity)

@Query("SELECT * FROM available_premium_package ")
suspend fun GetAll(): LiveData<List<PremiumPackageDBEntity>>

to :

@Delete
suspend fun Delete(premiumPackageDBEntity: PremiumPackageDBEntity)

@Query("SELECT * FROM available_premium_package ")
fun GetAll(): LiveData<List<PremiumPackageDBEntity>>

and the problem solved.

Upvotes: 2

Sangeetha Sakthivel
Sangeetha Sakthivel

Reputation: 439

In my case, I forgot to add the room db entities to the database

@Database(version = 1,
    entities = [DummyEntity::class]
 )

Upvotes: 6

Ahmed
Ahmed

Reputation: 1

I got the same error and solved it simply by following these steps

1- open File menu -> then choose Project Structure or press Ctrl+Alt+Shift+s

2- open Modules from the left

3- In Source Compatibility press the drop down menu and choose Java 8 or 1.8

4- In Target Compatibility press the drop down menu and choose Java 8 or 1.8

5- press ok then sync and rebuild your project or run it

Upvotes: 0

Devendra
Devendra

Reputation: 3444

Worked for me:

I also had the same problem and solved it by adding this to gradle.properties

org.gradle.java.home=<go to project structure, copy JDK location and past here>

This ensures that gradlew uses the same JDK as Android Studio

Upvotes: 3

Related Questions