0

I'm trying to make room entity observable (extend BaseObservable) so it could be used in LiveData with bi-directional binding. So I have this data class:

@Entity
data class Model(

@PrimaryKey(autoGenerate = true)
val id: Int,
val name: String) : Serializable

and in XML I want to use it like this:

<EditText
            ...
            android:text="@{viewModel.myLiveData.name}"

and in the ViewModel I have:

val myLiveData: MutableLiveData<Model>

I've found basically the same question here.. but for JAVA, and I'm having issues converting it into kotlin. My kotlin equivalent is:

@Entity
class Model: BaseObservable,  Serializable {

   constructor(id: Int, name: String) {
     this.id = id
     this.name = name
   }

  @PrimaryKey(autoGenerate = true)
  var id: Int = 0

  @get:Bindable
  var name: String = ""
    set(value) {
        field = value
        notifyChange()
    }
 }

which doesn't work and causes:

> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
kodlan
  • 561
  • 8
  • 23

1 Answers1

0

Answering my own question... So my question was missing one important detail - in my model class I had second constructor which was not annotated with @Ignore so Android Room was complaining " Room cannot pick a constructor since multiple constructors are suitable. Try to annotate unwanted constructors with @Ignore."

BUT(!) the error was shown ONLY for the class in Java, not in Kotlin for some strange reason.

So in Java I had this class:

@Entity
public class Model extends BaseObservable implements Serializable {

   @PrimaryKey(autoGenerate = true)
   public int id;

   private String name;

   //@Ignore (THIS SHOULD BE UNCOMMENTED TO GET RID OF AN ERROR!!!!!
   public Model(String name) {
      this.id = 0;
      this.name = name;
   }

   public Model(int id, String name) {
      this.id = id;
      this.name = name;
   }

   public void setName(String name) {
      if (!Objects.equals(name, this.name)) {
          this.name = name;
          notifyPropertyChanged(BR.name);
      }
   }

   @Bindable
   public String getName() {
      return name;
   } 
}

So for this Java class I was getting:

Room cannot pick a constructor since multiple constructors are suitable. Try to annotate unwanted constructors with @Ignore.

Then for the equivalent Kotlin class:

@Entity
class Model: BaseObservable,  Serializable {

  constructor(id: Int, name: String) {
      this.id = id
      this.name = name
  }

  // @Ignore - THIS VAS MISSING!!!
  constructor(name: String): this(0, name)

  @PrimaryKey(autoGenerate = true)
  var id: Int = 0

  @get:Bindable
  var name: String = ""
      set(value) {
          field = value
          notifyChange()
      } 
}

I get only this error:

> Task :app:kaptDebugKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s
28 actionable tasks: 2 executed, 26 up-to-date

Which disappointedly doesn't say anything about the second constructor!!!

kodlan
  • 561
  • 8
  • 23