Duplicate class found, when trying to use a class that is specific to testing

334 views
Skip to first unread message

Kaushik Gopal

unread,
Apr 23, 2014, 11:33:51 PM4/23/14
to adt...@googlegroups.com
I'm using gradle for my android build process. Is it possible to have a specific version of a class in the androidTest folder, for the test environment alone?

I know you cannot have classes at a different build levels i.e. we cannot have the same class across a "buildType" (debug/release), "productFlavor"(flavor1, flavor2, default) or "source set"(main/androidTest) so:

- debug + flavor1 = not allowed
- flavor1 + main = not allowed
- main + debug = not allowed

- flavor1 + flavor2 = allowed
- debug + release = allowed

so my expectation would be that

- main + androidTest = allowed (because they are at the same build level i.e. source set)

But this does not seem to be the case. I get a duplicate class file. Can someone let me know of a way to achieve this?

My reasoning as to why I would want this is because, for testing alone i want to inject mock modules, based on a strategy recommended here.

Xavier Ducrohet

unread,
Apr 24, 2014, 12:55:27 AM4/24/14
to adt...@googlegroups.com
Is this a library project?

For library project, the code of the library is packaged with the code of the test in a single apk, so you cannot have the same class twice.

For app project, when loading the test apk, the Android runtime will complain if it finds the same class in both the tested app and the test app so you cannot do that either.


--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Kaushik Gopal

unread,
Apr 24, 2014, 3:04:47 AM4/24/14
to adt...@googlegroups.com

Ah.. this isn't a library project. Its definitely an app project.

So am I right in understanding that we can't have two versions of the same class - one in the main source set and the other in the androidTest source set?

Is there an alternative strategy then for having "test" specific versions of a class file (as we have in the case of different product flavors ?)

You received this message because you are subscribed to a topic in the Google Groups "adt-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adt-dev/RY7J8w6QOG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adt-dev+u...@googlegroups.com.

Jake Wharton

unread,
Apr 24, 2014, 3:24:15 AM4/24/14
to adt...@googlegroups.com
The easiest way that we've found to have test specific classes is to add another build type and change the instrumentation tests to use it instead of debug or release.

Jake Wharton

unread,
Apr 24, 2014, 3:25:33 AM4/24/14
to adt...@googlegroups.com
And to specifically answer your question, no you cannot duplicate a class in the app and the test app. The device loads the classes from both APKs inside the same process using the same class loader when invoking and running your tests.x

Kaushik Gopal

unread,
Apr 24, 2014, 3:34:30 AM4/24/14
to adt...@googlegroups.com
Thanks Jake. That makes sense. 

Also, I like the workaround for the build type. pretty slick!
Reply all
Reply to author
Forward
0 new messages