Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Unit test (junit) Android Flavor/Variant - class does not exist

We have 2 product flavors in our app and one flavor has a class (FlavorSpecificClass) that the other does not. We have a junit test for FlavorSpecificClass in the src/test folder which will not compile when we are on our core variant because the class does not exist in that variant.

I cannot find a way to have separate unit tests per variant and the code will not compile in our core variant unless I comment out the references to FlavorSpecificClass. How can I get around this?

like image 903
DDM Avatar asked Jan 25 '17 19:01

DDM


People also ask

How to show build variant in Android Studio?

To change the build variant Android Studio uses, select Build > Select Build Variant in the menu bar. For projects without native/C++ code, the Build Variants panel has two columns: Module and Active Build Variant.

How to add new build variant android?

NOTE: By default, the Android Studio will generate "debug" and "release" Build Types for your project. So, to change a Build Type, all you need to do is just select your Build Type from the Build Variant and after the project sync, you are good to go.

How to create product flavors in Android Studio?

Creating Product Flavor is pretty much easy in Android Studio, we simply need to add productFlavors block inside the Android block in the application-level build. gradle file. It's that simple to create product flavors.


1 Answers

Assuming you are using gradle.

So for this you need to have another directory which rests in the same place as your 'test' directory. Name the directory as 'test[BuildVariant]' and have the tests for that build variant inside that directory.

Suppose you have built variant debug, Your directory structure would look something like this, if you want to test classes that are just in debug build variant

-debug/java/...
-main/java/...
-test/java/[your tests]
-testDebug/java/[your tests for build variant debug]
like image 50
Gaurav Vashisth Avatar answered Sep 21 '22 09:09

Gaurav Vashisth