Search code examples
javaandroidandroid-build-flavors

Ressource conflict/not found with flavors in android studio


i am playing around with flavors in Android studio, i made one demo and one full flavor, the sole difference beeing the demo version having a copy of the activity_main.xml layout with an admod view in it,

the full version has not....

i read that having separate java class files is a bad idea, so i integrated the admob stuff in the main/.../src/java/.../MainActivity class:

if("free".equals(BuildConfig.FLAVOR))
{
  Log.d(TAG, "flavor: " + BuildConfig.FLAVOR);
  MobileAds.initialize(this, ADMOB_APP_ID);

  AdView mAdView;
  mAdView = findViewById(R.id.adView);
  AdRequest adRequest = new AdRequest.Builder()
      //.addTestDevice("151A597D874BD0B8D69D5D5E5B18B0E8")
  .build();
  mAdView.loadAd(adRequest);
}

and building the demo version goes flawlessly.... but, since the admob view id doesn't exist in the full version, well it doesn't build anymore....

quitting with a

 Error:(204, 34) error: cannot find symbol variable adView

so i am a bit at a loss how to solve that... all the examples about flavors i read through, adress the problem generally, but for solving specifically that sort of problem i couldn't find anything....


Solution

  • You must to use only one xml file (activity_main.xml) and use

    AdView mAdView;
    mAdView = findViewById(R.id.adView);    
    if("free".equals(BuildConfig.FLAVOR))
        {
          mAdView.setVisibility(View.VISIBLE);
          mAdView.loadAd(adRequest);
        }else{
          mAdView.setVisibility(View.GONE);
    }