android - 如何使用 Android Room @Ignore Sqlite 实体列
问题描述
我当前的 Android 应用程序需要从我的本地 Sqlite 数据库中获取具有计数字段的行。
我意识到我需要让我的 DO 实体定义这个计数字段并 @Ignore(ed)
我用谷歌搜索并找到了这个页面
@Entity
data class User(
@PrimaryKey val id: Int,
val firstName: String?,
val lastName: String?,
@Ignore val picture: Bitmap?
)
但是,当我将这个确切的 Kotlin 代码复制并粘贴到我的应用程序中时,它无法构建并出现这些异常
database_room:kaptStagingDebugKotlin FAILED
e: /Users/rossulbricht/projects/silkroad/database_room/build/tmp/kapt3/stubs/stagingDebug/org/dread/pirate/database_room/model/User.java:7: 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).
public final class User {
^
Tried the following constructors but they failed to match:
User(int,java.lang.String,java.lang.String,android.graphics.Bitmap) -> [param:id -> matched field:id, param:firstName -> matched field:firstName, param:lastName -> matched field:lastName, param:picture -> matched field:unmatched]
e: /Users/rossulbricht/projects/silkroad/database_room/build/tmp/kapt3/stubs/stagingDebug/org/dread/pirate/database_room/model/User.java:9: error: Cannot find setter for field.
private final int id = 0;
^
e: /Users/rossulbricht/projects/silkroad/database_room/build/tmp/kapt3/stubs/stagingDebug/org/dread/pirate/database_room/model/User.java:11: error: Cannot find setter for field.
private final java.lang.String firstName = null;
^
e: /Users/rossulbricht/projects/silkroad/database_room/build/tmp/kapt3/stubs/stagingDebug/org/dread/pirate/database_room/model/User.java:13: error: Cannot find setter for field.
private final java.lang.String lastName = null;
^
w: [kapt] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).
> Task :networking:mergeStagingDebugNativeLibs
> Task :android_idplus:generateStagingDebugRFile
> Task :networking:processStagingDebugManifest
FAILURE: Build failed with an exception.
我的问题是...
如何忽略 Kotlin 数据教室实体中的列?
为什么google android示例不起作用?
我在用
Android Studio 3.5
Build #AI-191.8026.42.35.5791312, built on August 9, 2019
JRE: 1.8.0_202-release-1483-b49-5587405 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.6
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://sdk.smartlook.com/android/release"
}
}
}
ext {
compileSdkVersion = 28
targetSdkVersion = 28
minSdkVersion = 21
supportLibVersion = '1.1.0-beta01'
coreKtx = '1.2.0-alpha04'
materialMaterial = '1.1.0-alpha10'
legacySupportV4 = "1.0.0"
recyclerView = '1.1.0-beta04'
lifecycleExtensions = '2.2.0-alpha04'
playServicesAuthVersion = "16.0.1"
archLifecycleVersion = '2.2.0-alpha04'
roomVersion = '2.2.0-rc01'
archPagingVersion = '2.1.0'
archWorkerRuntimeVersion = '2.3.0-alpha01'
constraintLayoutVersion = '2.0.0-beta2'
browserVersion = '1.0.0'
appCompatVersion = '1.1.0'
junitVersion = "4.12"
testRunnerVersion = '1.1.0-alpha4'
espressoTestCoreVersion = '3.1.0-alpha4'
uiautomatorVersion = '2.2.0-alpha4'
leakcanaryVersion = "1.6.1"
commonsTextVersion = '1.7'
rxbinding2Version = "2.2.0"
picassoVersion = "2.71828"
retrofit2Version = '2.6.1'
rxAndroidVersion = '2.1.1'
rxJavaVersion = '2.2.11'
jacksonKotlinVersion = '2.10.0.pr1'
okhttp3Version = '4.1.1'
okioVersion = '2.4.0'
jakewRxrelay2Version = '2.1.0'
daggerVersion = '2.24'
numbusJoseJwtVersion = '7.1'
interComVersion = "5.4.1"
guavaAndroidVersion = "28.1-android"
}
task clean(type: Delete) {
delete rootProject.buildDir
}
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
解决方案
这里
@Entity
data class User(
@PrimaryKey val id: Int,
val firstName: String?,
val lastName: String?,
@Ignore val picture: Bitmap?
)
修改它,使
@Entity
data class User(
@PrimaryKey val id: Int,
val firstName: String?,
val lastName: String?,
@Ignore val picture: Bitmap?
){
constructor():this(id=null,firstName=null,lastName=null,picture=null) }
然后会出现另一个错误,没有设置器,所以替换val
为var
@Entity
data class User(
@PrimaryKey var id: Int,
var firstName: String?,
var lastName: String?,
@Ignore val picture: Bitmap?
){
constructor():this(id=null,firstName=null,lastName=null,picture=null) }
这是我用于房间的 gradle
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
implementation "androidx.room:room-ktx:$room_version"
apply plugin: 'kotlin-kapt'
在顶部添加
ext.room_version = '2.2.0-rc01'