错误:找不到字段的设置器。- java.util.ArrayList 中的大小 - Room 中的嵌入式 ArrayList 无法编译

Lar*_*kin 6 android kotlin android-room

我在将 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)
Run Code Online (Sandbox Code Playgroud)

在查看构建错误时,我发现了这个错误

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
Run Code Online (Sandbox Code Playgroud)

几乎没有其他错误消息与该error: Cannot find setter for field. - size in java.util.ArrayList语言相似

我无法发布完整的课程,但我相信这些是相关的部分。在这个类中ArrayList只使用一次

* 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)
Run Code Online (Sandbox Code Playgroud)

在检查 UserDao_Impl.java 时我发现

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
Run Code Online (Sandbox Code Playgroud)

出现错误“_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
    }
}
Run Code Online (Sandbox Code Playgroud)
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'
}
Run Code Online (Sandbox Code Playgroud)

Lar*_*kin 4

问题在于较新版本的 Android Studio 中内置的 JDK 以及我们当前的 Gradle 设置,尤其是此块

android {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
Run Code Online (Sandbox Code Playgroud)

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 LocationJDK 指向新安装的 1.8,而不是内置的 JDK。

对于 SDKMan 来说,路径是/Users/me/.sdkman/candidates/java/8.0.292.hs-adpt