0
0

More than 3 years have passed since last update.

HiltでMediaBrowserServiceCompatに依存関係を注入する

Posted at

MediaBrowserServiceCompat@AndroidEntryPoint を使って依存関係を注入しようとすると、以下のエラーがでてしまいます。

エラー: MediaBrowserServiceCompatにアクセスできません
  androidx.media.MediaBrowserServiceCompatのクラス・ファイルが見つかりません

これを解決するために、新たに EntryPoint を定義します。

@EntryPoint
@InstallIn(ApplicationComponent::class)
internal interface MediaBrowserServiceCompatInterface {
    fun getHoge(): Hoge
}

そして、 MediaBrowserServiceCompat 内でこれを使って注入します。

    EntryPoints.get(applicationContext, MediaBrowserServiceCompatInterface::class.java).also {
        this.hoge = it.getHoge()
    }

Entry Points - Hilt のBestPracticeにはクラス内にInterfaceを定義し、それを使うことが推奨されていますが、 MediaBrowserServiceCompat の場合はうまく動きません。おとなしく別ファイルに書くしかないようです。

MediaBrowserServiceCompatはServiceを継承しているので、AndroidEntryPointでかけそうなものですがなにがだめなのでしょうか。。。もし、なにかいい方法をご存じの方がおられたら、コメントで教えていただけると嬉しいです!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0