4

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?

Tim
  • 41,901
  • 18
  • 127
  • 145

2 Answers2

2

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.

Tim
  • 41,901
  • 18
  • 127
  • 145
  • If you haven't done so already, [file a bug report](http://b.android.com), as the tools team is comparatively responsive to bug reports. – CommonsWare Jun 02 '16 at 11:32
  • @CommonsWare Thanks. I was waiting for some 'can/cannot reproduce' feedback before doing that. It seems like a bug but I'm not sure yet – Tim Jun 02 '16 at 11:34
  • FWIW, I can confirm that AS 2.1.1 does not have a problem with your sample code. – CommonsWare Jun 02 '16 at 11:49
  • @CommonsWare FYI the team never responded to my bug report, but it is solved in studio 2.2 preview 3 :-) – Tim Jun 09 '16 at 09:21
1

I'm assuming you figured this out by now, but you have to use square brackets instead of curly brackets.

Example:

@RequiresPermission(anyOf = [ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION])

I use Kotlin though and the latest version of Android Studio. I see some mentioned it was fixed, but it is not fixed as far as I can tell.

Android Studio Version

enter image description here

Curly Brackets Curly Brackets Broken

Square Brackets Square Brackets Working

Sam
  • 5,342
  • 1
  • 23
  • 39