https://kotlinlang.org logo
d

dimsuz

02/25/2022, 1:56 PM
I am using Anvil and I have a class with no arg constructor and which doesnt extend any interface and I'm tempted to add it with
@ContributesBinding(...)
, but this requires that target class extends some interface. Is it wrong to wish this? It just looks simpler than writing a boring provide with
MyClass()
in the Module.
n

Nicholas Doglio

02/25/2022, 2:44 PM
You don't need to create a provide method. Just add an
@Inject
annotation to your empty constructor.
Copy code
class MyClass @Inject constructor() {}
d

dimsuz

02/25/2022, 3:02 PM
awesome. I faintly remembered something like this could work, but wasn't sure. It has been quite some time since I last used Dagger 🙂
👍 1
r

ralf

02/25/2022, 5:06 PM
That sample doesn’t work. You can only use
@ContributesTo
for Dagger modules and component interfaces, but not classes with an
@Inject
constructor.
If you don’t have any super types, then you don’t need Anvil at all. You can simply inject the type with the
@Inject
constructor. Nothing else is needed.
👌 1
n

Nicholas Doglio

02/25/2022, 5:38 PM
Thanks @ralf I removed the wrong annotation, that's what I get for writing sudo code on my phone after just waking up 😅