4

I need to be able to switch off the 'Duplicate class' error and visual indicator (the red squiggly underline) that you get when you have two classes that are named the same in the same package but in different folders. In this case, 'main' and 'test'.

Background

It could be argued that this is a duplicate of this posting, however, I thought some background would be in order to avoid some of the obvious replies (and also, there is no satisfactory answer to that post).

This question is related to unit-testing and collaborator mocking. As everyone knows, Android violates the basic 'containment over inheritance' guideline for java development. This is why you need Robolectric to test lifecycle overrides without the framework crashing your tests. Your collaborator can't be mocked and injected in the normal way because the class under test is the collaborator.

In this case, my large corporate client has further violated the 'containment over inheritance' guideline by adding several layers of class extensions from Activitys and Fragments that have to be used instead. I can't modify these layers and attempting to mock out the internals is impractical. The simplest solution has been to create duplicates of the leaf classes of this extra layer that provide unit-test-friendly results to the class-under-test.

This works. When you run unit tests the test duplicate is resolved and when you run production code the production duplicate is resolved.

It's just that constant annoying error indication that needs to be suppressed.

Workarounds, IntelliJ plugins, custom class loaders, etc. - all will be considered (with instructions).

Community
  • 1
  • 1
Myles Bennett
  • 533
  • 4
  • 12
  • The latest update to Studio (2.2) no longer reports this error. – Myles Bennett Oct 06 '16 at 14:51
  • The AS 2.3.3 does report it for me, but at least only over the `/test/` variant. Also everything works for me as expected (JUnit test using the duplicate variant, production using the correct `/main/` one). BTW, I did need just one boolean set to true/false differently for debug build type, when ran under tests (like using small part of release variant in debug, the release/debug code being done by `if (BuildConfig.DEBUG)` any way). Tried several different approaches, but there's no single elegant and simple, each feels like kind of hack. This "duplicate error" in the end looks as one of best. – Ped7g Jul 27 '17 at 18:03

1 Answers1

1

It is hardcoded: source

The only way is to change sources and build your own IntelliJ - but you will not be able to use Ultimate version.

Or making an agent to replace the method - HighlightClassUtil.checkDuplicateTopLevelClass.

This might come handy, so you can do it as a normal plugin: How can I add a Javaagent to a JVM without stopping the JVM?

Community
  • 1
  • 1
Meo
  • 12,020
  • 7
  • 45
  • 52