'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()
}


Solution 1:[1]

Run your application with ./gradlew clean build command to see what's exactly wrong with your code. Just paste it into the Terminal in Android Studio.

Solution 2:[2]

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.

Solution 3:[3]

I faced this problem for a while. What helped me a lot was reading the build tab because it gave the reasons the library was failing.
Here is the tab Build tab I had many problems,
1. I haven't added the new entity I created into the @Database annotation
2. I haven't added the @Dao annotation in my interface
3. I haven't updated some variables names that was wrote in a @Query annotation
So I had to kill problem by problem, finally it could run later. In Addition, I was cleaning my project and rebuilding to ensure code doesn't get stuck. Also close and open Android Studio.

Futhermore, you can check this answer to help you find the error enable more log on error

Solution 4:[4]

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

Solution 5:[5]

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

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

Solution 6:[6]

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"

Solution 7:[7]

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

Solution 8:[8]

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

Solution 9:[9]

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

Solution 10:[10]

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

Solution 11:[11]

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.

Solution 12:[12]

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.

Solution 13:[13]

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

Solution 14:[14]

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}"

Solution 15:[15]

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'

Solution 16:[16]

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

Solution 17:[17]

For me I deleted these folders.

  1. .gradle
  2. .idea

Close android studio , delete the folders and reopen the project

enter image description here

Solution 18:[18]

First change

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

to

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

Now you have to tweak your project Gradle file and update the version of Kotlin being used which should be something like below:

ext { kotlin_version = '1.3.10' gradleVersion = '3.1.0' }

Solution 19:[19]

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

Solution 20:[20]

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")

Solution 21:[21]

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.

Solution 22:[22]

try add to gradle.properties:

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

Solution 23:[23]

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

Solution 24:[24]

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"

Solution 25:[25]

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.

Solution 26:[26]

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