silverFoxA
silverFoxA

Reputation: 4659

POPUPMENU icon is not getting displayed in released apk version

I'm following this tutorial and it works perfectly fine when i'm in debugging mode but when I generate the apk in release mode the icons won't work i mean those won't appear why??

Here is the link to the tutorial that i was following Tutorial here

custom_menu.xml

<?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/....."
    android:title="------"
    android:icon="....."
    app:showAsAction="always"/>
 <item android:id="@+id/....."
    android:title="------"
    android:icon="....."
    app:showAsAction="always"/>
 <item android:id="@+id/....."
    android:title="------"
    android:icon="....."
    app:showAsAction="always"/>
<item android:id="@+id/....."
    android:title="------"
    android:icon="....."
    app:showAsAction="always"/>
</menu>

And here is the java code

  View menuItemView = getActivity().findViewById(R.id.overflow);
            PopupMenu popupMenu = new PopupMenu(getActivity(), menuItemView);
            popupMenu.inflate(R.menu.custom_menu);
            //
            Object menuHelper;
            Class[] argTypes;
            Field fMenuHelper = null;
            try {
                fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
                fMenuHelper.setAccessible(true);
                menuHelper = fMenuHelper.get(popupMenu);
                argTypes = new Class[]{boolean.class};
                menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

Upvotes: 2

Views: 253

Answers (1)

Ivan
Ivan

Reputation: 6608

This happens because proguard obfuscates PopupMenu class name. To make icons work in the release apk, include the following code into your proguard.cfg file:

-keepclassmembernames class android.support.v7.widget.PopupMenu {
private android.support.v7.internal.view.menu.MenuPopupHelper mPopup; }

-keepclassmembernames class android.support.v7.internal.view.menu.MenuPopupHelper {
public void setForceShowIcon(boolean); }

Upvotes: 1

Related Questions