Have a class be a NO-OP for any flavor other then a specific flavor

Viewed 29

What i want : I want to have some class say Example, which used in the main project, be a complete NO-OP when being built with any <build type><flavor> other then <build type>example, only when built with <build type>example flavors, it should have code that really does stuff.

What i tried:

I added an Example flavor to gradle:

    flavorDimensions "example"
    productFlavors {
        create("example") {
            dimension = "example"
        }
    }

Added source to the coresponding source set:

src->example->com.example->Example.java

Now i am stuck with how to add a NO-OP Example.java so that it will be used for any OTHER flavor. tried to add to

src->main->com.example->Example.java

But its considered redeclaration (since main is shared between all flavors). So how can it be done?

NOTE: the user of Example class should not care or be aware of weither Example class methods are complete NO-OPs or not, I want him to be able to have same minimal (without annoying if checks) source code be built succesfully with NO-OPs methods in <build type>example builds, and with real code in <build type><any flavor other then example> builds.

1 Answers

Step #0: Remove the no-op class from the main sourceset, if you still have it there

Step #1: Add another flavor

Step #2: Add your no-op class to the flavor added in Step #1

Step #3: Repeat steps #1 and #2 for any other flavors that you need

Related