Kapt build cache never cleared?

I’m working on an Android project with multiple build flavors and we’re using dagger and kapt.

Whenever we’re switching branches we will always have build errors if we don’t to a gradle clean. The cause is kapt because it has in it’s cache generated/compiled classes from the other branch. This never happend with gradle when we weren’t using kotlin in our project.

It also happens if we modify classes related to dependency injection: modules, components or annotated constructors/singletons; in such a way that requires to regenerate some classes for the dependency injection to work. Kapt is probably not very aware of those changes and it won’t clear it’s cache.

I honestly don’t know what’s causing it so I can’t create a sample project where this happens but I can reproduce it very easily on my project.

What could be the problem? How can I help you help me? Is there a --debug switch for kapt to see where it checks for changes?

5 Likes

I am seeing the same issue when switching branches and changing annotations that cause code to be generated. Hopefully @yanex can help.

It would probably be useful to include the versions of Android Gradle plugin and kapt you are using.

Here are mine:
Kotlin v1.1.60
Android Gradle Plugin v3.0.0

To me it happens with kotlin 1.1.4, 1.1.50, 1.1.60 and android gradle 2.3.3

Guys, can we get some help on this? On 1.2.0 our build times have increased

Here are some logs:

:app:kaptGenerateStubsKaufdaDebugKotlin
Caching disabled for task ':app:kaptGenerateStubsKaufdaDebugKotlin': Caching has not been enabled for the task
Putting task artifact state for task ':app:kaptGenerateStubsKaufdaDebugKotlin' into context took 0.002 secs.
Task :app:kaptGenerateStubsKaufdaDebugKotlin class loader hash: a838e713f4f3b6468d76718b109f2e8a
Task :app:kaptGenerateStubsKaufdaDebugKotlin actions class loader hashes: [750e1d72030581ace81f7325929812f4, a838e713f4f3b6468d76718b109f2e8a]
Cache key for task ':app:kaptGenerateStubsKaufdaDebugKotlin' is 76c7e3e6d60f64c2b81fa890c9d429f2
Executing task ':app:kaptGenerateStubsKaufdaDebugKotlin' (up-to-date check took 0.0 secs) due to:

The following lines basically say that the stubs should have been regenerated:

      Task ':app:kaptGenerateStubsKaufdaDebugKotlin' class path has changed from 7d1b1118f451dc19a87f1c96b72ff8f4 to a838e713f4f3b6468d76718b109f2e8a.
    All input files are considered out-of-date for incremental task ':app:kaptGenerateStubsKaufdaDebugKotlin'.
    file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/main/kotlin', not found
    file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufda/kotlin', not found
    file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/debug/kotlin', not found
    file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufdaDebug/kotlin', not found
    file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/main/kotlin', not found
    file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufda/kotlin', not found
    file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/debug/kotlin', not found
    file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufdaDebug/kotlin', not found
    :app:kaptGenerateStubsKaufdaDebugKotlin (Thread[Daemon worker Thread 10,5,main]) completed. Took 12.875 secs.

Continuing:

:app:kaptKaufdaDebugKotlin (Thread[Daemon worker Thread 10,5,main]) started.
:app:kaptKaufdaDebugKotlin
Caching disabled for task ':app:kaptKaufdaDebugKotlin': Caching has not been enabled for the task
Putting task artifact state for task ':app:kaptKaufdaDebugKotlin' into context took 0.019 secs.
Task :app:kaptKaufdaDebugKotlin class loader hash: a838e713f4f3b6468d76718b109f2e8a
Task :app:kaptKaufdaDebugKotlin actions class loader hashes: [750e1d72030581ace81f7325929812f4, a838e713f4f3b6468d76718b109f2e8a]
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/main/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufda/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/debug/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufdaDebug/kotlin', not found
Cache key for task ':app:kaptKaufdaDebugKotlin' is 9d10198f22eee8a3aa34396384206863
Executing task ':app:kaptKaufdaDebugKotlin' (up-to-date check took 0.002 secs) due to:
  Output property 'destinationDir' file /Users/aurelian.dumanovschi/Android/android-core/app/build/generated/source/kapt/kaufdaDebug/com/bonial/common/network/BasicConfig_MembersInjector.java has changed.
  Output property 'destinationDir' file /Users/aurelian.dumanovschi/Android/android-core/app/build/generated/source/kapt/kaufdaDebug/com/bonial/shoppinglist/operations/ShoppingListRepositoryImpl_Factory.java has changed.
  Output property 'destinationDir' file /Users/aurelian.dumanovschi/Android/android-core/app/build/generated/source/kapt/kaufdaDebug/com/bonial/kaufda/repository/offer_banner/OfferBannerRepositoryImpl_Factory.java has changed.
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/main/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufda/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/debug/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufdaDebug/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/main/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufda/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/debug/kotlin', not found
file or directory '/Users/aurelian.dumanovschi/Android/android-core/app/src/kaufdaDebug/kotlin', not found
w: warning: Supported source version 'RELEASE_7' from annotation processor 'android.arch.persistence.room.RoomProcessor' less than -source '1.8'
w:

