Jetpack Compose : 使用 Compose Compiler Gradle 来设置 Compose

1,691 阅读3分钟

对于 Gradle 用户,您可以使用 Compose Compiler Gradle 插件来更轻松地设置和配置 Compose。

  • 注意 :Compose Compiler Gradle 插件仅适用于 Kotlin 2.0 及更高版本。

以下说明概述了如何设置 Compose Compiler Gradle 插件:

  1. 在 libs.versions.toml 文件中,移除对 Compose 编译器的任何引用
  2. 在“plugins”部分,添加以下新依赖项
[versions]
kotlin = "2.0.20"

[plugins]
org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

// Add this line
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
  1. 在项目根 build.gradle.kts 文件中,将以下内容添加到插件部分:
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler) apply false
}
  1. 在使用 Compose 的每个模块中,应用该插件:
  2. 不再需要在 composeOptions 中配置 kotlinCompilerExtensionVersion,并且可以将其删除。
  3. 如需使用 Gradle 插件配置 Compose 编译器,请将 composeCompiler 代码块添加到模块的顶级 build.gradle.kts 文件中。
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler)
}

composeOptions { kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get() }

android { ... }

composeCompiler {
    enableStrongSkippingMode = true
}

感谢 用户8759268773177 - 掘金 (juejin.cn) 的反馈,官方已发布 Kotlin 2.0.20 对 Kotlin 2.0.0 的性能改进和错误修复,这里就直接贴改动点,文末附上原文地址感兴趣的童鞋自行阅读:

In Kotlin 2.0.20, we introduce a new approach that changes the way the outputs of Kotlin/JVM compilations, such as JAR files, are shared between projects. With this approach, Gradle's apiElements configuration now has a secondary variant that provides access to the directory containing compiled .class files. When configured, your project uses this directory instead of requesting the compressed JAR artifact during compilation. This reduces the number of times JAR files are compressed and decompressed, especially for incremental builds.

Our testing shows that this new approach can provide build performance improvements for Linux and macOS hosts. However, on Windows hosts, we have seen a degradation in performance due to how Windows handles I/O operations when working with files.

To try this new approach, add the following property to your gradle.properties file:

kotlin.jvm.addClassesVariant=true

Added task dependency for rare cases when the compile task lacks one on an artifact

Prior to 2.0.20, we found that there were scenarios where a compile task was missing a task dependency for one of its artifact inputs. This meant that the result of the dependent compile task was unstable, as sometimes the artifact had been generated in time, but sometimes, it hadn't.

To fix this issue, the Kotlin Gradle plugin now automatically adds the required task dependency in these scenarios.

In very rare cases, we've found that this new behavior can cause a circular dependency error. For example, if you have multiple compilations where one compilation can see all internal declarations of the other, and the generated artifact relies on the output of both compilation tasks, you could see an error like:

FAILURE: Build failed with an exception.

What went wrong:
Circular dependency between the following tasks:
:lib:compileKotlinJvm
--- :lib:jvmJar
     --- :lib:compileKotlinJvm (*)
(*) - details omitted (listed previously)

To fix this circular dependency error, we've added a Gradle property: archivesTaskOutputAsFriendModule.

By default, this property is set to true to track the task dependency. To disable the use of the artifact in the compilation task, so that no task dependency is required, add the following in your gradle.properties file:

kotlin.build.archivesTaskOutputAsFriendModule=false

Fix for the unnecessary recompositions issue introduced in 2.0.0

Compose compiler 2.0.0 has an issue where it sometimes incorrectly infers the stability of types in multiplatform projects with non-JVM targets. This can lead to unnecessary (or even endless) recompositions. We strongly recommended updating your Compose apps made for Kotlin 2.0.0 to version 2.0.10 or newer.

If your app is built with Compose compiler 2.0.10 or newer but uses dependencies built with version 2.0.0, these older dependencies may still cause recomposition issues. To prevent this, update your dependencies to versions built with the same Compose compiler as your app.

New way to configure compiler options

We've introduced a new option configuration mechanism to avoid the churn of top-level parameters. It's harder for the Compose compiler team to test things out by creating or removing top-level entries for the composeCompiler {} block. So, options such as strong skipping mode and non-skipping group optimizations are now enabled through the featureFlags property. This property will be used to test new Compose compiler options that will eventually become default.

This change has also been applied to the Compose compiler Gradle plugin. To configure feature flags going forward, use the following syntax (this code will flip all of the default values):

composeCompiler {
    featureFlags = setOf(
        ComposeFeatureFlag.IntrinsicRemember.disabled(),
        ComposeFeatureFlag.OptimizeNonSkippingGroups,
        ComposeFeatureFlag.StrongSkipping.disabled()
    )
}

Or, if you are configuring the Compose compiler directly, use the following syntax:

-P plugin:androidx.compose.compiler.plugins.kotlin:featureFlag=IntrinsicRemember

The enableIntrinsicRememberenableNonSkippingGroupOptimization, and enableStrongSkippingMode properties have been therefore deprecated.

原文地址:

What's new in Kotlin 2.0.20 | Kotlin Documentation (kotlinlang.org)

示例项目

Thanks

以上就是本篇文章的全部内容,如有问题欢迎指出,我们一起进步。
如果喜欢的话希望点个赞吧,您的鼓励是我前进的动力。
谢谢~~