question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hilt: compilation error for inherited Fragments

See original GitHub issue

Compilation 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:closed
  • Created 3 years ago
  • Comments:17

github_iconTop GitHub Comments

6reactions
FunkyMusecommented, Jun 16, 2020

@Zhelyazko, for deeper inheritance you can use the same workaround for each base class, i.e. both CreateNoteFragment and NoteFragment would use the long-form notation for @AndroidEntryPoint.

@AlekseyMiheev, I’ll be submitting a fix to avoid having to use the workaround (i.e. you should be able to use the short-form of the annotation everywhere). The workaround isn’t mentioned in the release notes/documentation because it was more of an oversight than something we intended.

I can close out this issue once that fix is submitted.

I think it’s smart if you can use

@AndroidEntryPoint
abstract class AbstractFragment : Fragment() {

}

and when you have

class TasksFragment : AbstractFragment()

You wouldn’t have to do

@AndroidEntryPoint
class TasksFragment : AbstractFragment()
5reactions
bcorsocommented, Jun 13, 2020

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.

@AndroidEntryPoint(Fragment.class)
public class CreateNoteFragment extends Hilt_CreateNoteFragment {...}

Edit: @CraZyLegenD’s suggestion of removing @AndroidEntryPoint from the base class will also work if you don’t need to instantiate CreateNoteFragment 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 on CreateNoteFragment avoids relying on bytecode injection so that your class hierarchy is correct at compile time.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found