98

Android studio gave the 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)

I want to add in my project Kotlin Coroutines and use it with Room database. But after added all libraries I got this error. This is all information from the compiler.

I have identified, This is because of the annotation @Database. If I removed this annotation, the error don't appear, but Room is not working too.

My gradle file:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
//apply plugin: 'androidx.navigation.safeargs'

kotlin {
    experimental {
        coroutines 'enable'
    }
}

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.bestcred.coursetthree"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        //vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    // Enables data binding.
    buildFeatures {
        dataBinding true
    }
}

dependencies {


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

    // Support libraries
    implementation "androidx.appcompat:appcompat:1.2.0"
    implementation 'com.google.android.material:material:1.2.0'
    implementation "androidx.fragment:fragment:1.2.5"
    implementation "androidx.constraintlayout:constraintlayout:2.0.0"

    // Android KTX
    implementation 'androidx.core:core-ktx:1.3.1'

    // Room and Lifecycle dependencies
    implementation "androidx.room:room-runtime:$room_version"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    kapt "android.arch.persistence.room:compiler:$room_version"

    // Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"

}

kotlin_version = "1.4.0"
room_version = "2.2.5"
coroutine_version = '1.3.9'

I update Room version and add Kotlin Coroutines. What's problem?enter image description here

Alex
  • 1,407
  • 2
  • 10
  • 19

26 Answers26

208

Android Studio's UI was just hiding the error...

when the error occurred, it highlighted the item in the list view, and showed an unhelpful message in the terminal view. unhelpful error message appears when erroneous item is selected

to find the real error, select the root item in the list view so that Android Studio would display the whole build output in the terminal view, then scroll to find error. helpful error message appars when you select the root item from the list view on the left

Eric
  • 16,397
  • 8
  • 68
  • 76
  • 1
    Ahhh ... I ran into this before but did not recall the solution. For me to resolve the prior time, I had to setup the command-line build and searched the output. THIS IS MUCH EASIER and more direct!!! Bonus points for you and get a special badge. BTW - my issue is related to @Transaction functions in my Dao object after upgrading the compiler and frameworks (probably room). – mobibob Jan 08 '21 at 00:34
  • 1
    In my case I don't see a list on the left hand side like in your screenshot @Eric – Alvin Dizon May 30 '21 at 02:44
  • In my case the error wasn't shown when root item was selected... Doing `./gradlew clean build` from the terminal showed the error – Demigod Oct 08 '21 at 15:33
  • 1
    it's not always the case. I selected build failed but it also doesn't contain anything – user924 Nov 22 '21 at 13:51
  • it does not show anything to me!! – StackOverflower May 10 '23 at 09:01
31

I develop in Apple Silicon Macbook M1.

use room_version 2.2.4, fails in 2.2.5

def room_version = "2.2.4"

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
testImplementation "androidx.room:room-testing:$room_version"
Minkyu Kang
  • 311
  • 3
  • 3
18

You need change:

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

to

kapt "androidx.room:room-compiler:$room_version"
Alex
  • 1,407
  • 2
  • 10
  • 19
13

This exception occurs when you have done some mistake on Room database or Doa or entity class for example I had done mistakes in the entity class

  1. I had made the autogenerated field of Entity class val instead of var
  2. I had put delete annotation on two functions with a different name but they were deleting the same data

so I would suggest to check the entity,dao or database class carefully if you imported the right dependency.

@Entity(tableName = "user_table")
data class User(
    val firstName: String,
    val lastName: String,
    val age: Int
) {
    @PrimaryKey(autoGenerate = true)
    var id: Int = 0 //**do not made it val**
}
Arun Aditya
  • 828
  • 8
  • 10
11

I'm currently having this error on m1 Mac with Room version 2.3.0.

I fixed it by changing it to 2.4.0-alpha04.

This has been reported to Google (issue tracker).

clement.l
  • 189
  • 1
  • 6
8

Today I faced this error. In my case, I have both java 11 and java 16 installed in my laptop, and java 16 was the default. But the gradle in my project uses java 11 to build it. So when I invoked

$ gradle build

from my terminal, it would use java 16 and so the build failed. I know, the proper solution is to make my code compatible with java 16, but for now, I am explicitly telling gradle to use java 11 using the following command:

$ gradle build -Dorg.gradle.java.home=/usr/lib/jvm/java-11-openjdk-amd64 # warning: use te path to your java 11 

Since it is a large command, I saved it in a script file and so simply running the script solves the problem for me.

Qazi Fahim Farhan
  • 2,066
  • 1
  • 14
  • 26
  • 4
    In Android Studio go to Settings (Alt+Ctrl+S) ➡ Build, Execution, Deployment ➡ Build Tools and select the correct Gradle JDK version. – John Doe Jul 23 '21 at 17:16
  • android studio is using java 11, that is ok. but you see, we are working on an android library. The library should be published using github packages. Last time when I had checked, all the tutorials were using the terminal. So in my usecase, I had to build and publish using terminal. That's why I faced the trouble. – Qazi Fahim Farhan Jul 23 '21 at 18:23
  • 1
    That was my issue. Saved my life – Sebastien FERRAND Jan 27 '22 at 03:50
  • Thanks, @JohnDoe! I changed Java from 11 to 15 in settings. – CoolMind Jun 22 '22 at 16:21
6

Try to add this in your app/build.gradle file and run the program again. It helped me to find the real cause of the issue.

kapt {
    correctErrorTypes true 
} 
Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
5

