Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hilt: Compile error on attempt to inject @ActivityContext as member property #1980

Closed
jt-9 opened this issue Jul 10, 2020 · 2 comments · Fixed by #1985
Closed

Hilt: Compile error on attempt to inject @ActivityContext as member property #1980

jt-9 opened this issue Jul 10, 2020 · 2 comments · Fixed by #1985

Comments

@jt-9
Copy link

jt-9 commented Jul 10, 2020

I am setting Hilt as DI tool in my Kotlin Android project. The issue appears when I try to inject activity context and with qualifier standard @ActivityContext. Compile error message:
This annotation is not applicable to target 'member property with backing field'

Code sample

@AndroidEntryPoint
class TestActivity : AppCompatActivity() {
    @Inject
    @ApplicationContext
    lateinit var context: Context // Okay

    @Inject
    @ActivityContext  // compile error
    lateinit var activityContext: Context 
} 

If I annotate property with qualifier @ApplicationContext Hilt properly injects it
Dagger Hilt ver 2.28.1-alpha

@danysantiago
Copy link
Member

I think this might be occurring because @ActivityContext has no @Target(ElementType.FIELD). I'll confirm that and get a fix going.

@jt-9
Copy link
Author

jt-9 commented Jul 12, 2020

@danysantiago Thank you, for quick reply.
One more thing: I have compared declaration of @ApplicationContext

@Qualifier
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
public @interface ApplicationContext {}

And @ActivityContext

@Qualifier
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
public @interface ActivityContext {}

Indeed the @ActivityContext is missing ElementType.FIELD.
Unlike in declaration of @ActivityContext the @ApplicationContext has no @Retention(RetentionPolicy.CLASS), ElementType.TYPE. Should this be corrected as well?

Dagger Hilt ver 2.28.1-alpha

netdpb pushed a commit that referenced this issue Jul 13, 2020
If the qualifiers does not have the target field then it can't
be used with in members that are injected either by entry points
or @androidentrypoint.

This change also removes the @target(TYPE) the annotation contained,
this was a mistake and there is no reason for this qualifier to be
added to a class definition.

Fixes #1980

RELNOTES=Added missing FIELD target to @ActivityContext qualifier.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=320698031
netdpb pushed a commit that referenced this issue Jul 13, 2020
If the qualifiers does not have the target field then it can't
be used with in members that are injected either by entry points
or @androidentrypoint.

This change also removes the @target(TYPE) the annotation contained,
this was a mistake and there is no reason for this qualifier to be
added to a class definition.

Fixes #1980

RELNOTES=Added missing FIELD target to @ActivityContext qualifier.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=320698031
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants