简体   繁体   English

在 KMM 模块中启用 Compose 编译器

[英]Enable Compose Compiler in KMM Module

I'm working on a KMM project and ran into this article while working on improving performance https://www.jetpackcompose.app/articles/how-can-I-debug-recompositions-in-jetpack-compose我正在做一个 KMM 项目并在努力提高性能时遇到了这篇文章https://www.jetpackcompose.app/articles/how-can-I-debug-recompositions-in-jetpack-compose

My shared KMM module has a lot of data classes that are used in the UI and the section at the end of the article caught my attention.我的共享 KMM 模块有很多在 UI 中使用的数据类,文章末尾的部分引起了我的注意。

If you are using classes from modules that don't have compose enabled, the compose compiler won't be able to infer their stability如果您正在使用未启用 compose 的模块中的类,则 compose 编译器将无法推断它们的稳定性

I have a composable that takes in a data class from the shared module and displays the data similar to the article.我有一个可组合的,它从共享模块中接收数据 class 并显示类似于文章的数据。 Using the Compose debug tools, I was able to confirm that the composable wasn't marked as skippable and the data class was marked with unstable使用 Compose 调试工具,我能够确认可组合组件未被标记为skippable ,并且数据 class 被标记为unstable

I followed the second workaround they provided wrapping the data class in another data class in the Android module and it worked to mark it as stable and skippable .我遵循他们提供的第二种解决方法,将数据 class 包装在 Android 模块中的另一个数据 class 中,它可以将其标记为stableskippable

Instead of cluttering my app with tons of wrapper classes, I'd love to use their first around.我不想用大量的包装类使我的应用程序混乱,而是喜欢使用它们的第一个。

  1. Add compose support to the module that has the data class为具有数据 class 的模块添加 compose 支持

My question is how do you enable compose support for the shared portion of a KMM app?我的问题是如何为 KMM 应用程序的共享部分启用撰写支持?

I've tried enabling the plugin with the following.我尝试使用以下方法启用插件。

plugins {
  id("org.jetbrains.compose") version "1.2.0-alpha01-dev753"
}

However, I can't get Gradle to resolve the plugin.但是,我无法让 Gradle 解析插件。

I don't see a place to do it the "normal" way of setting compose to be true like in the Android build.gradle.kts file.我没有看到像 Android build.gradle.kts 文件中那样将compose设置为 true 的“正常”方式。

android {
...
    buildFeatures {
        // Enables Jetpack Compose for this module
        compose = true
    }
...
}

Is there a way to add to buildFeatures in the shared module?有没有办法添加到共享模块中的buildFeatures

I was able to get this to work.我能够让它工作。

First, in the shared build.gradle.kts file, make these changes.首先,在共享的 build.gradle.kts 文件中,进行这些更改。

android {
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.2.0"
    }
}

kotlin {
    ...
    sourceSets {
        ...
        val androidMain by getting {
            dependencies {
                implementation("androidx.compose.runtime:runtime:1.3.0-alpha02")
                ...
            }
        }
    ...
    }
}
  

Make sure the compiler version is the same as in your Android module's build file.确保编译器版本与 Android 模块的构建文件中的相同。

Then create a file and add an expect for the @Immutable compose annotation.然后创建一个文件并为@Immutable compose 注释添加一个期望。

expect annotation class Immutable()

In the Android side of the shared module add在共享模块的Android端添加

actual typealias Immutable = androidx.compose.runtime.Immutable

In the iOS side add a dummy version.在 iOS 端添加一个虚拟版本。

actual annotation class Immutable

You can then annotate your data classes with @Immutable and they will properly be marked as stable by Compose.然后,您可以使用@Immutable注释您的数据类,它们将被 Compose 正确标记为stable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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