Suleyman
Suleyman

Reputation: 2943

Handling minor differences in build variants

I have an application with 2 product flavors and as a result 4 build variants:

1) freeDebug
2) freeRelease
3) paidDebug
4) paidRelease

I have a Fragment that contains an ad, and it takes 2 lines to implement:

@Nullable @BindView (R.id.bottomsheet_ad) AdView mAdView;
mAdView.loadAd(adRequest);

So because of 2 lines, I have to potentially maintain 2 or more files.

I'm considering 2 solutions:

1) Check flavor at runtime:

if (BuildConfig.FLAVOR.equals("flavor123")) {
    ...
}

2) Create a common flavor and point needed variants in gradle, as explained here:

android {
    ...
    productFlavors {
        flavorOne {
            ...
        }
        flavorTwo {
            ...
        }
        flavorThree {
            ...
        }
        flavorFour {
            ...
        }
    }
    sourceSets {
        flavorOne.java.srcDir 'src/common/java'
        flavorTwo.java.srcDir 'src/common/java'
        flavorThree.java.srcDir 'src/common/java'
    }
}

Which solution would be better? And is checking flavor at runtime as above considered polluting the code?

Upvotes: 0

Views: 154

Answers (1)

John O'Reilly
John O'Reilly

Reputation: 10330

You can add something like following to appropriate flavor in your build.gradle

buildConfigField "boolean", "showAds", 'true'

And then use like following (your main src files will still be used for additional flavors you add):

if (BuildConfig.showAds) {

}

Upvotes: 4

Related Questions