Anthony
Anthony

Reputation: 686

Share test classes between modules

I would like to be able to have common test code in a library module of my android projects, so that the different apps in the project can use them.

The problem is, apps cannot import classes from <library>/src/androidTest/java, and if I move that at code into src\main, it would have to move dependencies from androidTestCompile to compile (more dependencies to the release build).

The only solution right now is to create a separate library to hold the shared test classes, however this has the downside of adding a new library to the project structure, which is not that big a deal, but I'd like to know nonetheless if there are better solutions.

I'd rather implement a Gradle hack at this point if any Gradle (Android plugin) wizards out there can help me find one.

Upvotes: 9

Views: 3526

Answers (2)

Bob Liberatore
Bob Liberatore

Reputation: 886

If you have code (test or otherwise) that can be reused across multiple modules, the appropriate thing to do is exactly what you've done: put it in it's own module and import it into the other modules.

The small overhead of creating a new module is giving you a lot of power. It allows you to manage its build without having to change every dependent module's build.

Any other option I've tried (creating a single module that contains all modules' tests, e.g.) ends up being a much bigger headache and a dependency nightmare.

Upvotes: 0

Anthony
Anthony

Reputation: 686

Since I got no answers, I might as well answer my own question.

I ended up using the solution I already mentioned in my question:

Create a library with shared test classes (not actual test cases, but common code to be used in the final ones) under src/main and import it with androidTestCompile or androidTestImplementation for recent gradle versions.

Got a solution that doesn't involve adding a module? I won't accept my own answer since it doesn't exactly answer the question.

Upvotes: 5

Related Questions