Andoridx Room rx issue

Viewed 198

I'm start to use latest androidx.room version that support io.reactivex return type.

dependencies {
    def room_version = "2.1.0-alpha02"
    implementation "androidx.room:room-rxjava2:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    testImplementation "androidx.room:room-testing:$room_version"
}

Simple dao class

@Dao
interface UserDao : BaseDao<UserTable> {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertUser(user: UserTable):Maybe<Long>

    @Update
    fun updateUser(user:UserTable):Completable

    @Query("DELETE FROM $USER_TABLE")
    fun clear()
}

generates UserDao_Impl.java with error

error: local variable user is accessed from within inner class; needs to be declared final

Highlighted field is in this method

  @Override
  public Maybe<Long> insertUser(UserTable user) {
    return Maybe.fromCallable(new Callable<Long>() {
     @Override
     public Long call() throws Exception {
       __db.beginTransaction();
        try {
          long _result = 
           __insertionAdapterOfUserTable.insertAndReturnId(user);//error line
           __db.setTransactionSuccessful();
         return _result;
        } finally {
         __db.endTransaction();
        }
      }
    });
  }

It's true that (UserTable user) should be declared as final in java but it is generated class. Does any one came across this?

2 Answers

I had the same problem. Solved it by adding

compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}

in build.gradle

I'm using Linux machine and combining answers of user3021656 and tynn got success build with

compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}

and setting Java version for Android Studio as

java version "1.8.0_191"

Java(TM) SE Runtime Environment (build 1.8.0_191-b12)

Related