Recently, I merged a repository’s main branch into an older feature branch, only to get a mysterious InvocationTargetException during compilation:

Viewing the full error (by clicking “Build: failed”) provided lots more information but… only deepened the mystery! All of the apparently duplicate classes are temporary Java files (the codebase is 100% Kotlin) and the classes all only have 1 definition.

There’s also various errors to do with “ANTLR Tool version”, and even some lint warnings about deprecated methods sneaking in. Definitely important, but very unrelated to the duplicate class mystery. My first thought was Android Studio’s cache causing issues, but a project clean and cache clear didn’t help. Hmm!

After looking into all of the Kotlin files, checking no major changes had been merged, redoing the merge, and fruitlessly searching various StackOverflow answers, I finally found the answer.

The merge had resulted in a class (that referenced all the supposedly duplicate classes) having two companion objects:

The obvious fix of removing the outdated companion immediately resolved the issue, shame the original error was so unhelpful!

And yes, this did take an embarrassingly long time to resolve.