Search code examples
androidxmladmobadsandroid-productflavors

How to only display ads in one version of your app


I am planning on an ads vs. ad-free version of my app. I already have two app flavors set up.

In my XML, this AdMob tutorial says I add this to my activity XML to hold the banner ad:

<com.google.android.gms.ads.AdView
        android:id="@+id/admob_adview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_footer" />

And then in the code, the tutorial says:

public class MainActivity extends AppCompatActivity {

    private AdView mAdMobAdView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdMobAdView = (AdView) findViewById(R.id.admob_adview);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("4DD0986B8BB49093161F4F00CF61B887")// Add your real device id here
                .build();
        mAdMobAdView.loadAd(adRequest);


        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), ActivityTwo.class));
            }
        });
    }
}

But if I only want this ad to display on one flavor vs. the other, what's the correct way to handle this without violating any rules? Normally flavors are checked via something like

if (BuildConfig.FLAVOR.equals("adfree")) {
    //no ads
}
else {
    //show ads
} 

Would I simply put the adRequest code in the above if-statement within my Activity? For the ad-free version, does it matter that the AdView is still in my XML, or do I need to use a separate copy of the XML with the AdView removed?


Solution

  • This answer is not specific to AdMob, and so there may be options or limitations tied to AdMob that render this analysis moot.

    However, IMHO, in the ad-free app, you want AdMob totally gone. Why would you bother having it in there, taking up space, possibly crashing with uninitialized views, etc.? It just seems like an unnecessary cost to you and your users.

    To do that (referring to your two flavors ad and addfree):

    Step #1: For AdMob-specific dependencies, use adCompile instead of compile

    Step #2: Have the layout resource(s) with the AdMob AdView in ad/src/res/layout/, with equivalents with the same names in adfree/src/res/layout (so you can redesign around having no ad taking up space)

    Step #3: Define an AdStrategy strategy class in both the ad/ and adfree/ source sets, with the same public API, where the ad/ one has method(s) tied to AdMob (such as that initialization code) and the adfree/ one just has empty methods

    Step #4: In MainActivity and anywhere else you interact with AdMob from Java, call the appropriate AdStrategy method, so in ad builds you get the AdMob stuff and in adfree builds it does nothing (or alternative logic, if appropriate)

    Now your adfree builds are truly AdMob-free, not just AdMob-uninitialized.