Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequiresPermission annotations with "allOf" in Kotlin

Tags:

android

kotlin

In Kotlin, I'd like to add a method annotation that's equivalent to this RequiresPermission annotation in Java, indicating that multiple permissions are required:

@RequiresPermission(allOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
public Location getLocation() {
    // ...
}

How can I write this annotation in Kotlin?

like image 275
rmtheis Avatar asked Jun 20 '17 03:06

rmtheis


1 Answers

You can pass in an array of items as an annotation parameter with arrayOf:

@RequiresPermission(allOf = arrayOf(ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION))
fun getLocation(): Location {
    // ...
}

You can actually get to this solution by just pasting your Java code into a Kotlin file Android Studio as well.

Update: since Kotlin 1.2, you can use an array literal syntax as well:

@RequiresPermission(allOf = [ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION])
fun getLocation(): Location {
    // ...
}
like image 176
zsmb13 Avatar answered Oct 22 '22 20:10

zsmb13