13

This happens for both Dagger generated files and for LiveData generated binding files.

I'll change branches and the generated file for the new Live Data layout or the generated MemberInjector for a new daggeer component will stick around even though those new components are actually on the other branch.

I'll do a Invalidate caches and restart as well as a clean build. But it doesn't work. I even tried manually deleting the files and they just got regenerated.

The only way I've found to fix it is to uninstall android studio and reinstall it.

My co-worker who uses a mac does not experience this problem.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Dan Anderson
  • 1,062
  • 13
  • 26

3 Answers3

24

This might happen when using Gradle with the build cache enabled where it fails to recognize files no longer present after switching branches, so that it keeps recreating files that reference non-existant code.

Clearing the build cache directory (USER_HOME/.gradle/caches/build-cache-1) helps resolve the issue

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
  • Can you elaborate on why gradle is unable to recognize source files change? The annotation processors must have defined the source files as an input. – Vishal Arora Feb 10 '20 at 12:49
  • @VishalArora I myself haven't had this issue in quite some time now. Make sure to use the latest versions of Gradle, Android Gradle Plugin, and Dagger – David Medenjak Feb 10 '20 at 13:07
  • Got it. I believe this was an issue with kapt reported here - https://youtrack.jetbrains.com/issue/KT-34258 . And it is not related to dagger or any other code generating library. – Vishal Arora Feb 10 '20 at 13:09
8

For myself, it was a combination of @David Medenjak's answer and a couple of other things.

  1. Delete the Gradle build cache: rm -rf ~/.gradle/caches/build-cache-1/
  2. Not sure if this step is required but delete your module's build/generated folder.
  3. Perform a rebuild of the project. Build > Rebuild Project
StuStirling
  • 15,601
  • 23
  • 93
  • 150
7

I fixed this kind of problem (dagger created fabric for deleted class) by:

  1. Editing gradle.properties:

org.gradle.caching=false

  1. Building project

  2. Restoring gradle.properties:

org.gradle.caching=true

I also noticed that cleaning the project and deleting caches only doesn't work.

Lluís Suñol
  • 3,466
  • 3
  • 22
  • 33
Inliner
  • 1,061
  • 7
  • 10
  • I'm sorry, your answer seems a bit messy to me. Do you think you could improve it (maybe just formatting is ok, I don't really know) to maybe find out a new best answer for this question? Thanks! – Lluís Suñol Sep 18 '19 at 12:40
  • @LluísSuñol I've edited it. See if it looks better for you now :) – Inliner Sep 19 '19 at 13:30
  • Better, but still some things need to be clarified, I think... you typed "had the same problem": this is extra information we don't really need to know, you can remove this and better start your response with something like "I solved this problem by..." (if you really did!). When you say "gradlew clean", etc. I guess these are command line, so it would be better if you give them the proper markdown format. "didn't help" refers to what you explained before, right? so if it didn't work... then you better don't add it to the response, remove it and leave only the actual solution for the problem. – Lluís Suñol Sep 19 '19 at 14:00
  • @LluísSuñol Is it ok now?) – Inliner Sep 20 '19 at 06:40
  • Quite better. Notice though that I edited your answer taking profit of markdown capabilities to make it look a bit more readable now. – Lluís Suñol Sep 20 '19 at 09:47