Support v7 MenuPopupHelper is now hidden and restricted to LIBRARY_GROUP

Viewed 2981

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 ?

3 Answers

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")

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.

Related