Search code examples
androidandroid-studioandroid-support-libraryandroid-lintandroid-studio-2.2

RequiresPermission annotation 'only specify one of value, anyOf or allOf'


Trying to apply the @RequiresPermission annotation as explained in tech-docs.

The example shows

If you require at least one from a set of permissions, you can use the anyOf attribute:

@RequiresPermission(anyOf = {
    Manifest.permission.ACCESS_COARSE_LOCATION,
    Manifest.permission.ACCESS_FINE_LOCATION})
public abstract Location getLastKnownLocation(String provider);

I try to apply it in my code, which does some bluetooth scanning:

@RequiresPermission(anyOf = {
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION})
private void initiateConnectionProcess() {
    startScanAndBroadcast();
}

which gives an error

Only specify one of value, anyOf or allOf

I thought maybe startScanAndBroadcast() has a allOf annotation that could cause problems if the annotations are resolved recursively, but that method has no annotations at all.

The error remains if the method body is blank.

The error goes away if I don't list the permissions:

@RequiresPermission(anyOf = { })
private void initiateConnectionProcess() {

What's going wrong here?


Solution

  • Update:
    Solved in Android Studio 2.2 preview 3

    After some checking, it seems that this error occurs in Android Studio 2.2 preview 2, but not in Android Studio 2.1.1.

    Even though it shows as an error, builds succeed and the annotation works properly.

    It appears this is a bug in the linter.
    Filed a bug report here.