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?
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 {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With