I have an app that has 3 different flavors, full
, part1
and part2
.
All different flavors have different package names, so I can ship them as different apps.
Now I want that only part1
gets a menu item called Reload
. The other 2 flavors shouldn't have this menu item. Is this possible?
I tried the following with the menu resources:
app
|
+-src
|
+-full
|
+-main
| |
| +-res
| |
| +-menu
| |
| +-main_activity.xml
|
+-part1
| |
| +-res
| |
| +-menu
| |
| +-main_activity.xml
|
+-part2
Where main_activity.xml
for part1
is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_reload"
android:icon="@drawable/ic_reload"
android:title="@string/action_reload"
app:showAsAction="always"/>
</menu>
And main_activity.xml
for main
is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
</menu>
However, if I build the app in any other build variant than part1
, I get a compilation error in my MainActivity
where I need to react to the menu selection:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_reload: // Compile error: This item is not available
// TODO reload
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It's quite obvious why that is. But do you have any suggestion what the solution is to customize menus for different build flavors?
Create a MainActivity
in the main source folder where you handle the normal common code. Create another MainActivity
in the part1 source folder where you override onOptionsItemSelected
where it's not a problem to have references to R.id.action_reload
. That should work.