Android Room in Kotlin 1.7.0
Asked Answered
K

3

5

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.
Kiely answered 14/7, 2022 at 10:21 Comment(0)
K
10

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!

Kiely answered 14/7, 2022 at 10:21 Comment(1)
Also Glide now support Ksp with 25% faster.Reahard
K
1

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 here

Karat answered 16/7, 2022 at 14:36 Comment(0)
M
0

Upgrading Room to stable version 2.4.3 has fixed this issue (for me) - I can now build the project with Kotlin 1.7.10.

Militarize answered 4/8, 2022 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.