PC パソコン

How to Fix Android Studio “Must be one of: … [WrongConstant]” for Valid Nested @IntDef Constants

Android Studio or Gradle Lint can report Must be one of: … [WrongConstant] even when a valid @IntDef constant is passed. This is a known AGP 9 Lint issue when constants are declared in a nested type. This guide covers the diagnostic, cause, affected versions, official fix, workarounds, and verification.

Representative log

src/main/java/com/example/Sample.kt:42: Error:
Must be one of: Mode.NONE, Mode.BASIC, Mode.ADVANCED [WrongConstant]

Cause

The defect prevents Lint from resolving some @IntDef constants declared inside nested classes or Kotlin objects. Google tracked it as Issue 478779827 and fixed it in AGP 9.2.0-alpha08. Stable AGP 9.2.0 and later include the fix.

Important: Not every WrongConstant is a false positive. First confirm that the supplied value is actually listed by the annotation.

Affected conditions and versions

  • Lint runs with an AGP 9 build from before the fix.
  • An argument or property is restricted by @IntDef.
  • Allowed constants live in a nested class, interface, or object.
  • Lint rejects a constant that is visibly present in the allowed set.

Confirm the false positive

object Mode {
    const val NONE = 0
    const val BASIC = 1
    const val ADVANCED = 2

    @IntDef(NONE, BASIC, ADVANCED)
    @Retention(AnnotationRetention.SOURCE)
    annotation class Value
}

fun configure(@Mode.Value mode: Int) { }
configure(Mode.BASIC)

If Mode.BASIC is flagged despite being declared in the annotation, the project matches the known bug. A raw integer or an unlisted constant may represent a genuine error.

Recommended fix

1. Upgrade to AGP 9.2.0 or later

plugins {
    id("com.android.application") version "9.2.0" apply false
    id("com.android.library") version "9.2.0" apply false
}

With a Version Catalog, update gradle/libs.versions.toml:

[versions]
agp = "9.2.0"

2. Align the compatible toolchain

Check the AGP 9.2 release notes and use compatible Gradle Wrapper, build JDK, and SDK versions. Updating Android Studio alone may not fix Gradle Lint when the project still pins an older AGP.

3. Rerun Lint cleanly

./gradlew --stop
./gradlew clean lint

If only CI fails, compare its resolved AGP, Gradle, and JDK versions with the local environment.

Temporary alternatives

  • Temporarily move the constants to a top-level or shallower declaration.
  • Add @SuppressLint("WrongConstant") only at a manually verified false-positive call site.
  • Record only this known false positive in a Lint baseline.
@SuppressLint("WrongConstant")
fun applyKnownValidMode() {
    configure(Mode.BASIC)
}

Do not disable the rule across the module. A broad suppression can hide real invalid integer values.

Verification

  1. Confirm that the resolved AGP version is 9.2.0 or newer.
  2. Run ./gradlew clean lint.
  3. Verify that the warning disappears from the valid constant.
  4. Temporarily pass an unlisted value and confirm that a genuine WrongConstant is still detected.

Notes

  • Similar diagnostics from @IntRange or resource annotations may have different causes.
  • A third-party custom Lint rule may require its own update.
  • There is no need to adopt the historical alpha build; use stable AGP 9.2.0 or a newer compatible version.

Summary

When a valid nested @IntDef constant triggers Must be one of: … [WrongConstant], an AGP 9 Lint false positive may be responsible. Validate the value, upgrade to AGP 9.2.0 or later, and rerun the Gradle Lint task. Keep any temporary suppression narrowly scoped.

Official references