1

I have created my own extension function that checks if the sdk is at least lollipop

inline val buildIsLollipopAndUp: Boolean
    get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP

However, if I use this, the lint tools can't figure out that I've verified my api level and still gives me new api warnings. Is there anyway to make those warning disappear and have the lint tools recognize my check?

A simple example:

if (buildIsLollipopAndUp) ripple()
else fade()

Where ripple calls on the circular animation that is only available for lollipop and up, and fade is the default animation.

My full example:

@SuppressLint("NewApi")
@KauUtils fun View.circularReveal(x: Int = 0, y: Int = 0, offset: Long = 0L, radius: Float = -1.0f, duration: Long = 500L, onStart: (() -> Unit)? = null, onFinish: (() -> Unit)? = null) {
    if (!isAttachedToWindow) {
        onStart?.invoke()
        visible()
        onFinish?.invoke()
        return
    }
    if (!buildIsLollipopAndUp) return fadeIn(offset, duration, onStart, onFinish)

    val r = if (radius >= 0) radius
    else Math.max(Math.hypot(x.toDouble(), y.toDouble()), Math.hypot((width - x.toDouble()), (height - y.toDouble()))).toFloat()

    val anim = ViewAnimationUtils.createCircularReveal(this, x, y, 0f, r).setDuration(duration)
    anim.startDelay = offset
    anim.addListener(object : AnimatorListenerAdapter() {
        override fun onAnimationStart(animation: Animator?) {
            visible()
            onStart?.invoke()
        }

        override fun onAnimationEnd(animation: Animator?) = onFinish?.invoke() ?: Unit
        override fun onAnimationCancel(animation: Animator?) = onFinish?.invoke() ?: Unit
    })
    anim.start()
}

Note that I need the lint suppressor so as to avoid lint warnings

Allan W
  • 2,791
  • 4
  • 23
  • 41
  • its not a function, and its also not an extension function. its actually a property with a `getter`. can you show code how you are using it? – Les Jul 23 '17 at 01:25
  • @Les oops my mistake. I had it in a file with my extensions and I forgot that this doesn't count as one. I've added an example usage, but it's basically the same as replacing `Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP` with `buildIsLollipopAndUp` – Allan W Jul 23 '17 at 03:18

1 Answers1

0

It looks like lint doesn't know how to expand the inline Kotlin variable in place. I didn't check if it expands for Java. Will do so later.

Also noted...

    if(!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) return
    val anim = ViewAnimationUtils.createCircularReveal(this, x, y, 0f, r).setDuration(duration)

...will not suppress lint. But...

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    val anim = ViewAnimationUtils.createCircularReveal(this, x, y, 0f, r).setDuration(duration)

...will. Again, I did not check if the same is true for Java.

So, I doubt this is a Kotlin/Java issue. I think it is a lint issue.

Les
  • 10,335
  • 4
  • 40
  • 60