2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Dagger Hilt + Roomでbuildが通らない問題を解決する

Last updated at Posted at 2020-06-11

はじめに

Android/Javaプロジェクトの依存性注入フレームワークのひとつにdaggerがあります。
このdaggerの冗長なボラープレートや学習コストの高さを改善する新しいフレームワークがDagger Hiltです。

RoomJetPackの一部として提供されているライブラリです。
SQliteにアクセスする処理をより簡潔に書くことができます。

Dagger Hilt と Room を併用するとbuildに失敗する

Roomはコンパイル時、データベースのスキーマ情報をJSONファイルにエクスポートします。
このJSONファイルをgitなどで管理することで、テストやデータベースのマイグレーションに役立てることができます。
スキーマをエクスポートするには、build.gradle ファイル内に room.schemaLocation アノテーションプロセッサプロパティを設定します(参考)。


    android {
        ...
        defaultConfig {
            ...
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
                }
            }
        }
    }

しかしこのアノテーションプロセッサプロパティの書き換えは、dagger hiltのアノテーションプロセッサにも反映されてしまい、その結果buildが通りません。

> Task :app:kaptDebugKotlin FAILED
/Users/hiroyukitamura/StudioProjects/dagger-hilt-example/app/build/tmp/kapt3/stubs/debug/com/github/aakira/hilt/ui/second/SecondActivity.java:7: エラー: [Hilt]
public final class SecondActivity extends androidx.appcompat.app.AppCompatActivity {
             ^
  Expected @AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin?

解決方法

そこで前述のようにアノテーションプロセッサプロパティを書き換えず、追加することで対応します。


    android {
        ...
        defaultConfig {
            ...
            javaCompileOptions {
                annotationProcessorOptions {
                    // "="ではなく"+="にすればargmentが追加されるだけで書き換わりません!
                    arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
                }
            }
        }
    }

最後に

既にPRがdaggerにあがっているので、そのうちdaggerのドキュメントが更新されるかと思います。

参考記事
2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?