Hilt: compilation error for inherited Fragments
See original GitHub issueCompilation fails for the next code:
@AndroidEntryPoint
open class CreateNoteFragment: Fragment() {
@Inject
lateinit var someValA: SomeClassA
...
}
@AndroidEntryPoint
class EditNoteFragment: CreateNoteFragment() {
@Inject
lateinit var someValB: SomeClassB
...
}
Compilation error:
error: method does not override or implement a method from a supertype
@Override
Method from the class where the error occurs:
public abstract class Hilt_EditNoteFragment extends CreateNoteFragment {
...
@Override
protected void inject() {
if (!injected) {
injected = true;
((EditNoteFragment_GeneratedInjector) generatedComponent()).injectEditNoteFragment(UnsafeCasts.<EditNoteFragment>unsafeCast(this));
}
}
Possible problem: Hilt_CreateNoteFragment is generated for CreateNoteFragment class Hilt_EditNoteFragment is generated for EditNoteFragment class Hilt_EditNoteFragment extends CreateNoteFragment that doesn’t have inject() method
Issue Analytics
- State:
- Created 3 years ago
- Comments:17
Top Results From Across the Web
Hilt: Inherited fragment class not generated - Stack Overflow
When compiling, I got the following error as if the Hilt class is not properly generated: error: [Hilt] public final class ZipCodeFragment ......
Read more >Migrating to Hilt - Dagger
Retained fragments; Adding Hilt to the Activity/Fragment; Dagger; dagger.android ... Hilt by default raises an error when unannotated modules are found, ...
Read more >Dependency Injection with Hilt (Kotlin) | by Metehan Bolat
Hilt Application Class First of all, the first thing we need to do is create a class. Then, inheriting this class from the...
Read more >Hilt in multi-module apps - Android Developers
Hilt code generation needs access to all the Gradle modules that use Hilt. The Gradle module that compiles your Application class needs to ......
Read more >DI for Android using Hilt - Medium
All Dagger issues can now be found at compile time, improving on Dagger ... scopes can inherit from parent scopes; i.e. fragments can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think it’s smart if you can use
and when you have
class TasksFragment : AbstractFragment()
You wouldn’t have to do
We should be able to fix this internally. I’ll work on getting this fix out shortly, but in the meantime there’s the following workarounds.
Workaround
In the meantime, you should be able to work around this using the long-form notation for
@AndroidEntryPoint
on your base class, e.g.Edit: @CraZyLegenD’s suggestion of removing
@AndroidEntryPoint
from the base class will also work if you don’t need to instantiateCreateNoteFragment
directly.Details:
The issue is that when using the short-form notation for
@AndroidEntryPoint
, the class hierarchy isn’t quite right until after bytecode injection. The class hierarchy for the generated parent class should be:“Hilt_EditNoteFragment > CreateNoteFragment > Hilt_CreateNoteFragment > Fragment”
But before the bytecode injection it’s actually:
“Hilt_EditNoteFragment > CreateNoteFragment > Fragment”
Thus, using the long-form notation for
@AndroidEntryPoint
onCreateNoteFragment
avoids relying on bytecode injection so that your class hierarchy is correct at compile time.