【问题标题】:How do I resolve annotations for Room library in Kotlin?如何在 Kotlin 中解析 Room 库的注释?
【发布时间】:2019-12-06 11:24:19
【问题描述】:

我正在关注 this tutorial 在 kotlin 中的 Room for SQLite。这是他们拥有用户实体的第一步

@Entity
data class User(
    @PrimaryKey val uid: Int,
    @ColumnInfo(name = "first_name") val firstName: String?,
    @ColumnInfo(name = "last_name") val lastName: String?
)

我在所有这些注释上都收到了 Unresolved references 的 IDE 错误。

我对此完全陌生。我找不到任何关于如何在 Kotlin 中注释的信息。基本上,我不知道如何在 Kotlin 中进行注释。

我需要依赖库吗?

【问题讨论】:

    标签: android kotlin annotations android-room


    【解决方案1】:

    发生这种错误,您还没有在build.gradle 中声明依赖项。

    另外,如您发送的页面中所述:

    注意:为了在您的应用中使用 Room,请在应用的 build.gradle 文件中声明 Room 依赖项。

    依赖列表可以找到here

    因此,将 Room 依赖项添加到您的 build.gradle 中,如下所示(您可以一一添加并检查哪一个解决了问题:

    dependencies {
      def room_version = "2.2.2"
    
      implementation "androidx.room:room-runtime:$room_version"
      annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
    
      // optional - Kotlin Extensions and Coroutines support for Room
      implementation "androidx.room:room-ktx:$room_version"
    
      // optional - RxJava support for Room
      implementation "androidx.room:room-rxjava2:$room_version"
    
      // optional - Guava support for Room, including Optional and ListenableFuture
      implementation "androidx.room:room-guava:$room_version"
    
      // Test helpers
      testImplementation "androidx.room:room-testing:$room_version"
    }
    

    添加它们并执行同步。那么,这些错误应该消失了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多