alibaba/ARouter · error · IllegalAccessException

The inject fields CAN NOT BE 'private'!!! please check field

Error message

The inject fields CAN NOT BE 'private'!!! please check field [<field>] in class [<class>]

What it means

Thrown by AutowiredProcessor.categories when an annotated @Autowired field is declared private. ARouter generates code in a separate helper class that writes injected values into the field reflectively/generated bytecode, so private fields cannot be assigned. The compiler aborts processing with the field and enclosing class named in the message.

Source

Thrown at arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/AutowiredProcessor.java:267

            case DOUBLE:
                return "getDouble";
            default:
                throw new IllegalArgumentException("Unsupported primitive type: " + TypeKind.values()[type]);
        }
    }

    /**
     * Categories field, find his papa.
     *
     * @param elements Field need autowired
     */
    private void categories(Set<? extends Element> elements) throws IllegalAccessException {
        if (CollectionUtils.isNotEmpty(elements)) {
            for (Element element : elements) {
                TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

                if (element.getModifiers().contains(Modifier.PRIVATE)) {
                    throw new IllegalAccessException("The inject fields CAN NOT BE 'private'!!! please check field ["
                            + element.getSimpleName() + "] in class [" + enclosingElement.getQualifiedName() + "]");
                }

                if (parentAndChild.containsKey(enclosingElement)) { // Has categries
                    parentAndChild.get(enclosingElement).add(element);
                } else {
                    List<Element> childs = new ArrayList<>();
                    childs.add(element);
                    parentAndChild.put(enclosingElement, childs);
                }
            }

            logger.info("categories finished.");
        }
    }
}

View on GitHub (pinned to 84f451d244)

Solutions

  1. Remove the `private` modifier from the annotated field (use public or package-private).
  2. Use @Autowired(name = "key") on a non-private field and keep the private one as a wrapper if encapsulation is needed.
  3. In Kotlin, avoid `private` on @Autowired fields; declare as `var`/`lateinit var` without private.

Example fix

// before
@Autowired
private String name;

// after
@Autowired
String name; // non-private so ARouter can inject
Defensive patterns

Strategy: validation

Validate before calling

for (Field f : activity.getDeclaredFields()) {
    if (f.isAnnotationPresent(Autowired.class) && Modifier.isPrivate(f.getModifiers())) {
        throw new IllegalStateException("@Autowired field must not be private: " + f.getName());
    }
}

Prevention

When it happens

Trigger: Declaring a field with @Autowired (or @Autowired in a class processed by ARouter) with the `private` modifier, detected during annotation processing via element.getModifiers().contains(Modifier.PRIVATE).

Common situations: Kotlin `private var` fields annotated with @Autowired; Java fields made private for encapsulation; refactoring previously package-private fields to private without removing the annotation.

Understand the failure class

Background: "You do not have permission" / 403 Forbidden errors: authenticated but not allowed — causes and fixes across open-source libraries — this error's family across 31 libraries.

Related errors


AI-assisted analysis of alibaba/ARouter@84f451d244 (2026-09-06). Data as JSON: /api/errors/262550d396a332b5. Report an issue: GitHub.