error: Cannot find setter for field. - size in java.util.ArrayList - Embedded ArrayList in Room unable to compile
error: Cannot find setter for field. - size in java.util.ArrayList - Embedded ArrayList in Room unable to compile
我在将 Android 4.1.2 更新到 4.2.2 后收到此错误消息,但在将我的 Android Studio 更新到高于 4.1.2[=26 的任何版本后收到此错误消息=]
* What went wrong:
Execution failed for task ':core:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
查看构建错误时我发现了这个错误
error: Cannot find setter for field. - size in java.util.ArrayList/Users/xxxxx/XXXXX.Core-Android/core/build/tmp/kapt3/stubs/debug/com/XXXXX/android/core/entities/user/User.java:91: warning: Primary key constraint on placeId is ignored when being merged into com.xxxx.android.core.entities.user.User
几乎没有其他错误消息都与 error: Cannot find setter for field. - size in java.util.ArrayList
语言相似
我无法 post 完整 class 但我相信这些是相关部分。在这个class ArrayList只用了一次
@Entity
data class User(
@PrimaryKey @JsonProperty("userId") var userId: Int = 0,
@JsonProperty("email") override var email: String?,
...
@JsonProperty("place") @Embedded var place: Place?,
@JsonProperty("thing") @Embedded var thing: ArrayList<Thing>? = ArrayList(),
...
) : UserType
检查 UserDao_Impl.java 时我发现
final ArrayList<E> _tmpThing;
if (! (_cursor.isNull(_cursorIndexOfSize))) {
_tmpThing = new ArrayList();
_tmpThing.size = _cursor.getInt(_cursorIndexOfSize);
} else {
_tmpThing = null;
}
_tmpThing.size = _cursor.getInt(_cursorIndexOfSize);
出现错误其他开发者的构建中没有此部分
我认为这可能与 @Embedded var thing: ArrayList<Thing>?
有关
非常奇怪的是,另一个开发人员也将他们的 Android Studio 更新到 4.2.2,并且能够很好地编译项目,所以我相信这是我的机器,但我收到了这个完全擦除我的机器并进行最小安装后出现错误,只需 Android 通过工具箱安装 Studio。
我尝试过的东西
- 已将 Gradle 更新为 6.7.1 并将插件更新为
com.android.tools.build:gradle:4.2.2
,同样的错误
- 正在为
thing: ArrayList<Thing>?
创建类型转换器,同样的错误
- 将
thing: ArrayList<Thing>?
更改为列表,Entities and POJOs must have a usable public constructor
错误
- 将
kapt "androidx.room:room-compiler:2.3.0"
更改为 annotationProcessor "androidx.room:room-compiler:2.3.0"
,编译并通过所有测试,但在调用任何数据库方法时我收到运行时崩溃
- 将我的 JDK 从嵌入式更改为 JDK 11,同样的错误
- 添加
kapt.use.worker.api=false
,同样的错误
相关 gradle 个部分
buildscript {
//Fallback versions
ext {
kotlin_version = '1.4.21'
...
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
....
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'digital.wup.android-maven-publish'
apply from: 'jacoco.gradle'
ext {
...
room_version = "2.3.0"
}
android {
compileSdkVersion 29
buildToolsVersion '29.0.3'
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += ["-Xopt-in=kotlin.RequiresOptIn"]
}
dataBinding {
enabled true
}
lintOptions {
abortOnError false
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
compileOptions {
sourceCompatibility '1.8'
targetCompatibility '1.8'
}
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode computeVersionCode()
versionName computeVersionName()
consumerProguardFiles 'proguard-android.txt'
....
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
...
debug {
testCoverageEnabled true
debuggable true
minifyEnabled false
buildConfigField "String", "BUILD_VARIANT", "\"DEBUG\""
}
local {
initWith debug
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
kapt "androidx.room:room-compiler:$room_version"
//Android Core Libraries
api "androidx.room:room-runtime:$room_version"
api "androidx.room:room-ktx:$room_version"
kapt "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
....
//Asynchronous Programming
def coroutines = '1.4.2'
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
//Network/Json Parsing Libraries
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
def jackson = '2.12.4'
api "com.fasterxml.jackson.core:jackson-core:$jackson"
api "com.fasterxml.jackson.core:jackson-databind:$jackson"
api "com.fasterxml.jackson.core:jackson-annotations:$jackson"
api "com.fasterxml.jackson.module:jackson-module-kotlin:$jackson"
implementation "com.fasterxml.jackson.datatype:jackson-datatype-joda:$jackson"
//Dependency Injection
def koin = "2.0.1"
api "org.koin:koin-androidx-viewmodel:$koin"
....
//Test Libraries
testImplementation group: 'junit', name: 'junit', version: '4.12'
testImplementation 'org.robolectric:robolectric:4.4'
testImplementation 'com.github.javafaker:javafaker:1.0.1'
testImplementation "org.koin:koin-test:$koin"
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'androidx.test:runner:1.2.0'
testImplementation 'androidx.test:rules:1.2.0'
testImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation "io.mockk:mockk:1.12.0"
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
}
我遇到了类似的问题,只需从位于实体中的 ArrayList 属性 中删除注释“@Embedded”即可。
问题是 JDK 内置于更新版本的 Android Studio 和我们当前的 Gradle 设置中,尤其是这个块
android {
kotlinOptions {
jvmTarget = "1.8"
}
}
Android Studio 包含的内置 JDK 冲突。一旦我将 Android Studio 指向 Java 1.8 JDK,它就会在我的机器和新机器上编译。
在我当前的机器上,我使用 SDKMan 安装了 AdoptOpenJDK 8.0.292.hs。在我之前的机器上,我使用 Homebrew
安装了 OpenJDK 1.8
然后我转到 File -> Project Structure -> SDK Location -> JDK Location
并将 JDK 指向新安装的 1.8,而不是内置 JDK。
对于 SDKMan,路径是 /Users/me/.sdkman/candidates/java/8.0.292.hs-adpt
我在将 Android 4.1.2 更新到 4.2.2 后收到此错误消息,但在将我的 Android Studio 更新到高于 4.1.2[=26 的任何版本后收到此错误消息=]
* What went wrong:
Execution failed for task ':core:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
查看构建错误时我发现了这个错误
error: Cannot find setter for field. - size in java.util.ArrayList/Users/xxxxx/XXXXX.Core-Android/core/build/tmp/kapt3/stubs/debug/com/XXXXX/android/core/entities/user/User.java:91: warning: Primary key constraint on placeId is ignored when being merged into com.xxxx.android.core.entities.user.User
几乎没有其他错误消息都与 error: Cannot find setter for field. - size in java.util.ArrayList
语言相似
我无法 post 完整 class 但我相信这些是相关部分。在这个class ArrayList只用了一次
@Entity
data class User(
@PrimaryKey @JsonProperty("userId") var userId: Int = 0,
@JsonProperty("email") override var email: String?,
...
@JsonProperty("place") @Embedded var place: Place?,
@JsonProperty("thing") @Embedded var thing: ArrayList<Thing>? = ArrayList(),
...
) : UserType
检查 UserDao_Impl.java 时我发现
final ArrayList<E> _tmpThing;
if (! (_cursor.isNull(_cursorIndexOfSize))) {
_tmpThing = new ArrayList();
_tmpThing.size = _cursor.getInt(_cursorIndexOfSize);
} else {
_tmpThing = null;
}
_tmpThing.size = _cursor.getInt(_cursorIndexOfSize);
出现错误其他开发者的构建中没有此部分
我认为这可能与 @Embedded var thing: ArrayList<Thing>?
非常奇怪的是,另一个开发人员也将他们的 Android Studio 更新到 4.2.2,并且能够很好地编译项目,所以我相信这是我的机器,但我收到了这个完全擦除我的机器并进行最小安装后出现错误,只需 Android 通过工具箱安装 Studio。
我尝试过的东西
- 已将 Gradle 更新为 6.7.1 并将插件更新为
com.android.tools.build:gradle:4.2.2
,同样的错误 - 正在为
thing: ArrayList<Thing>?
创建类型转换器,同样的错误 - 将
thing: ArrayList<Thing>?
更改为列表,Entities and POJOs must have a usable public constructor
错误 - 将
kapt "androidx.room:room-compiler:2.3.0"
更改为annotationProcessor "androidx.room:room-compiler:2.3.0"
,编译并通过所有测试,但在调用任何数据库方法时我收到运行时崩溃 - 将我的 JDK 从嵌入式更改为 JDK 11,同样的错误
- 添加
kapt.use.worker.api=false
,同样的错误
相关 gradle 个部分
buildscript {
//Fallback versions
ext {
kotlin_version = '1.4.21'
...
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
....
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'digital.wup.android-maven-publish'
apply from: 'jacoco.gradle'
ext {
...
room_version = "2.3.0"
}
android {
compileSdkVersion 29
buildToolsVersion '29.0.3'
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += ["-Xopt-in=kotlin.RequiresOptIn"]
}
dataBinding {
enabled true
}
lintOptions {
abortOnError false
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
compileOptions {
sourceCompatibility '1.8'
targetCompatibility '1.8'
}
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode computeVersionCode()
versionName computeVersionName()
consumerProguardFiles 'proguard-android.txt'
....
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
...
debug {
testCoverageEnabled true
debuggable true
minifyEnabled false
buildConfigField "String", "BUILD_VARIANT", "\"DEBUG\""
}
local {
initWith debug
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
kapt "androidx.room:room-compiler:$room_version"
//Android Core Libraries
api "androidx.room:room-runtime:$room_version"
api "androidx.room:room-ktx:$room_version"
kapt "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
....
//Asynchronous Programming
def coroutines = '1.4.2'
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
//Network/Json Parsing Libraries
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
def jackson = '2.12.4'
api "com.fasterxml.jackson.core:jackson-core:$jackson"
api "com.fasterxml.jackson.core:jackson-databind:$jackson"
api "com.fasterxml.jackson.core:jackson-annotations:$jackson"
api "com.fasterxml.jackson.module:jackson-module-kotlin:$jackson"
implementation "com.fasterxml.jackson.datatype:jackson-datatype-joda:$jackson"
//Dependency Injection
def koin = "2.0.1"
api "org.koin:koin-androidx-viewmodel:$koin"
....
//Test Libraries
testImplementation group: 'junit', name: 'junit', version: '4.12'
testImplementation 'org.robolectric:robolectric:4.4'
testImplementation 'com.github.javafaker:javafaker:1.0.1'
testImplementation "org.koin:koin-test:$koin"
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'androidx.test:runner:1.2.0'
testImplementation 'androidx.test:rules:1.2.0'
testImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation "io.mockk:mockk:1.12.0"
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
}
我遇到了类似的问题,只需从位于实体中的 ArrayList 属性 中删除注释“@Embedded”即可。
问题是 JDK 内置于更新版本的 Android Studio 和我们当前的 Gradle 设置中,尤其是这个块
android {
kotlinOptions {
jvmTarget = "1.8"
}
}
Android Studio 包含的内置 JDK 冲突。一旦我将 Android Studio 指向 Java 1.8 JDK,它就会在我的机器和新机器上编译。
在我当前的机器上,我使用 SDKMan 安装了 AdoptOpenJDK 8.0.292.hs。在我之前的机器上,我使用 Homebrew
安装了 OpenJDK 1.8然后我转到 File -> Project Structure -> SDK Location -> JDK Location
并将 JDK 指向新安装的 1.8,而不是内置 JDK。
对于 SDKMan,路径是 /Users/me/.sdkman/candidates/java/8.0.292.hs-adpt