javaandroidannotationsannotation-processing

Unresolved reference when trying to use annotations


I'm facing an issue in my Android project where I'm trying to use annotations defined in Module B in my code within Module C. However, I'm getting an "Unresolved reference" error in Android Studio. I've tried various configurations, but I'm unable to resolve this issue. Here's my project structure and what I've done so far:

Project Structure

Module B: Annotation processor module containing annotations and an annotation processor. Module A: Android app module that includes Module B as a dependency for annotation processing. Module C: Android app module that includes Module A and aims to use annotations from Module B.

What I've Tried

  1. Added Module B as a dependency in Module A with the appropriate annotation processor configurations.
  2. Included Module A as a dependency in Module C.
  3. Ensured proper Gradle sync, dependencies, and import statements.
  4. Checked the Gradle console for any errors during the build process.

Current Issue:

Despite following the outlined steps and confirming the setup, I'm still unable to import and use the annotations from Module B in my code within Module C. The import statement for MyAnnotation in Module C is showing the "Unresolved reference" error.

Any insights into why I'm encountering this issue and how I can resolve it would be greatly appreciated.

Please let me know if there are any additional details I should provide. Thank you in advance for your help!


Solution

  • If you have a dependency such as C depends on A depends on B, using implementation() to add the dependency will not work. In order to get the transient dependency in the module C you can set api in the gradle file of A when you specify dependency B, then the classes and method of module B will be available in module C.

    Gradle file of module A

    api(project(":moduleB")) // moduleB's source will be able with moduleA
    

    Gradle file of Module C

    imeplementation(project(":moduleA")) // source of both moduleA and moduleB will be available for module C
    

    However, the best way to specify this would be to add both the dependencies in the module C.

    imeplementation(project(":moduleA"))
    annotationProcessor(project(":moduleB"))