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,398
  • 1
  • 15
  • 28

3 Answers3

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
  • 799
  • 6
  • 12
  • 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
  • 22,602
  • 12
  • 167
  • 196
-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
  • 16,948
  • 10
  • 63
  • 98
  • 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