使用proguard时未显示自定义弹出菜单图标

时间:2016-06-22 11:12:41

标签: java android popup proguard

我已经为我的一个Android应用程序实现了带有图标的自定义弹出菜单。在这里,我设置的图标也应用了setForceShowIcon。这是以下代码

 final PopupMenu popupMenu = new PopupMenu(mContext, shareFeed);
            popupMenu.inflate(R.menu.custom_share_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();
            }
            final String des = map.get("title") + "\n" + map.get("description");
            final String message = Html.fromHtml(des).toString() + "\n" + mContext.getResources().getString(R.string.app_url);
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                        case R.id.whatsapp:
                            if (PerformCommonOps.internetConnection(mContext))
                                PerformCommonOps.shareFeedsOnWhatsapp(mContext, message);
                            else
                                Snackbar.make(coordinatorLayout, "Please ensure you have a working internet connection", Snackbar.LENGTH_SHORT).show();
                            return true;
                        case R.id.facebook:
                            if (PerformCommonOps.internetConnection(mContext))
                                PerformCommonOps.shareToFacebbok(mContext, mContext.getResources().getString(R.string.app_url), view, (Activity) mContext);
                            else
                                Snackbar.make(coordinatorLayout, "Please ensure you have a working internet connection", Snackbar.LENGTH_SHORT).show();
                            return true;
                        case R.id.text:
                            if (PerformCommonOps.internetConnection(mContext))
                                PerformCommonOps.sendText(mContext, message);
                            else
                                Snackbar.make(coordinatorLayout, "Please ensure you have a working internet connection", Snackbar.LENGTH_SHORT).show();
                            return true;
                        case R.id.googleplus:
                            if (PerformCommonOps.internetConnection(mContext))
                                PerformCommonOps.shareOnGooglePlus(mContext, Html.fromHtml(des).toString(), mContext.getResources().getString(R.string.app_url), view);
                            else
                                Snackbar.make(coordinatorLayout, "Please ensure you have a working internet connection", Snackbar.LENGTH_SHORT).show();
                            return true;
                        default:
                            common();
                            return true;
                    }

                }
            });
            popupMenu.show();

当我在调试模式中运行应用程序时显示图标,但在发布模式中使用 Proguard 时图标显示没有显示。 这是我setForceShowIcon

的计划配置
######################################
####### POPUpMenu configurations######
######################################
 -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); }

请让我知道哪里&我做错了什么。的 TIA

3 个答案:

答案 0 :(得分:1)

proguard-rules.pro中的这一行解决了我的问题:

-keep class android.support.v7.widget.** { *; }

答案 1 :(得分:0)

由于您在自定义弹出类中使用反射,因此需要避免使用反射。

-keep public class * extends com.yoursite.android.yourappname.YourClassName

-keepclassmembers class * extends com.yoursite.android.yourappname.YourClassName{
 public <init>(android.content.Context);
}

答案 2 :(得分:0)

由于setForceShowIconandroid.support.v7.view.menu.MenuPopupHelper我的成员,一般来说,将这些行添加到我的proguard规则中并且效果非常好:

-keep class * extends android.support.v7.** { *; }
-keepclassmembernames class * extends android.support.v7.** { *; }