Home » Android » @StringRes, @DrawableRes, @LayoutRes and so on android annotations lint check with kotlin parameters

@StringRes, @DrawableRes, @LayoutRes and so on android annotations lint check with kotlin parameters

Posted by: admin May 14, 2020 Leave a comment

Questions:

assuming you have such data class with default parameters

data class Info(
        @DrawableRes
        val iconRes: Int = 0,
        @StringRes
        val stringRes: Int = 0,
        @LayoutRes
        val layoutRes: Int = 0)

so you can create this data-class like

    val data = Info(
        iconRes = R.drawable.icon, 
        stringRes = R.string.text,
        layoutRes = R.layout.layout)

in this case there isn’t any lint error

But when i try to use default parameters a lint error is occured:

val data = Info(
        //here is expected resource of type drawable error
        stringRes = R.string.text
        layoutRes = R.layout.layout)

Seems that it uses position in parameters, not the exact type & name.

I’ve tried to use explicit annotation like @param:DrawableRes, but with the same result.

Can i somehow solve this problem? I use latest Android Studio 3.0.

How to&Answers:

It was an issue in Android Studio and was fixed in 3.1. Checked on 3.1-beta4