title: Android Studio build flavors - How to have same source files in diverse flavors
body: <p>I need to create a demo flavor in android studio for an app. In my app level gradle file i have created another flavor called demo and the default flavor of full of course. It looks like this:</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 22
      buildToolsVersion "21.1.2"
  
      defaultConfig {
          applicationId "com.example.uen229.myapplication"
          minSdkVersion 17
          targetSdkVersion 22
          versionCode 1
          versionName "1.0"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  
      productFlavors {
          demo {
              applicationId "com.buildsystemexample.app.demo"
              versionName "1.0-demo"
          }
          full {
              applicationId "com.buildsystemexample.app.full"
              versionName "1.0-full"
          }
      }
  
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:22.2.0'
  }
  </code></pre>
  
  <p>and here is a image of my project structure in which I have created a demo flavor  directory:</p>
  
  <p><a href="https://i.stack.imgur.com/yII8W.png" rel="noreferrer"><img src="https://i.stack.imgur.com/yII8W.png" alt="enter image description here"></a></p>
  
  <p>Now onto the issue.  I have two classes called Hello.java.  Both are in there respective flavors and print different things. I'll show you both files now:</p>
  
  <pre><code>import android.util.Log;
  
  /** this is from demo flavor directory**/
  
  public class Hello {
  
      Hello(){
  
          Log.v("","hello from demo");
      }
  
      public String getName();
          return "im from demo";
  
      };
  
  }
  </code></pre>
  
  <p>And here is the other Hello:</p>
  
  <pre><code>package com.example.uen229.myapplication;
  
  import android.util.Log;
  
  
  /** this is from full or main flavor directory**/
  public class Hello {
  
  
      Hello(){
  
          Log.v("", "hello from main");
      }
  
      public String getName(){
  
          return "im from main";
  
      };
  }
  </code></pre>
  
  <p>notice how the first hello.java does not have  package, even if i had a package the IDE wont compile. look at this photo:</p>
  
  <p><a href="https://i.stack.imgur.com/5e4lV.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5e4lV.png" alt="enter image description here"></a></p>
  
  <p>Now finally lets look at mainActivity.java to see that when i switch build variants it only does a toast for "im from main" but i need it to print 'im from demo" if i use the demoDebug build variant. If i switch the build variant to demoDebug it still prints "im from main". can anyone help :</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Hello h = new Hello();
          Toast.makeText(this, h.getName(), Toast.LENGTH_LONG).show();
  
      }
  }
  </code></pre>
  
  <h2>UPDATE</h2>
  
  <p>From stackoverflow it says:</p>
  
  <blockquote>
    <p>If you want to have a different version of the same class in the two
    flavor you'll need to create it in both flavors.</p>
  
  <pre><code>src/flavor1/java/com/foo/A.java
  
  src/flavor2/java/com/foo/A.java
  </code></pre>
    
    <p>And then your code in <code>src/main/java</code> can do:</p>
  
  <pre><code>import com.foo.A
  </code></pre>
    
    <p>depending on the flavor selected, the right version of <code>com.foo.A</code> is
    used.</p>
  </blockquote>
  
  <p>This is what I want to accomplish with the Hello class</p>