title: Fragment inflation from different flavors
body: <p>I am learning Android and I cannot find the solution to a fragment instantiation error when they are in different flavors.</p>
  
  <p>Setup:</p>
  
  <ul>
  <li>I have a main fragment that is loaded into the main activity.</li>
  <li>I have two favors: paid and free.</li>
  <li>The main (common) part of the app doesn't have the fragment (layout + java files), I put them into the favors.</li>
  </ul>
  
  <p>I get the following error when I try to run my program:</p>
  
  <pre><code>01-24 19:38:06.211 27417-27417/com.udacity.gradle.builditbigger.paid E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.udacity.gradle.builditbigger.paid, PID: 27417
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.udacity.gradle.builditbigger.paid/com.udacity.gradle.builditbigger.MainActivity}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
      at android.os.Handler.dispatchMessage(Handler.java:105)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6541)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
  Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
  Caused by: java.lang.NullPointerException
      at java.lang.VMClassLoader.findLoadedClass(Native Method)
      at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
      at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:499)
      at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3637)
      at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
      at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
      at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
      at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
      at com.udacity.gradle.builditbigger.MainActivity.onCreate(MainActivity.java:14)
      at android.app.Activity.performCreate(Activity.java:6975)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
      at android.os.Handler.dispatchMessage(Handler.java:105)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6541)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  </code></pre>
  
  <p>I have tried many different versions of the fragment name because the error message tells me that the problem is with that, unfortunately none of them worked. Could you please help me to find the route cause of the issue (I mean not just the solution but why doesn't this work)? <a href="https://github.com/judit-juhasz/flavour-sandbox" rel="nofollow noreferrer">The code can be found on GitHub</a>, but the relevant files are here:</p>
  
  <p><strong>src/java/com/udacity/gradle/builditbigger/MainActivity.java</strong></p>
  
  <pre><code>package com.udacity.gradle.builditbigger;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  
  public class MainActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  public void tellJoke(View view) {
      new EndpointsAsyncTask().execute(this);
  }
  }
  </code></pre>
  
  <p><strong>src/res/layout/activity_main.xml</strong></p>
  
  <pre><code>&lt;fragment xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/fragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:layout="@layout/fragment_main" /&gt;
  </code></pre>
  
  <p><strong>src/free/java/com/udacity/gradle/builditbigger/free/MainActivityFragment.java</strong></p>
  
  <pre><code>package com.udacity.gradle.builditbigger.free;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.google.android.gms.ads.AdRequest;
  import com.google.android.gms.ads.AdView;
  import com.udacity.gradle.builditbigger.R;
  
  
  /**
   * A placeholder fragment containing a simple view.
   */
  public class MainActivityFragment extends Fragment {
  
  public MainActivityFragment() {
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View root = inflater.inflate(R.layout.fragment_main, container, false);
  
      AdView mAdView = (AdView) root.findViewById(R.id.adView);
      // Create an ad request. Check logcat output for the hashed device ID to
      // get test ads on a physical device. e.g.
      // "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
      AdRequest adRequest = new AdRequest.Builder()
              .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
              .build();
      mAdView.loadAd(adRequest);
      return root;
  }
  }
  </code></pre>
  
  <p><strong>src/free/res/layout/fragment_main.xml</strong></p>
  
  <pre><code>&lt;RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.udacity.gradle.builditbigger.MainActivity"&gt;
  
  &lt;TextView
      android:id="@+id/instructions_text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/instructions" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/instructions_text_view"
      android:onClick="tellJoke"
      android:text="@string/button_text" /&gt;
  
  &lt;com.google.android.gms.ads.AdView
      android:id="@+id/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_ad_unit_id" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>src/paid/java/com/udacity/gradle/builditbigger/paid/MainActivityFragment.java</strong></p>
  
  <pre><code>package com.udacity.gradle.builditbigger.paid;
  
  import android.support.v4.app.Fragment;
  
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.udacity.gradle.builditbigger.R;
  
  
  /**
   * A placeholder fragment containing a simple view.
   */
  public class MainActivityFragment extends Fragment {
  
  public MainActivityFragment() {
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View root = inflater.inflate(R.layout.fragment_main, container, false);
      return root;
  }
  }
  </code></pre>
  
  <p><strong>src/paid/res/layout/fragment_main.xml</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.udacity.gradle.builditbigger.MainActivity"&gt;
  
  &lt;TextView
      android:id="@+id/instructions_text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/instructions" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/instructions_text_view"
      android:onClick="tellJoke"
      android:text="@string/button_text" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>