title: Android flavor - cannot find symbol variable in R file
body: <p>I have 2 flavors - paidapp and freeapp.
  The only difference that paidapp have 1 more button on <code>MainActivity</code>, say <code>"paidbutton"</code>.
  paidapp has its own layout with button <code>android:id="@+id/paidbutton"</code> and layout for freeapp does not have this button.</p>
  
  <p>In code I use this:</p>
  
  <pre><code>if (BuildConfig.FLAVOR.equals("paidapp")) {
              View paidbutton = findViewById(R.id.paidbutton);
              paidbutton.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      //...
                  }
              });
  }
  </code></pre>
  
  <p>But I have an error <code>cannot find symbol variable</code> in <code>findViewById(R.id.paidbutton);</code></p>
  
  <p>How to solve this problem without cloning MainActivity.java in both flavors?</p>
  
  <p><strong>EDIT1</strong> - add more code examples:</p>
  
  <p>Gradle:</p>
  
  <pre><code>productFlavors {
      paidapp {
      }
      freeapp {
      }
  }
  </code></pre>
  
  <p>/app/src/main/res/layout/activity_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;Button
          android:id="@+id/goNext"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Go next" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>/app/src/paidapp/res/layout/activity_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;Button
          android:id="@+id/goNext"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Go next" /&gt;
  
      &lt;Button
          android:id="@+id/paidbutton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="See more" /&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>/app/src/main/java/company/com/myapplication/MainActivity.java</p>
  
  <pre><code>package company.com.myapplication;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Toast;
  
  
  public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          View goNext = findViewById(R.id.goNext);
          goNext.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Toast.makeText(MainActivity.this, "goNext click", Toast.LENGTH_SHORT).show();
              }
          });
  
          if (BuildConfig.FLAVOR.equals("paidapp")) {
              View paidbutton = findViewById(R.id.paidbutton);
              paidbutton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Toast.makeText(MainActivity.this, "paidapp click", Toast.LENGTH_SHORT).show();
                  }
              });
          }
      }
  
  }
  </code></pre>
  
  <p><strong>EDIT2</strong> - I have found some workaround using reflection, but still looking for normal solution:</p>
  
  <p>instead of <code>View paidbutton = findViewById(R.id.paidbutton);</code> I have used <code>View paidbutton = findViewById(getIdFromRId("paidbutton"));</code></p>
  
  <p>where <code>getIdFromRId</code> is:</p>
  
  <pre><code>private int getIdFromRId(String idName) {
      int id = 0;
      try {
          Class c = R.id.class;
          Field field = c.getField(idName);
          id = field.getInt(null);
      } catch (Exception e) {
          // do nothing
      }
      return id;
  }
  </code></pre>