error: Cannot find setter for field. - size in java.util.ArrayList - Embedded ArrayList in Room unable to compile

Viewed 1816

I am receiving this error message after updating Android 4.1.2 to 4.2.2, but have received this error after updating my Android Studio to any version above 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)

When looking through the build errors I find this error

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

There are few other error messages that are all similar with the error: Cannot find setter for field. - size in java.util.ArrayList language

I can't post the full class but I believe these are the relevant sections. In this class ArrayList is only used once

@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

When inspecting the UserDao_Impl.java I find

final ArrayList<E> _tmpThing;
            if (! (_cursor.isNull(_cursorIndexOfSize))) {
              _tmpThing = new ArrayList();
              _tmpThing.size = _cursor.getInt(_cursorIndexOfSize);
            }  else  {
              _tmpThing = null;
            }

With an error on _tmpThing.size = _cursor.getInt(_cursorIndexOfSize); The other devs do not have this section in their builds

I believe it could possibly be related to the @Embedded var thing: ArrayList<Thing>?

What is extremely strange is that another dev has updated their Android Studio to 4.2.2 as well and is able to compile the project just fine so I believed it was my machine, but I am receiving this error after a complete wipe of my machine and minimal installation, just Android Studio installed via Toolbox.

Things I have tried

  • Updated Gradle to 6.7.1 and updated the plugin to com.android.tools.build:gradle:4.2.2, same error
  • Creating a type converter for thing: ArrayList<Thing>?, same error
  • Changing thing: ArrayList<Thing>? to a List, Entities and POJOs must have a usable public constructor error
  • Changing kapt "androidx.room:room-compiler:2.3.0" to annotationProcessor "androidx.room:room-compiler:2.3.0", compiles and all test pass but I receive runtime crashes when any database methods are called
  • Changing my JDK from the embedded to JDK 11, same error
  • Adding kapt.use.worker.api=false, same error

Relevant gradle sections

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

The issue was the JDK built into newer versions of Android Studio and our current Gradle setup, particularly this block

android {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

The built in JDK included with Android Studio conflicted. Once I pointed Android Studio to a Java 1.8 JDK, it compiled on my machine and new machines.

On my current machine I installed AdoptOpenJDK 8.0.292.hs using SDKMan. On my previous machine I installed OpenJDK 1.8 using Homebrew

I then went to File -> Project Structure -> SDK Location -> JDK Location and pointed the JDK to the newly installed 1.8 instead of the built in JDK.

For SDKMan the path was /Users/me/.sdkman/candidates/java/8.0.292.hs-adpt

I had a similar issue, just remove the annotation "@Embedded" from your ArrayList property located in your Entity.

Related