I might be late but only following solution worked for me.

Open gradle.properties file in Android studio.

Add following lines:

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

Clean & Re-build the application.

Done

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
3

I had faced the same issue. The problem was because I was using coroutine suspend functions in the Room DAO and had not implemented the necessary implementations. The following worked for me, try replacing the existing Room dependencies with:

def room_version = "2.2.5" //Use latest version

"androidx.room:room-runtime:$room_version"
"androidx.room:room-compiler:$room_version"
"androidx.room:room-ktx:$room_version"

If the above does not work, go to Analyze->Inspect Code and check for code-breaking warnings or errors(Specifically in Room)

Rawlin Crasto
  • 195
  • 2
  • 8
3

Make sure Gradle JDK is set to the correct version (it was set to JDK 8 even though I've been using JDK 11 for quite some time now).

Open Preferences -> Builds Tools -> Gradle -> Set Gradle JDK.

Image of Gradle JDK in Android Studio

malinjir
  • 1,475
  • 2
  • 12
  • 17
2

I had to update Room to latest 2.2.5 and it has resolved the issue.

sonique
  • 4,539
  • 2
  • 30
  • 39
2

This issue occurred on package renaming I've dragged and dropped all files from one package to other.

Objectbox it created the cursor files and entity files (auto-generated files)

Had to delete them and re-built.

It ate up 1 hour straight -_-

Prabs
  • 4,923
  • 6
  • 38
  • 59
2

Update the gradle JDK to java 11.

Shaikh Mohib
  • 278
  • 3
  • 11
2

You have to use gradlew assemble --stacktrace in terminal to get more detailed error log.

If you're on Mac, then run this instead ./gradlew assemble --stacktrace

flamyoad
  • 519
  • 7
  • 15
1

Make sure to include all entities inside the Database annotation.

Eyosiyas
  • 1,422
  • 1
  • 9
  • 25
1

I updated the Room version from 2.2.5 to 2.3.0 or to current version in the build.gradle script file. The build was successful.

tonnymulatya
  • 626
  • 6
  • 4
1

There's an error that is in your files, but the editor cannot show you because the file is closed and hasn't been analysed.

This mostly occurs when you amend some data classes/POJOs and your app has several layers that depend on each other.

As the dependency goes up, some functions cannot infer data types and returns this kind of an error.

  1. Command+Click on the modified data classes and see where they are being used, you may possibly find the error there.
  2. Open your files one by one, you don't have to hang on there and wait for the editor to finish analysing(If your editor takes some time to), as the editor analyses, the errors will be displayed and you can fix them and have a successful build.

There's the option of Code -> Analyse code on android studio but sometimes it doesn't work as expected, you may try this - just incase.

KE Keronei
  • 323
  • 2
  • 8
1

For Apple M1 processor : Kindly update the room version to the latest one.

implementation "androidx.room:room-runtime:2.4.3"
implementation "androidx.room:room-rxjava2:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
0

In my case I gave the argument type of insertAll() and delete() function as Entity ofyour database, the Error Solved

@Dao
interface NotesDao {

    @Insert
    fun insertAll(INotesEntity: NotesEntity) // argument type should be entity of your database

    @Delete
    fun delete(DNotesEntity: NotesEntity)    // argument type should be entity of your database

}
0

While it's rather strange, I had onDestory defined two times in one file and caused this error when trying to run the app, once i deleted one of them everything worked normally.

Amr
  • 1,068
  • 12
  • 21
  • How is that possible? It will be a compile error. – CoolMind Jun 22 '22 at 16:26
  • 1
    I encountered a similar problem. Also after rebasing to another branch got classes where methods were duplicated. First I got this compile error and later other error. – CoolMind Jul 07 '22 at 08:13
0

When I faced this issue, it was about data-binding in an XML file. I deleted a code using backticks `` and changed it into string interpolation.

Mark Choi
  • 420
  • 6
  • 16
0

suspend not being compatible with LiveData, So delete suspend from your DAO function that returns LiveData

  • To clarify: You CAN use suspend with live data, but it would be a bit strange to do so, LiveData (in Room etc.) is already asynchronious and it will work just fine in the background, so there is no need to use "suspend" here – JustSightseeing Oct 24 '22 at 20:46
0

I had the same situation with Dagger library. Like as @StackOverFlower's response, there was a problem with Room library inside Dagger library. It started to build after I download and select coretto-11 JDK under Build Tools > Gradle page.

enter image description here

ycannot
  • 1,807
  • 1
  • 9
  • 20
0

In my case I had to close my Utils.Java class with }.

From:

public static final String DUMMY_KEY = "dummyKey";
public static

To:

public static final String DUMMY_KEY = "dummyKey";
}
Tigerrrrr
  • 526
  • 6
  • 24
-1

The only thing that worked for me -

In gradle.properties replace the following line -

org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8

by

org.gradle.jvmargs=--illegal-access=permit
-1

According to the question that asked here and the solution for Dagger/Hilt this some times is because of kapt. You just need to change:

//This
kapt "androidx.room:room-compiler:$room_version"
//To
annotationProcessor "androidx.room:room-compiler:$room_version"

Or according to Room document site You can also use ksp instead if kapt, it's 2x faster, in this case you must/should not use annotationProcessor. Your dependencies would look like this:

def room_version = "2.5.1"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-runtime:$room_version"
ksp "androidx.room:room-compiler:$room_version"
StackOverflower
  • 369
  • 2
  • 12