2
1

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.

[Android Architecture Components]Roomをmulti moduleプロジェクトで扱うとハマった

Last updated at Posted at 2017-12-20

現象

RoomのEmbbededアノテーションを付けたfieldに別moduleのクラスを使うとbuildが通らなくなる

@Entity
class HogeEntity(
    @Primarykey
    val id: Long,
    @Embedded
    val hogeUser: HogeUser 
)

// HogeEntityとは別のmoduleで定義
data class HogeUser(
    val personId: PersonId,
    val name: Name,
    val height: Double,
    val weight: Double,
    val nickName: String
)

例えばこのようなクラスでbuildしてみると以下のようなエラーが発生する

Error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
Tried the following constructors but they failed to match:
Error:HogeUser(PersonId,Name,double,double,java.lang.String) : [arg0 : null, arg1 : null, arg2 : null, arg3 : null, arg4 : null, arg5 : null]

回避策

  1. エラーメッセージに記載されている通りに空のconstructorを定義してsetterでfieldに値を入れてもらうスタイルに変更する
  2. Embedded使うのをやめて手動でEntityのfieldに入れ込む
  3. 別moduleにするのをやめて同一moduleで扱う

どれも微妙ですねぇ。。。 :sob::sob::sob:

もし他に解決策などありましたら教えていただけるとありがたいです :bow:

参考

https://issuetracker.google.com/issues/67181813
https://stackoverflow.com/questions/47833645/android-room-multi-module-project-in-kotlin

2
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?