alibaba/ARouter · error · IllegalAccessException

The fields need autowired from intent, its parent must be ac

Error message

The fields need autowired from intent, its parent must be activity or fragment! [<qualifiedName>]

What it means

AutowiredProcessor (annotation processor) generates field-injection code only for classes whose parent is an Activity or Fragment. During compilation, if a class annotated for autowiring (with fields marked @Autowired) is neither, the processor throws IllegalAccessException with the class's qualified name, aborting the build.

Source

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

                for (Element element : childs) {
                    if (!isSubtypeOf(element.asType(), iProvider)) {
                        hasAutowiredValues = true;
                        break;
                    }
                }

                if (hasAutowiredValues) {
                    if (isSubtypeOf(parent.asType(), activityTm)) {
                        injectMethodBuilder.addStatement(
                                "$T bundle = substitute.getIntent() == null ? null : substitute.getIntent().getExtras()",
                                AndroidBundle
                        );
                    } else if (isSubtypeOf(parent.asType(), fragmentTm)
                            || isSubtypeOf(parent.asType(), fragmentTmV4)
                            || isSubtypeOf(parent.asType(), fragmentTmAndroidX)) {
                        injectMethodBuilder.addStatement("$T bundle = substitute.getArguments()", AndroidBundle);
                    } else {
                        throw new IllegalAccessException("The fields need autowired from intent, its parent must be activity or fragment! ["
                                + parent.getQualifiedName() + "]");
                    }
                }

                // Generate method body, start inject.
                for (Element element : childs) {
                    Autowired fieldConfig = element.getAnnotation(Autowired.class);
                    String fieldName = element.getSimpleName().toString();
                    if (isSubtypeOf(element.asType(), iProvider)) {  // It's provider
                        if ("".equals(fieldConfig.name())) {    // User has not set service path, then use byType.

                            // Getter
                            injectMethodBuilder.addStatement(
                                    "substitute." + fieldName + " = $T.getInstance().navigation($T.class)",
                                    ARouterClass,
                                    ClassName.get(element.asType())
                            );
                        } else {    // use byName

View on GitHub (pinned to 84f451d244)

Solutions

  1. Move @Autowired fields into Activities or Fragments that ARouter injects
  2. Remove @Autowired from unsupported classes and pass values manually via getIntent/constructor
  3. For Services/others, read extras manually or use ARouter's navigation callback instead of field injection
  4. Check the class hierarchy — the parent chain must bottom out at Activity/Fragment (incl. androidx/support fragments)

Example fix

// before
public class MyViewModel extends ViewModel {
    @Autowired
    String userId; // IllegalAccessException at compile time
}
// after
public class UserActivity extends AppCompatActivity {
    @Autowired
    String userId; // injected by ARouter for activity
    @Override protected void onCreate(Bundle b) {
        ARouter.getInstance().inject(this);
    }
}
Defensive patterns

Strategy: validation

Validate before calling

// compile-time: only annotate supported parents
// ensure: activity instanceof Activity || activity instanceof Fragment
if (!(clazz.isAssignableFrom(Activity.class)) && !(isFragment(clazz))) {
    throw new IllegalStateException("@Autowired only valid on Activity/Fragment: " + clazz);
}

Prevention

When it happens

Trigger: Adding @Autowired fields to a class that is not an Activity/Fragment (e.g. a Service, BroadcastReceiver, ViewModel, or plain object) while the compiler maven plugin's autowire processing scans it; typically triggered by ARouter's injectable supertype chain.

Common situations: Developers assuming @Autowired works in ViewModels or Services; adding ARouter.autoconfigure modules that register non-activity classes; upgrading ARouter and adding @Autowired to newly scanned 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 alibaba/ARouter@84f451d244 (2026-09-06). Data as JSON: /api/errors/00bd7779c48d8191. Report an issue: GitHub.