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 with Custom Views and @AndroidEntryPoint, generating @Nullable annotation twice

See original GitHub issue

Hi there 👋 I’m trying to migrate my project to use Hilt from Dagger Android.

I’ve run into an issue where adding @AndroidEntryPoint onto a custom view, produces a compilation issue on the generated custom view component.

The custom view looks similar to the following:

@AndroidEntryPoint
class CustomShapeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    CustomBaseView<ShapeLayer>(context, attrs, defStyleAttr) {
    @Inject
    lateinit var customInjectedClass: CustomInjectedClass

And now when I run the build, it breaks with the generated class for the view looking as follows:

/**
 * A generated base class to be extended by the @dagger.hilt.android.AndroidEntryPoint annotated class. If using the Gradle plugin, this is swapped as the base class via bytecode transformation.
 */
@Generated("dagger.hilt.android.processor.internal.androidentrypoint.ViewGenerator")
public abstract class Hilt_CustomShapeView<T> extends CustomBaseView<T> implements GeneratedComponentManager<Object> {
  private ViewComponentManager componentManager;
  Hilt_CustomShapeView(@NotNull Context arg0, @Nullable @Nullable AttributeSet arg1,
      int arg2) {
    super(arg0, arg1, arg2);
    inject();
  }
  Hilt_CustomShapeView(@NotNull Context arg0, @Nullable @Nullable AttributeSet arg1) {
    super(arg0, arg1);
    inject();
  }
  Hilt_CustomShapeView(@NotNull Context arg0) {
    super(arg0);
    inject();
  }

You can see the first two generated constructors for the view has the @Nullable annotation twice on the AttributeSet. The compilation error is the following:

error: Nullable is not a repeatable annotation type
  Hilt_CustomShapeView(@NotNull Context arg0, @Nullable @Nullable AttributeSet arg1,

I tried to remove the @JvmOverloads annotation and create the constructors one by one, but that didn’t seem to make a difference either.

I’m not sure if I’m missing something, but I think this is a bug.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:26 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
ZacSweerscommented, Jun 18, 2020

JavaPoet 1.13.0 is available now on maven central: https://repo1.maven.org/maven2/com/squareup/javapoet/1.13.0/

Thanks to @egorand for the quick turnaround on it!

5reactions
ZacSweerscommented, Jun 16, 2020

Heard back and they’re going to target releasing this week 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android ViewModel instantiate twice using hilt - Stack Overflow
viewModels() delegation create view model against the same instance i.e Fragment's instance in your case. What you need to do is to create...
Read more >
Zac Sweers on Twitter: "☕️✍️ JavaPoet 1.13.0 is out with ...
Hilt with Custom Views and @AndroidEntryPoint, generating @Nullable annotation twice · Issue #1909... ... I'm trying to migrate my project to use ...
Read more >
All about Hilt. Get deeper understanding about Hilt.
Hilt is a dependency injection framework for Android that is built on ... Step 5 - Create a custom scope annotation: Hope now...
Read more >
Dependency injection with Hilt | Android Developers
Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.
Read more >
Testing Fragments with Dagger-Hilt - Part 12 - YouTube
In this video I will show you a workaround to be able to test fragments with the help of Dagger- Hilt.⭐ Get certificates...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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