错误:找不到字段的设置器。-Size in java.util.ArrayList-Room中的Embedded ArrayList无法编译

error: Cannot find setter for field. - size in java.util.ArrayList - Embedded ArrayList in Room unable to compile(错误:找不到字段的设置器。-Size in java.util.ArrayList-Room中的Embedded ArrayList无法编译)

本文介绍了错误:找不到字段的设置器。-Size in java.util.ArrayList-Room中的Embedded ArrayList无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Android 4.1.2更新到4.2.2之后收到此错误信息,但在将我的Android Studio更新到4.1.2以上的任何版本后收到此错误

* 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语言完全相似的其他错误消息

我不能发布完整的课程,但我相信这些是相关的部分。在此类中,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,并且能够很好地编译项目,所以我相信这是我的机器,但我在彻底擦除我的机器和最小安装后收到这个错误,只是通过Toolbox安装了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,相同错误

相关托架部分

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'
}

推荐答案

问题是内置于更新版本的Android Studio和我们当前的Gradle设置中的jdk,特别是这个挡路

android {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Android Studio附带的内置JDK冲突。一旦我将Android Studio指向Java1.8JDK,它就会在我的机器和新机器上编译。

在我当前的机器上,我使用SDKMan安装了AdoptOpenJDK 8.0.292.hs。在我的前一台计算机上,我使用Homebrew安装了OpenJDK 1.8

然后我转到File -> Project Structure -> SDK Location -> JDK Location,将JDK指向新安装的1.8,而不是内置的JDK。

对于SDK Man,路径为/Users/me/.sdkman/candidates/java/8.0.292.hs-adpt

这篇关于错误:找不到字段的设置器。-Size in java.util.ArrayList-Room中的Embedded ArrayList无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:错误:找不到字段的设置器。-Size in java.util.ArrayList-Room中的Embedded ArrayList无法编译

基础教程推荐