简体   繁体   中英

Jetpack Compose and Android Room compatibility

I am trying to set up Android Room in my Jetpack Compose project. However, when trying to add...

kapt "androidx.room:room-compiler:2.2.3"

... to the app build.gradle dependencies. My build fails with the error...

e: java.lang.NoSuchMethodError: org.jetbrains.kotlin.codegen.state.GenerationState$Builder.isIrBackend(Z)Lorg/jetbrains/kotlin/codegen/state/GenerationState$Builder;

Any insight as to why this is? Can we use Android Room with Jetpack Compose yet?

I'm pretty sure that we're still in a state where annotation processors (for Room, Retrofit, etc.) and the experimental Kotlin compiler are not getting along. The recommended approach at the moment is to isolate the Compose stuff in a module that is independent of modules that need annotation processors.

This is currently not supported as kapt does not play well with Jetpack Compose. This issue is being tracked here - https://issuetracker.google.com/issues/143232368

It's actually working, but not without some changes.

For a new project created with AndroidStudio 2020.3.1 Canary 2:

In build.gradle (Project): Downgrade compose_version to 1.0.0-alpha07 and ext.kotlin_version to 1.4.10 .

In build.gradle (Module):

Under composeOptions downgrade the composeCompilerVersion to 1.4.10 .

Under dependencies change " androidx.compose.ui :ui-tooling:$compose_version" to " androidx.ui :ui-tooling:$compose_version".

In Main activity - change the import for the preview, as it depends on ui-tooling.

Add room dependencies.

Should work.

However, some newer Compose functionalities won't probably work, as they are explicitly stating, that one should use Kotlin version 1.4.20 and above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM