Android flavor - can't find character variable in R file

I have 2 options - paid and free. The only difference is that paidapp has another button on MainActivity , say "paidbutton" . The paid template has its own layout with the android:id="@+id/paidbutton" button android:id="@+id/paidbutton" , and the layout for freeapp does not have this button.

In code, I use this:

 if (BuildConfig.FLAVOR.equals("paidapp")) { View paidbutton = findViewById(R.id.paidbutton); paidbutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //... } }); } 

But I have an error cannot find symbol variable in findViewById(R.id.paidbutton);

How to solve this problem without cloning MainActivity.java in both variants?

EDIT1 - Add Code Examples:

Gradle:

 productFlavors { paidapp { } freeapp { } } 

/app/src/main/res/layout/activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/goNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go next" /> </LinearLayout> 

/app/src/paidapp/res/layout/activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/goNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go next" /> <Button android:id="@+id/paidbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="See more" /> </LinearLayout> 

/app/src/main/java/company/com/myapplication/MainActivity.java

 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(); } }); } } } 

EDIT2 . I found some workaround using reflection, but still looking for a normal solution:

instead of View paidbutton = findViewById(R.id.paidbutton); I used View paidbutton = findViewById(getIdFromRId("paidbutton"));

where getIdFromRId :

 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; } 
+6
source share
3 answers

If you do not have the paidbutton layout element in the paidbutton text, you can simply declare the corresponding id element in one of your xml resource files in the folder with the flavor of the freeapp product. For example, create a src/freeapp/res/values/ids.xml containing:

 <?xml version="1.0" encoding="utf-8"?> <resources> <item name="paidbutton" type="id"/> </resources> 

After that, you will not have problems compiling common code, as the R.id.paidbutton symbol will be defined for both products. findViewById() call will return the real layout element in paidapp build and return null in freeapp build.

+10
source

You need your if clause in create, and then add setContentView, so something like this:

 if (BuildConfig.FLAVOR.equals("paidapp")) { setContentView(R.layout.activity_main_paid); View paidbutton = findViewById(R.id.paidbutton); paidbutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //... } }); } else{ setContentView(R.layout.activity_main_free); } 

or you can use one layout for both and just make the button invisible

I hope this helps

+1
source

Can you put your paybutton XML tag and import the MainActivity.java class.

Perhaps you are using the wrong resource import, for example import android.R; instead of import my_package.R;

0
source

Source: https://habr.com/ru/post/979164/


All Articles