w: /Users/aurelian.dumanovschi/Android/android-core/app/src/main/java/com/bonial/shoppinglist/model/Clipping.java:22: warning: shoppingListItemId column references a foreign key but it is not part of an index. This may trigger full table scans whenever parent table is modified so you are highly advised to create an index that covers this column.
w:

w: public class Clipping implements Parcelable {
w:        ^
w: /Users/aurelian.dumanovschi/Android/android-core/app/src/main/java/com/bonial/shoppinglist/database/ShoppingListDatabase.java:10: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
w:

w: public abstract class ShoppingListDatabase extends RoomDatabase {
w:                 ^
w: /Users/aurelian.dumanovschi/Android/android-core/app/build/tmp/kapt3/stubs/kaufdaDebug/com/bonial/kaufda/favorites/data/FavoritesRoomDatabase.java:8: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
w:

w: public abstract class FavoritesRoomDatabase extends android.arch.persistence.room.RoomDatabase {
w:                 ^

And now the errors:

e: /Users/aurelian.dumanovschi/Android/android-core/app/build/tmp/kapt3/stubs/kaufdaDebug/com/bonial/kaufda/favorites/SimpleStringFavorable.java:4: error: cannot find symbol
e:
e: public final class SimpleStringFavorable implements com.bonial.kaufda.favorites.Favorable {
e:                                                                                ^
e:   symbol:   class Favorable
e:   location: package com.bonial.kaufda.favorites
e: /Users/aurelian.dumanovschi/Android/android-core/app/build/tmp/kapt3/stubs/kaufdaDebug/com/bonial/kaufda/favorites/SimpleFavorable.java:4: error: cannot find symbol
e:

So, kapt says that the stubs have been generated, but that’s impossible because in the sources SimpleFavorable implements com.bonial.kaufda.favorites.**data**.Favorable and not com.bonial.kaufda.favorites.Favorable

Also, SimpleStringFavorable doesn’t exist anymore

More insights after I ran with --debug:

16:37:13.265 [DEBUG] [org.gradle.api.Task] [KOTLIN] args.coroutinesState=warn
16:37:13.265 [DEBUG] [org.gradle.api.Task] [KOTLIN] args.moduleName = app_kaufdaDebug
16:37:13.265 [DEBUG] [org.gradle.api.Task] [KOTLIN] args.friendPaths = []
16:37:13.279 [DEBUG] [org.gradle.api.Task] [KOTLIN] kaptGenerateStubsKaufdaDebugKotlin destinationDir = /Users/aurelian.dumanovschi/Android/android-core/app/build/tmp/kapt3/incrementalData/kaufdaDebug
16:37:13.300 [INFO] [org.gradle.api.internal.changedetection.rules.TaskTypeTaskStateChanges] Task :app:kaptGenerateStubsKaufdaDebugKotlin class loader hash: a838e713f4f3b6468d76718b109f2e8a
16:37:13.300 [INFO] [org.gradle.api.internal.changedetection.rules.TaskTypeTaskStateChanges] Task :app:kaptGenerateStubsKaufdaDebugKotlin actions class loader hashes: [750e1d72030581ace81f7325929812f4, a838e713f4f3b6468d76718b109f2e8a]
16:37:13.301 [DEBUG] [org.gradle.api.Task] [KOTLIN] args.coroutinesState=warn
16:37:13.301 [DEBUG] [org.gradle.api.Task] [KOTLIN] args.moduleName = app_kaufdaDebug
16:37:13.301 [DEBUG] [org.gradle.api.Task] [KOTLIN] args.friendPaths = []
16:37:13.306 [DEBUG] [org.gradle.api.Task] [KOTLIN] kaptGenerateStubsKaufdaDebugKotlin destinationDir = /Users/aurelian.dumanovschi/Android/android-core/app/build/tmp/kapt3/incrementalData/kaufdaDebug
16:37:13.609 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending taskClass to build cache key: org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask_Decorated
16:37:13.609 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending classLoaderHash to build cache key: a838e713f4f3b6468d76718b109f2e8a
16:37:13.609 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending actionsClassLoaderHash to build cache key: 750e1d72030581ace81f7325929812f4
16:37:13.609 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending actionsClassLoaderHash to build cache key: a838e713f4f3b6468d76718b109f2e8a
16:37:13.609 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending inputPropertyHash for 'coroutinesStr$kotlin_gradle_plugin' to build cache key: f11c69e19062475a4bd6447e0c6820fc
16:37:13.610 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending inputPropertyHash for 'javaPackagePrefixInputString$kotlin_gradle_plugin' to build cache key: f1d3ff8443297732862df21dc4e57262
16:37:13.610 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending inputPropertyHash for 'moduleName$kotlin_gradle_plugin' to build cache key: 61e0124d79f754926c1eb4d194b81d97
16:37:13.610 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending inputPropertyHash for 'serializedCompilerArguments' to build cache key: c525fdd224dbfcd06b11e1bc407799cb
16:37:13.610 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending inputPropertyHash for 'sourceCompatibility' to build cache key: 12f0ae79f405c46e9045f83b66543728
16:37:13.610 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending inputPropertyHash for 'targetCompatibility' to build cache key: 12f0ae79f405c46e9045f83b66543728
16:37:13.610 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending inputPropertyHash for 'classpath' to build cache key: 73262bd07b83ecea05928b074ad0e0d8
16:37:13.610 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending inputPropertyHash for 'computedCompilerClasspath$kotlin_gradle_plugin' to build cache key: e53225cac337e44491e70513bd7dd516
16:37:13.613 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending inputPropertyHash for 'source' to build cache key: 046f968d912d0c4b97785f3237c8b43c
16:37:13.614 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending outputPropertyName to build cache key: destinationDir
16:37:13.614 [DEBUG] [org.gradle.caching.internal.tasks.DefaultTaskOutputCachingBuildCacheKeyBuilder] Appending outputPropertyName to build cache key: stubsDir
16:37:13.614 [INFO] [org.gradle.api.internal.tasks.execution.ResolveBuildCacheKeyExecuter] Cache key for task ':app:kaptGenerateStubsKaufdaDebugKotlin' is 109bb1ceecae3fd7beb8a83ae89d8daa
16:37:13.614 [DEBUG] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Determining if task ':app:kaptGenerateStubsKaufdaDebugKotlin' is up-to-date
16:37:13.617 [INFO] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Skipping task ':app:kaptGenerateStubsKaufdaDebugKotlin' as it is up-to-date (took 0.003 secs).
16:37:13.617 [DEBUG] [org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter] Removed task artifact state for {} from context.
16:37:13.617 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':app:kaptGenerateStubsKaufdaDebugKotlin'

So kaptGenerateStubsKaufdaDebugKotlin outputs to app/build/tmp/kapt3/incrementalData/kaufdaDebug

But the compilation error references app/build/tmp/kapt3/stubs/kaufdaDebug/com/bonial/kaufda/favorites/SimpleFavorable.java

Which that file doesn’t exist in my sources anymore (but it still exists in the stubs), it’s in the data subpackage. And I can find the correct file indeed in app/build/tmp/kapt3/stubs/kaufdaDebug/com/bonial/kaufda/favorites/data/SimpleFavorable.java

Also the package app/build/tmp/kapt3/incrementalData/kaufdaDebug/com/bonial/kaufda/favorites/ is correct in that it doesn’t have outdated files, but during branch change and recompilation, the stubs folder isn’t cleared or, in my opinion, not used altogether.

I’m using

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

kapt {
correctErrorTypes = true
}

I tried to create a simple sample project because I thought I could reproduce this and in there the cache is cleared correctly.

But on 2 older projects which were migrated to kotlin we can reproduce this. So it seems to be caused in complex projects.

How can I debug this more?

We frequently experience similar issues, so I made a sample project with one of the repos we found.

Instructions can be found in the readme.

Thanks for creating the repro project! Could you please file a YouTrack issue with the link to your project, so that we can track the resolution of this problem? Thanks!

I can add it to my ticket https://youtrack.jetbrains.com/issue/KT-21735