Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

android - Inject Interface with Generics in Hilt

I am trying to use Hilt to inject a generic interface implementation but it keep gives me errors

My interface looks like this:

interface Validator<in T> {
  fun validate(value: T): Boolean

And a typical implementation of it can be like this:

class FullNameValidator @Inject constructor() : Validator<String>{

   override fun validate(value: String): Boolean {
      val isValid = //Validation logic

      return isValid

Now the way I am trying to use Hilt, i've tried to use @Bind method in a @Module like this

abstract fun bindFullNameValidator(validator: FullNameValidator) : Validator<String>

Like i am doing with all interfaces implementations and its working just fine But here, Hilt claims that it cannot find a way to inject the String object which is the generic type.

Is there a proper way to inject such interface

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share