Category: Blog, Android, Development

How to make generated Java test sources recognizable by IDE

The background

Starting from Android Gradle plugin 2.2 built-in configuration for annotation processors is available. Previously that functionality has been provided by 3rd party project android-apt

What is that configuration and why it is useful? All that is explained in another blog post: Android Gradle configurations.

The problem

There is a little issue with new configuration. Namely files placed in test source sets are not added to Java model thus they are not recognized by IDE (Android Studio) and marked red however they are normally compilable.

When generated test sources would be needed? Examples includes Dagger (test-only components) and Robolectric shadows (resetters).

I’ve raised an issue on Android Issue Tracker but unfortunately it is not yet fixed at the time of writing (over 4 months after raising).

The solution

Workaround is pretty much easy. We just need to call addJavaSourceFoldersToModel on relevant (usually all) build variants with appropriate argument. Here is a complete snippet for build.gradle:

For library projects replace applicationVariants with libraryVariants. Voilà!

About the author

Karol Wrótniak

Karol Wrótniak

Mobile Developer

Flutter & Android Developer with 12 years of experience. A warhorse with impressive experience and skills in native and Flutter app development. Karol is probably the most active contributor to open source libraries you've ever met. He develops Gradle plugins and Bitrise steps, and he is engaged in many projects, in particular those related to testing.

Karol has been engaged as a speaker in many events and meetups like DevFest, 4Developers Wrocław, JDD Conference, Linux Academy, and more. He is an active member of Google Developers Group Wrocław, Flutter Wrocław, and Bitrise User Group.