1

I have the following Kotlin Class:

@ExperimentalCoroutinesApi
class SharedPrefClient 
    @Inject constructor(private val prefs: SharedPreferences) 
      : SharedPreferences by prefs {

   ...

}

In Dagger, I get the following error while building the project:

enter image description here

But it workes when I remove the class Delegation : SharedPreferences by prefs

Now my question is how can I use Dagger and Kotlin Class Delegation both together.

zoha131
  • 1,758
  • 2
  • 16
  • 18

1 Answers1

1

It seems to me that the problem is with android.annotation.Nullable which gets pulled into your code by using the by keyword, but which isn't accessible from outside the Android source code.

Usually you'd see the error directly if the type cannot be resolved, but in this case Kotlin generates Java code with the unknown annotation and Dagger tries processing that, leading to your error.

If you create your own interface other than SharedPreferences it will work just fine as you'd expect.

You might be able to add android.annotation.Nullable to your (compile only) classpath somehow so that Dagger can do its thing, but it's probably easier to find another way.


I see three issues that you could raise here

  • Dagger should fail better. The error message is of little help in this case
  • Android shouldn't expose internal/hidden types,
  • and/or Kotlin shouldn't pull in those hidden types
David Medenjak
  • 33,993
  • 14
  • 106
  • 134
  • Currently, I have removed the class delegation and added functions manually. A little bit verbose but working for me. – zoha131 Nov 20 '19 at 05:28