MaterialDrawerKt icon indicating copy to clipboard operation
MaterialDrawerKt copied to clipboard

Add resource annotations

Open zsmb13 opened this issue 8 years ago • 1 comments
trafficstars

This is something that's included (at least in parts) in the base library, and it would be nice to have here.

~~This issue will have to be dealt with once Android Studio 3.0 is stable, since as of Canary 9, checks for these annotations don't seem to work when calling the annotated methods from Kotlin.~~

  • Update: Android Studio 3 is now stable, but the issue still remains, misusing properties that have these annotations doesn't produce any lint warnings.

  • Update 2: here is an unanswered StackOverflow issue for the same problem.


Example of what needs to be done:

var background: Int
    @Deprecated(...)
    get() = nonReadable()
    set(@DrawableRes value) { // <- resource annotation
        builder.withHeaderBackground(value)
    }

Example of what Studio should mark as an error:

accountHeader {
    background = R.drawable.header // this is ok, this is the proper usage
    background = 1234 // should warn for this, but doesn't
}

Example of what Studio marks as a warning in Java as of 3.0 Canary 9:

AccountHeaderBuilderKt builder = new AccountHeaderBuilderKt(this);
builder.setBackground(25124); // Error: Expected resource of type drawable

zsmb13 avatar Aug 07 '17 17:08 zsmb13

Another update: as of AS 3.1 Beta 1, Java annotations generate lint warnings at Kotlin use sites, but resource annotations in Kotlin code still have no effect.

Example:

public T withDescription(@StringRes int descriptionRes) { ... }
public var descriptionRes: Int
        get() = ...
        set(@StringRes value) { ... }

image

Even though according to the decompiled bytecode of the Kotlin property, the annotations is applied properly:

public final void setDescriptionRes(@StringRes int value) { ... }

Seems like this only an issue with property setters, as function parameters annotated with these annotations in Kotlin code work as expected, for example, this generates lint warnings for hardcoded int values:

fun test(@StringRes value: Int) { ... }

zsmb13 avatar Feb 06 '18 16:02 zsmb13