19

Recently i've been getting a lint error on my usage of android.support.v7.view.menu.MenuPopupHelper which is now hidden and restricted to be only used within its library group.

Exact message:

MenuPopupHelper constructor can only be called from within the same library group (groupId=com.android.support)

Excerpt from the MenuPopupHelper.java class:

/**
 * Presents a menu as a small, simple popup anchored to another view.
 *
 * @hide
 */
@RestrictTo(LIBRARY_GROUP)
public class MenuPopupHelper implements MenuHelper {

Question: Any idea when and why this happened ? or whats the workaround that I should look for ?

MiaN KhaLiD
  • 1,478
  • 1
  • 16
  • 28

4 Answers4

0

I just found out here that this is a bug in the pre-release version of the tool.

If you want a project wide workaround, put the snippet below in the build.gradle file of your project

android {
  lintOptions {
    disable 'RestrictedApi'
  }
}

OR
use the annotation below to suppress the lint warning for that particular method or class

@SuppressLint("RestrictedApi")
Wilson
  • 1,259
  • 11
  • 17
  • Can you please add some information/link supporting `I just found out this is a bug in the pre-release version of the tool.` – JaydeepW Mar 13 '19 at 12:40
0

I had an error in Lint report:

Error: MenuPopupHelper.show can only be called from within the same library group prefix (referenced groupId=androidx.appcompat with prefix androidx from groupId=MyProject) [RestrictedApi] }.show()

If you don't use icons in PopupMenu, you can remove MenuPopupHelper:

Before:

val popupMenu = PopupMenu(context, v)
...
val menuHelper = MenuPopupHelper(context, popupMenu.menu as MenuBuilder, v)
menuHelper.gravity = Gravity.END
menuHelper.show()

After:

val popupMenu = PopupMenu(context, v)
...
popupMenu.gravity = Gravity.END
popupMenu.show()

Or add @SuppressLint("RestrictedApi") annotation.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
0

I had the same problem when I migrated to AndroidX and I replaced the following classes:
android.support.v7.widget.PopupMenu
android.support.v7.view.menu.MenuPopupHelper
with the new ones:
androidx.appcompat.widget.PopupMenu
androidx.appcompat.view.menu.MenuPopupHelper
and I obtained the same lint error.
After looking to the reference docs, I discovered that now I can obtain the same behavior using just PopupMenu, without the need of MenuPopupHelper, even if my popup menu has icons.

 PopupMenu popup = new PopupMenu(getActivity(), view);
            popup.getMenuInflater().inflate(R.menu.opzioni_programma, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_programma_calendar:
                            // menu item logic
                        case R.id.action_programma_search:
                            // menu item logic
                        default:
                            return false;
                    }
                }
            });
            popup.setForceShowIcon(true);
            popup.show();
Gio
  • 94
  • 5
-2

Try using android.support.v7.widget.PopupMenu instead:

PopupMenu popup = new PopupMenu(v.getContext(), v);
popup.inflate(R.menu.mymenu);
//or
//popup.getMenuInflater().inflate(R.menu.mymenu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {
        final int itemId = item.getItemId();
        switch (itemId) {
            case R.id.someid:
                //do something
                return true;
            default:
                return false;
        }
    }
});
popup.show();
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • 8
    AFAIK PopMenu doesn't allow you to add icons to menu rows, which is why we switched to `MenuPopupHelper` in the first place. – MiaN KhaLiD Jul 12 '17 at 12:35