TargetAPI lint check produces wrong results

430 views
Skip to first unread message

Mika

unread,
Jun 21, 2022, 12:07:30 PM6/21/22
to lint-dev
Hi,

There are some weird cases with the TargetAPI lint checks. See this example

class MainActivity : Activity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setTranslucent(false) // Lint error, requires API level R

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
      setTranslucent(false) // Lint ok
    }

    if (BuildUtils().sdkInt() >= Build.VERSION_CODES.R) {
      setTranslucent(false) // Lint error, but it shouldn't be
    }

    if (BuildUtils().foo()) {
      setTranslucent(false) // Lint ok
    }

    if (BuildUtils().bar()) {
      setTranslucent(false) // Lint error, but it shouldn't be
    }

    if (BuildUtils().hasR()) {
      setTranslucent(false) // Lint ok
    }

    if (BuildUtils().hasS()) {
      setTranslucent(false) // Lint ok, but it shouldn't be
    }

    if (BuildUtils().hasN()) {
      setTranslucent(false) // Lint error, but is shouldn't be
    }
  }
}

class BuildUtils {

  fun sdkInt(): Int {
    return Build.VERSION.SDK_INT
  }

  fun foo(): Boolean {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
  }

  fun bar(): Boolean {
    return sdkInt() >= Build.VERSION_CODES.R
  }

  fun hasR(): Boolean {
    return sdkInt() >= Build.VERSION_CODES.R
  }

  fun hasS(): Boolean {
    return Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
  }

  fun hasN(): Boolean {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
  }
}

If I name a function as has<VERSION_CODE> then the lint check seems to pass no matter what is the content of that function. Is this a bug or intended behavior? I googled and tried to look documentation and https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ApiDetector.java source but I couldn't find anything about has<VERSION_CODE> function names being special. 

I tried this by creating a simple empty activity project, setting min api level to LOLLIPOP. 

Run in:

Android Studio Chipmunk | 2021.2.1 Patch 1
Build #AI-212.5712.43.2112.8609683, built on May 18, 2022
Runtime version: 11.0.12+0-b1504.28-7817840 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.4
GC: G1 Young Generation, G1 Old Generation
Memory: 4096M
Cores: 8
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: com.intellij.plugins.emacskeymap (212.4535.15), name.kropp.intellij.makefile (212.5712.51), org.intellij.plugins.markdown (212.5457.16), org.jetbrains.kotlin (212-1.6.21-release-334-AS5457.46), JProfiler (19.3)

-Mika


Reply all
Reply to author
Forward
0 new messages