Home > Software engineering >  Android Room in Kotlin 1.7.0
Android Room in Kotlin 1.7.0

Time:07-17

When updating to Kotlin 1.7.0, since it's required by the latest version of Jetpack Compose, I found out that Room was no longer working. I was using kapt as my annotation processor, and the compiler was throwing error messages such as:

[*] error: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.

CodePudding user response:

The fix was to migrate from kapt to ksp. KSP is the theoretical successor of kapt, and it's developed by Google. To do so, first I had to import the library into my classpath:

buildscript {
  ...
  repositories {
    ...
    classpath "com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.7.0-1.0.6"
    ...
  }
}

Check the latest version from MVN Repository just in case there has been an update, but as the time of writing this the latest version for Kotlin 1.7.0 is 1.7.0-1.0.6.

Now, in my module's build.gradle:

plugins {
  ...

  // Remove the old kapt plugin
  id 'org.jetbrains.kotlin.kapt'

  // Add the new KSP plugin
  id 'com.google.devtools.ksp'

  ...
}

...

android {
  ...
  defaultConfig {
    ...
    // Replace old compile options
    javaCompileOptions {
      annotationProcessorOptions {
        arguments  = [ "room.schemaLocation": "$projectDir/schemas".toString() ]
      }
    }
    // With the new KSP arg method
    ksp {
      arg("room.schemaLocation", "$projectDir/schemas".toString())
    }
    ...
  }
  ...
}

...

dependencies {
  ...
  // Replace all kapt calls with ksp
  // kapt "androidx.room:room-compiler:$room_version"
  ksp "androidx.room:room-compiler:$room_version"
}

Now Room should be working correctly in Kotlin 1.7.0!

CodePudding user response:

You can update the room version to 2.5.0-alpha02, which supports Kotlin 1.7.0, but it's still in alpha. Check the latest release hereenter link description here

  • Related