Search code examples
androidkotlintype-conversionandroid-roomkotlin-symbol-processing

Issue: java.lang.IllegalStateException: Storage for [...] is already registered When Adding Type Converters with KSP


Overview:

When working with Room and KSP (Kotlin Symbol Processing) for type converters in an Android project, I encountered a recurring issue after adding a new type converter. The error message was:

java.lang.IllegalStateException: Storage for [C:\...\symbolLookups\id-to-file.tab] is already registered

Context:

This issue appeared after introducing a new type converter for handling a list of strings (List<String>) with Room. The app uses Kotlin Symbol Processing (KSP) to handle annotation processing for Room and type converters.

The Problem:

Whenever I added a new type converter, KSP's caching mechanism caused a conflict. The error prevented the project from compiling. The root cause seems to be related to how KSP handles the storage of symbol lookup files, which may not automatically clear when significant changes occur (such as adding new type converters).

Solution:

Here’s how I resolved it:

  1. Clean the project using the Build -> Clean Project option in Android Studio.
  2. Invalidate caches and restart the IDE by navigating to File -> Invalidate Caches / Restart.
  3. Reboot the machine—surprisingly, this step was necessary after the above actions didn't work by themselves. Once the machine rebooted, the project compiled successfully.

Why does this happen?

It seems to be related to how KSP manages its caches. When introducing new elements, such as type converters, KSP may not invalidate certain cached symbol lookup files automatically. This results in a conflict when it attempts to register new storage for these symbols. A clean, followed by cache invalidation and a system restart, forces the system to refresh these caches fully.

Has anyone else faced this problem when adding a new type converter in a Room with KSP? Are there any known permanent fixes or preventive steps beyond cache invalidation and restarting the machine?

I'd appreciate any insights or potential solutions, especially if there's a more reliable way to prevent this error. Feel free to share your experiences with similar KSP cache issues!


Solution

  • I don't know the final solution, but you stop a gradle deamon:

     ./gradlew --stop
    

    It will kill the deamon and the next building will be working. But you have to do it every time, when it freezes...