Kotlin with Room and Dagger - 编译错误

时间:2018-04-11 07:31:30

标签: android kotlin android-room dagger

我正在处理我的第一个Android kotlin应用程序。我的第一个活动是使用模拟数据,我现在正试图从数据库中获取数据,但代码不会编译。

Kotlin代码:

@Dao
interface TagGroupDao {

    @Query("select * from TagGroup")
    fun getAll(): LiveData<List<TagGroup>>
}

这已经生成了这个java代码:

public class TagGroupDao_Impl implements TagGroupDao {
  private final RoomDatabase __db;

  public TagGroupDao_Impl(RoomDatabase __db) {
    this.__db = __db;
  }

  @Override
  public LiveData<List<TagGroup>> getAll() {
    final String _sql = "select * from TagGroup";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    return new ComputableLiveData<List<TagGroup>>() {
      private Observer _observer;

      @Override
      protected List<TagGroup> compute() {
        if (_observer == null) {
          _observer = new Observer("TagGroup") {
            @Override
            public void onInvalidated(@NonNull Set<String> tables) {
              invalidate();
            }
          };
          __db.getInvalidationTracker().addWeakObserver(_observer);
        }
        final Cursor _cursor = __db.query(_statement);
        try {
          final int _cursorIndexOfId = _cursor.getColumnIndexOrThrow("Id");
          final int _cursorIndexOfName = _cursor.getColumnIndexOrThrow("Name");
          final List<TagGroup> _result = new ArrayList<TagGroup>(_cursor.getCount());
          while(_cursor.moveToNext()) {
            final TagGroup _item;
            final long _tmpId;
            _tmpId = _cursor.getLong(_cursorIndexOfId);
            final String _tmpName;
            _tmpName = _cursor.getString(_cursorIndexOfName);
            _item = new TagGroup(_tmpId,_tmpName);
            _result.add(_item);
          }
          return _result;
        } finally {
          _cursor.close();
        }
      }

      @Override
      protected void finalize() {
        _statement.release();
      }
    }.getLiveData();
  }
}

对ComputableLiveData的引用无法解析。

e: TagGroupDao_Impl.java:3: error: cannot find symbol
e: 

e: import android.arch.lifecycle.ComputableLiveData;
e:                              ^
e:   symbol:   class ComputableLiveData
e:   location: package android.arch.lifecycle
e: java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing

最后,我的依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    implementation "android.arch.persistence.room:runtime:1.0.0"
    implementation "android.arch.lifecycle:viewmodel:1.1.1"
    implementation "android.arch.lifecycle:livedata-core:1.1.1"
    implementation "android.arch.lifecycle:common-java8:1.0.0"
    implementation 'com.google.dagger:dagger:2.13'
    implementation "com.google.dagger:dagger:2.13"
    implementation "com.google.dagger:dagger-android:2.13"
    implementation "com.google.dagger:dagger-android-support:2.13"
    kapt "android.arch.persistence.room:compiler:1.0.0"
    kapt 'com.google.dagger:dagger-compiler:2.13'
    kapt "com.google.dagger:dagger-android-processor:2.13"
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    testImplementation 'junit:junit:4.12'
    testImplementation "android.arch.persistence.room:testing:1.0.0"
    testImplementation "android.arch.core:core-testing:1.1.1"
}

从我设法收集的有关ComputableLiveData的内容来看,它是一个不能直接使用的内部类。

如果我删除了LiveData&lt;&gt;来自kotlin代码的包装器,它编译。<​​/ p>

我引用了MutableLiveData&lt;&gt;别处没有问题;我的活动观察数据集和更新。

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:5)

更改此依赖关系:

HAVING

对此:

implementation "android.arch.lifecycle:livedata-core:1.1.1"