A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
AndroidAndroid StudioKotlinAndroid Problem Overview
All of sudden I start getting this error, and I am not getting idea why if anyone just let me know where this error is, will be enough helpful. As much I am able to get is this because of new update of android studio. Detailed summary of error I am getting.
Task :app:kaptDebugKotlin
ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1C:\Users\shubh\Downloads\MarginCalculator\app\build\generated\source\kapt\debug\com\kotlin_developer\margincalculator\DataBinderMapperImpl.java:10: error: cannot find symbol
import com.kotlin_developer.margincalculator.databinding.FragmentCalculatorScreenBindingImpl;
symbol: class FragmentCalculatorScreenBindingImpl
Task :app:kaptDebugKotlin FAILED
location: package com.kotlin_developer.margincalculator.databinding
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 17s
29 actionable tasks: 27 executed, 2 up-to-date
Android Solutions
Solution 1 - Android
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.
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.
Solution 2 - Android
Maybe I am a bit late for the answer but, anyways, I was getting the same error. The build failure can be caused by some error that Android Studio fails to point out probably due the the project size being very large.
Here is the easy way to point out the exact error.
Solution 3 - Android
The way to find out what the underlying issue is, is to run the following command :
./gradlew assembleDebug --stacktrace
Solution 4 - Android
Change
implementation "android.arch.persistence.room:runtime:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
To (updated 4 - September-2021)
implementation "androidx.room:room-runtime:2.3.0"
annotationProcessor "androidx.room:room-compiler:2.3.0"
Solution 5 - Android
I got the same issue, so I tried to get more information, by doing
> gradle->app->Tasks->Build->assemble
After this I got exact error saying "Error while annotation processing". I checked my recently tweaked DAO class and found that one of the method return type was not defined.
//Before
@Query("SELECT countryName FROM country_table WHERE countryCode= :code")
fun getCountryNameForCode(code: String)
//After
@Query("SELECT countryName FROM country_table WHERE countryCode= :code")
fun getCountryNameForCode(code: String): String
Solution 6 - Android
I started getting this after upgrading my Android Studio to Arctic Fox(2020.3.1)
.
I resolved it by updating my JDK version from 1.8
to Embedded JDK
.
To change the JDK:
-
Open your project in Android Studio and select File > Settings... > Build, Execution, Deployment > Build Tools > Gradle
-
On Mac: Android Studio > Preferences... > Build, Execution, Deployment > Build Tools > Gradle.
-
Under Gradle JDK, choose the Embedded JDK option.
As mentioned here
Solution 7 - Android
I had the same error for a while then I started checking the other packages I came to know that I've made a typo mistake in my database code. So, "Go through your database and other activity class files u may find some mistakes there."
Solution 8 - Android
After a lot of pain, I decided to try annotationProcessor
instead of kapt
hoping it may at least show an error message or anything that can help me locate the source. But fortunately (or unfortunately; because of the wasted time), it was built successfully without any errors.
It's mostly a bug in kapt
itself. So, try this solution and it may help.
Solution 9 - Android
Click on --stacktrace on the terminal to see the error in details. You can find it here:
For me the problem was I had a MacBook Pro with the new M1 chip. To solve the problem I need to add kapt "org.xerial:sqlite-jdbc:3.34.0"
before the room-compiler. See more here.
Solution 10 - Android
For me, a bunch of reference errors and an error in the XML expressions with DataBinding
produced this error.
I have deleted a <variable/>
in a layout
file, because I thought, I don't need it anymore. I forgot that I had the variable referenced in the layout
file.
After building the project, this produced an error, where it was not possible to import the BindingImpl
class, because it does not exist and this error was only shown as a warning parallel to the above KaptExecution
error.
After searching for a while, I found this error and resolved it. Then, a bunch of reference errors where shown, because I renamed something and it did not rename it in the Fragment
files. After resolving these errors too, the build finished for me without errors or warnings.
Solution 11 - Android
try to build your project with
kapt.use.worker.api=false
in your gradle.properties settings file
Solution 12 - Android
Okay, this issue also occurs if you are using Android Studio 4.1.2.
So, what you have to do is:
-
Download jdk 11 (Search and download)
-
In Android Studio, go to File -> Project Structure -> Jdk Location -> Change to the path of the Jdk you just downloaded.
Solution 13 - Android
The same problem for me here. In my case, the reason is that I've forgot a @Module annotation in one of my dagger module.
To find the real problem of such an AS alert, it's necessary going deep: the messagge in the title it's only the final one, the 1 or more causes of the error are shown selecting the root element on the left, that shows you the exact problems, check my printscreen below :
Solution 14 - Android
I had the same problem. In my case the problem was about Database. i had to change this line of code
@Database(entities = [SearchedLocation::class, FavoriteLocation::class], version = 1)
I added another table in Database but forgot to add table in the line above.
Solution 15 - Android
If you have upgraded to classpath 'com.android.tools.build:gradle:4.0.0' Replace it previous version
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
}
And Change gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4- all.zip`
Solution 16 - Android
I had the same problem. Let me walk you through the example on how I ended up to the problem and the way I resolved it perhaps you can get a bigger picture.
Before resolving
@Entity(tableName = "modules")
data class Module
(
@PrimaryKey val id: Int,
val name: String
)
@Entity(tableName = "sessions")
data class Session
(
@PrimaryKey(autoGenerate = true) var id: Int,
@ColumnInfo(name = "module_id") val moduleId: Int,
@ColumnInfo(name = "start_time") val startTime: String,
@ColumnInfo(name = "end_time") val endTime: String
)
data class ModuleSession
(
@Embedded val module: Module,
@Relation(
parentColumn = "id",
entityColumn = "module_id"
)
val sessions: List<Session>,
@ColumnInfo(name = "is_updated") val isUpdated: Boolean = false // The problem
)
In the DAO
@Transaction
@Query("SELECT * FROM modules")
abstract suspend fun getModuleSession(): List<ModuleSession>
The error I got was
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
So I dug deeper and found the below message
The columns returned by the query does not have the fields [isUpdated] in com.gmanix.oncampusprototype.Persistence.ModuleSession even though they are annotated as non-null or primitive. Columns returned by the query: [id,name]
public abstract java.lang.Object getModuleSession(@org.jetbrains.annotations.NotNull()
I removed the field IsUpdated from the POJO ModuleSession and added it to the session table
After changes
@Entity(tableName = "sessions")
data class Session
(
@PrimaryKey(autoGenerate = true) var id: Int,
@ColumnInfo(name = "module_id") val moduleId: Int,
@ColumnInfo(name = "start_time") val startTime: String,
@ColumnInfo(name = "end_time") val endTime: String,
@ColumnInfo(name = "is_updated") val isUpdated: Boolean = false
)
data class ModuleSession
(
@Embedded val module: Module,
@Relation(
parentColumn = "id",
entityColumn = "module_id"
)
val sessions: List<Session>
)
On the other hand crosscheck if there is any field on the SELECT
statement that is a suspect causing issues or you can annotate it with @Ignore
However you can post your code if you're still not comfortable.
I hope that might help
Solution 17 - Android
In my case I forget adding @PrimaryKey in Entity class.
Solution 18 - Android
In my case I got this error when moving from jcenter() to mavenCentral(), one of my dependencies was available only on jcenter() and not mavenCentral(). So i submitted issue to the owner for the same on github and will wait to remove jcenter() until its moved.
Solution 19 - Android
In my case, I used room and one of my databasDao methods has an unused parameter and unfortunately android studio doesn't warn me correctly
Solution 20 - Android
This problem also happens if you installed new kotlin plugin (1.4.20-release-Studio4.1-1
) and have dagger (kapt 'com.google.dagger:dagger-compiler:2.30'
). In such a case one solution might be replacing deprecated plugin: 'kotlin-android-extensions'
with view binding (https://developer.android.com/topic/libraries/view-binding)
Solution 21 - Android
I got the same problem when I added ROOM to my App(Kotlin). I had a hard time finding out what was wrong and finally figured out that i was using the following for adding ROOM to my app.
//room
implementation 'android.arch.persistence.room:runtime:1.1.1'
kapt "android.arch.persistence.room:compiler:1.1.1"
So i changed the above two lines with the following and got rid of the pain.
def room_version = "2.2.6"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
Hit the up button if it helped you :D
Solution 22 - Android
In my case I was using Coroutines but I forget to add Kotlin Extensions and Coroutines support for Room
def room_version = "2.2.6" implementation "androidx.room:room-ktx:$room_version"
Solution 23 - Android
Shout Out to @Rene Spies' answer above, I also got this error while working with databinding. It turns out the build engine doesn't like it when you put the @Bindable
annotation on a field in the primary constructor of a data class
in Kotlin.
So never do the following,
data class MyAwesomePojo(
@Bindable
var firstname: String,
var lastname: String
)
instead what you need to do is
data class MyCorrectAwesomePojo(
var lastname: String
):{
@get:Bindable
var firstname: String
set(value){
field = value
}
}
Bonus: remember to check for same values before setting value to field if you are trying to use two-way binding like me to prevent infinite looping of setting and getting.
Solution 24 - Android
In my case it was because I was not implementing Observable in my ViewModel. I added an EditText to the constraint layout with android:text="@={addProductViewModel.inputProductName}"
Once I implemented Observable
in my ViewModel class the error was gone
ViewModel
class AddProductViewModel (
private val repository: ProductRepository,
private val context: Context
): ViewModel(), Observable {
@Bindable
val inputProductName = MutableLiveData<String>()
fun addProduct() {
//inputProductName.value
}
override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
TODO("Not yet implemented")
}
override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
TODO("Not yet implemented")
}
}
Complete example for MVVM Databinding using Fragments
Layout - add_product.xml
<?xml version="1.0" encoding="utf-8"?>
<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 class=".AddProductBinding">
<variable
name="addProductViewModel"
type="com.rao.iremind.AddProductViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextTextProductName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Product name"
android:inputType="textPersonName"
android:text="@={addProductViewModel.inputProductName}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
AddProductFragment
class AddProductFragment: Fragment() {
private lateinit var binding: AddProductBinding
private lateinit var addProductViewModel: AddProductViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.add_product, container, false)
val dao = SubscriberDatabase.getInstance(requireActivity().applicationContext).productDAO
val repository = ProductRepository(dao)
val factory = AddProductViewModelFactory(repository, requireActivity().applicationContext)
addProductViewModel = ViewModelProvider(this, factory).get(AddProductViewModel::class.java)
binding.addProductViewModel = addProductViewModel
binding.lifecycleOwner = this
val view = binding.root
return view
}
}
AddProductViewModel
class AddProductViewModel (
private val repository: ProductRepository,
private val context: Context
): ViewModel(), Observable {
@Bindable
val inputProductName = MutableLiveData<String>()
fun addProduct() {
//inputProductName.value
}
override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
TODO("Not yet implemented")
}
override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
TODO("Not yet implemented")
}
}
Hope this helps R
Solution 25 - Android
In My Case: Issue resolved
Steps:
- Remove viewModel variable - In XML.
<variable
name="viewModel"
type="com.xx.AppViewModel" / >
- Removed all viewModel binding references - In XML.
android:text="@{viewModel.simName}"
- Removed viewModel instance reference to the binding mapping - In Activity
binding.viewModel = viewModel
-
Clean project and recompile.
-
Add viewModel variable - In XML & Build project.
< variable
name="viewModel"
type="com.xx.AppViewModel" / >
- Add viewModel instance reference to the binding mapping - In Activity & Build project
binding.viewModel = viewModel
- Add all viewModel binding references - In XML & Build project..
android:text="@{viewModel.simName}"
- It will work now.
-- I hope it will work for you also.
Solution 26 - Android
I had same problem with Room and i was not using viewBinding.
I Fixed it with using exportSchema to false in my database class.
@Database(entities = [ModelClass::class], version = 1, exportSchema = false)
abstract class ModelDatabase: RoomDatabase() {}
Remember: exportScehma could be vary according to your use case, generally it stays false so i put it to false.
Solution 27 - Android
In my case I have changed globally one variable UserManager to NetWorkManager and everywhere where there were UserManager classes they turned to be NetworkManager.
Because I'm using Hilt, I had to build the project again.
I cleaned the Project and Kotlin showed where the errors were.
Solution 28 - Android
For me, the issue was with having 2 primary keys defined on the model.
// before
@field:ColumnInfo(name = "id") @field:PrimaryKey(autoGenerate = true) var id: Long = 0,
@field:ColumnInfo(name = "name") @field:PrimaryKey var name: String,
//after
@field:ColumnInfo(name = "id") @field:PrimaryKey(autoGenerate = true) var id: Long = 0,
@field:ColumnInfo(name = "name") @field:NotNull var name: String,
I had to rebuild the project and change the Dao class a little bit to trigger the message about the issue.
Solution 29 - Android
I just had this happen to me. Another dev had not completed a merge properly so this line was in the code
<<<<<<< HEAD
Instead of getting a compile error I got the KaptExecution error.
Solution 30 - Android
I had the same error. I had two issues.
-
You may need to add
implementation 'androidx.room:room-ktx:2.2.5'
-
I had deleted a file that was referenced as a member in one of the activity_xml files. The error never gave me any clue until I changed it to annotationProcessor instead of kapt, then it pointed out the error, I found the file, and sure enough I had a reference to a file that I no longer used and was deleted. I removed this data reference from the xml and it cleared it all up. Then I put it back to kapt.
Solution 31 - Android
In my case downgrading the kotlin version ( from 1.4.21 to 1.3.21) solved my issue .
buildscript {
ext.kotlin_version = '1.3.21'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
}
Solution 32 - Android
Looks like there is an issue with room library and apple M1 chips.
Update your room lib version to 2.4.0-alpha03 or above and resync project.
Worked for me!
Solution 33 - Android
Interestingly, I was getting this error because I added the description of the Retrofit. Be careful not to confuse the description of Room and Retrofit.
Solution 34 - Android
in my case, I have added this line
android {
.
.
.
kapt.includeCompileClasspath = false
}
Solution 35 - Android
I was also encountered with the same problem so for just try I deleted it project's .idea folder and .gradle folder then i also deleted build folder inside app folder and then restart android studio IDE and it works for me
Solution 36 - Android
This method occurs to me everything there is a problem with Room
database and Coroutines
, even spell mistakes. Lastly was when trying to return a single value with a Flow after inserted column, by: Flow<Long>
from DAOs classes.
It should be a suspend
function and only Long
type to return after inserted column.
These problems are ambiguous sometime, so try to read all the Build Output messages, the message that help me was: error: Not sure how to handle insert method's return type.
Solution 37 - Android
Nothing worked I tried everything and finally found a small mistake which was creating a big problem.
Go back to each newly created file for database and check for code line by line of each file carefully.
Check Database class and check if Dao is declared as for example,
abstract val commentDatabaseDao: CommentDatabaseDao
declare as val not var, this was in my case and finally for this resolved.
Solution 38 - Android
I just updated Android Studio IDE to 4.1.1 version and got a similar issue.
What I realised was…
… before I did not had some *_Impl
classes (I know about them in Kotlin stuff) and not even some new *Tests
classes.
So, after “run all the World” to take a solution, I just made an intuitive and fair choice: I deleted all those files inside my "hand builded" packages that was not there before Android Studio update.
And, guess what?
It worked. No issues, not even one problem about kapt
.
I’m not saying that it is a final solution. But it can work for you.
Have a good one.
Solution 39 - Android
Yup, I also got this error and it was also a Room
related issue.
I had defined my TypeConverters
, but never annotated my Room database
with: @TypeConverters(TypeConverter.class)
.
Solution 40 - Android
It's can be happened if non-english characters used in binding expressions within layout. For example:
<TextView ... android:text="@{ viewModel.letterTitle, default=`Важное письмо` }" />
You can check this case if you execute "Run with --stacktrace option". If stacktrace contains message "MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence." then possibly
- layout contains non-english characters
- or some source xml file have incorrect format
Solution 41 - Android
Solved by just increasing gradle version to latest -
It is in the project level gradle file, under dependencies block.
classpath "com.android.tools.build:gradle:4.x.x"
Solution 42 - Android
This happened to me when trying to Inject a private field inside an activity using Hilt.the solution was to remove the private keyword so change
@Inject
private lateinit var appAnalytics: AppAnalytics
to
@Inject
lateinit var appAnalytics: AppAnalytics
Solution 43 - Android
For me, this issue occurred because of the java version difference
In my case
-My studio default java version was 11
-I have a library in my project with java version 8 support
Fix -
1.listed all installed java version with command /usr/libexec/java_home -V
2.Copy java8 path:/Library/Java/JavaVirtualMachines/1.8.0_232.jdk/Contents/Home
- go to
File - Project structure - SDK location -JDK location
Add your java path there
Solution 44 - Android
In my case there were older dependencies which needed an upgradation, So you can follow this :
1.FILE -> PROJECT STRUCTURE ->
2.Go to SUGGESSIONS ->
3.Under the WARNING SECTION Android Studio will show you which dependencies needs to be upgraded->
4.Just upgrade it directly from there ->
5.CLICK APPLY->
It will work now!
Thanks!
Solution 45 - Android
For me the problem is about kotlin gradle plugin. Sometimes android studio updates kotlin gradle plugin by itself. Revert back gradle plugin solves problem.
For insatance from this:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
to this:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61"
Solution 46 - Android
in some cases it may help gradle.properties
kapt.include.compile.classpath=true
Solution 47 - Android
If you are using Kotlin with Hilt
Make sure you have annotated your module class with
@Module
@InstallIn(SingletonComponent::class)
class AppModule {
//...
}
Solution 48 - Android
Got exactly the same error while upgrading to kotlin_version = '1.4.31' (from 1.3.71)
The following moshi upgrade fixed the my problem:
implementation("com.squareup.moshi:moshi:1.11.0")
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.11.0")
Solution 49 - Android
I have an enum in my entity property using ROOM. It causes the following failure after long searching.
> Execution failed for task ':app:kaptDebugKotlin'. > > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution > > java.lang.reflect.InvocationTargetException (no error message)
enum class Color{RED,BLACK,BLUE,GREEN,WHITE}
@Entity(tableName = "flower_table")
data class Flower(
@PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "color") val color: Color)
val flower = Flower(2, "rose", Color.RED)
My solution is to use the index of the enum instead in the entity property.
@Entity(tableName = "flower_table")
data class Flower(
@PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "color") val color: Int)
val flower = Flower(4, "tulip", Color.BLUE.ordinal)
I hope it could help you! PS, do not forget to update the version number in your database class.
@Database(entities = [Flower::class], version = 2)
abstract class FlowerRoomDatabase : RoomDatabase() {
Solution 50 - Android
I was getting the same error, and for me the issue was I needed to switch from Gradle JDK JDK version 16.0.1
to Android Studio java home version 11.0.10
in Android Studio > Preferences... > Build, Execution, Deployment > Build Tools > Gradle
Solution 51 - Android
I got this error after doing the following:
- Upgrading Android Studio to Arctic Fox
- Updating the Kotlin plugin to version
1.5.21
- Upgrading my Java version to
16
Running assembleDebug
with --stacktrace
, I saw this exception in the logs: Caused by: java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext
. Searching around, I found this link that talks about compatibility issues between kapt
and JDK16+
.
I resolved the issue by making this change in my gradle.properties
file:
org.gradle.jvmargs=-Xmx1536m --illegal-access=permit
Solution 52 - Android
This error also comes due to data binding errors
- Check if your variable is pointing to the correct data class
- Check if your fields due for your views (text view, visibility are correct)
- You have imported the correct imports (for eg
for visibility related operations)
Solution 53 - Android
it took me days to find the solution to this problem, use a high version of the androidx.lifecycle:lifecycle-compiler plugin.
implementation 'androidx.lifecycle:lifecycle-runtime:2.2.0-alpha03'
kapt 'androidx.lifecycle:lifecycle-compiler:2.2.0-alpha03'
and remove this from your code kapt "android.arch.lifecycle:compiler:1.1.1"
Solution 54 - Android
In my case I just renamed rootProject.name in /settings.gradle I repeated this action again and it works .. It doesn't make sense why it happened but I hope that may help others and save their time
Solution 55 - Android
In my case i solve it by updating my kotlin version from 1.3.x to 1.6.0.