permissions-dispatcher/PermissionsDispatcher · error · WrongClassException

'${TypeName.get(type)}' can't be annotated with '@RuntimePer

Error message

'${TypeName.get(type)}' can't be annotated with '@RuntimePermissions'

What it means

This error comes from the PermissionsDispatcher annotation processor's Validators.kt:27. The processor scans an element annotated with @RuntimePermissions (or sibling annotations) and tries to find a matching ProcessorUnit for the element's type; when no processor unit's target type is a supertype of the annotated type, it throws WrongClassException with this message. It means the annotation was placed on a kind of element the library cannot process (typically not a class, or a type the processor does not recognize).

Source

Thrown at processor/src/main/kotlin/permissions/dispatcher/processor/util/Validators.kt:27

import javax.lang.model.element.Element
import javax.lang.model.element.ExecutableElement
import javax.lang.model.element.Modifier
import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeMirror

private const val WRITE_SETTINGS = "android.permission.WRITE_SETTINGS"
private const val SYSTEM_ALERT_WINDOW = "android.permission.SYSTEM_ALERT_WINDOW"

/**
 * Obtains the [ProcessorUnit] implementation for the provided element.
 * Raises an exception if no suitable implementation exists
 */
fun <K> findAndValidateProcessorUnit(units: List<ProcessorUnit<K>>, element: Element): ProcessorUnit<K> {
    val type = element.asType()
    try {
        return units.first { type.isSubtypeOf(it.getTargetType()) }
    } catch (ex: NoSuchElementException) {
        throw WrongClassException(type)
    }
}

/**
 * Checks the elements in the provided list annotated with an annotation against duplicate values.
 * <p>
 * Raises an exception if any annotation value is found multiple times.
 */
fun <A : Annotation> checkDuplicatedValue(items: List<ExecutableElement>, annotationClass: Class<A>) {
    val allItems: HashSet<List<String>> = hashSetOf()
    items.forEach {
        val permissionValue = it.getAnnotation(annotationClass).permissionValue().sorted()
        if (allItems.contains(permissionValue)) {
            throw DuplicatedValueException(permissionValue, it, annotationClass)
        } else {
            allItems.add(permissionValue)
        }
    }

View on GitHub (pinned to 74b532bf1f)

Solutions

  1. Move @RuntimePermissions onto a supported class type (an Activity or Fragment class declaration, not an interface or enum).
  2. Verify the element carrying the annotation is the class itself, not a companion object or nested type.
  3. Update kapt/annotation-processor and library versions so the processor's target types match your class hierarchy.
  4. Check that the class is in a module where the annotation processor is correctly configured (kapt/ksp dependencies present).

Example fix

// before
@RuntimePermissions
interface MyFragmentInterface { }

// after
@RuntimePermissions
class MyFragment : Fragment() { }
Defensive patterns

Strategy: validation

Validate before calling

val el: Element = ...
require(el.kind == ElementKind.CLASS && el is TypeElement) { "@RuntimePermissions must target a class" }

Type guard

fun isValidTarget(e: Element) = e.kind == ElementKind.CLASS && e.modifiers.contains(Modifier.PUBLIC)

Prevention

When it happens

Trigger: Placing @RuntimePermissions on a type that is not a supported class element (e.g. an interface, annotation, enum, or another annotation type), so units.first { type.isSubtypeOf(it.targetType) } finds no match and throws NoSuchElementException, converted to WrongClassException.

Common situations: Mistakenly annotating an interface or abstract declaration instead of an Activity/Fragment class; applying the annotation to the wrong element while refactoring; using an old processor version that does not know the new target type (e.g. newer Fragment classes); copy-pasting the annotation onto helper classes.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of permissions-dispatcher/PermissionsDispatcher@74b532bf1f (2026-09-08). Data as JSON: /api/errors/d873a2d53cf4da66. Report an issue: GitHub.