我想通过此官方docs启用撰写指标。
在 root gradle 中我添加了这个:
subprojects {
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
if (project.findProperty("composeCompilerReports") == "true") {
kotlinOptions.freeCompilerArgs = kotlinOptions.freeCompilerArgs + listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" +
project.buildDir.absolutePath + "/compose_reports"
)
kotlinOptions.freeCompilerArgs = kotlinOptions.freeCompilerArgs + listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
project.buildDir.absolutePath + "/compose_metrics"
)
}
}
}
}
并通过以下命令开始构建:
./gradlew assembleRelease -PcomposeCompilerReports=true --rerun-tasks
构建开始,甚至会在其构建文件夹中创建一个应用程序模块的一份报告。但据我了解,进一步的过程遇到了错误:
> Task :core-common-feature-utils:kaptGenerateStubsReleaseKotlin FAILED
e: Multiple values are not allowed for plugin option androidx.compose.compiler.plugins.kotlin:metricsDestination
Plugin "androidx.compose.compiler.plugins.kotlin" usage:
liveLiterals <true|false> Enable Live Literals code generation
liveLiteralsEnabled <true|false>
Enable Live Literals code generation (with per-file enabled flags)
generateFunctionKeyMetaClasses <true|false>
Generate function key meta classes with annotations indicating the functions and their group keys. Generally used for tooling.
sourceInformation <true|false>
Include source information in generated code
metricsDestination <path> Save compose build metrics to this folder
reportsDestination <path> Save compose build reports to this folder
intrinsicRemember <true|false>
Include source information in generated code
suppressKotlinVersionCompatibilityCheck <true|false>
Suppress Kotlin version compatibility check
generateDecoys <true|false>
Generate decoy methods in IR transform
FAILURE: Build completed with 2 failures.
此外,我注意到,每次成功后,进程都会给出不同模块的错误,并且可能会给出 2 个或 3 个相同的错误 - 最后是 stackoverflow 错误。 请帮忙想想如何克服这个问题
gradle 插件版本 7.4.1
附注据我从调查中了解到,这些模块没有在其 gradle 中
kotlin("kapt")
id("dagger.hilt.android.plugin")
创建报告。使用 kotlin("kapt") 会出现该错误。但我不知道如何在没有它的情况下编译项目,因为我正在使用 hilt。
P.P.S。当我尝试更多时,我在模块中的 build.gradle 中删除了 hilt 后成功地制作了报告。在这种情况下,命令运行到 100%。但应用程序当然不会运行。这样汇报有点“不方便”。 如果您有想法,请..
对于有同样问题的人:我刚刚通过将 gradle 文件迁移到 kts(build.gradle 到 build.gradle.kts)解决了同样的问题,现在 compose 指标可以正常工作(我使用 Hilt 和 Kapt)。
请参考这个链接
编辑:这是我的 build.gradle.kts (这是唯一的文件,单模块应用程序):
plugins {
id("com.android.application") version "7.4.1"
id("org.jetbrains.kotlin.android") version "1.8.10"
id("com.google.dagger.hilt.android") version "2.44"
id("org.jetbrains.kotlin.kapt") version "1.8.10"
}
android {
namespace = "com.target33.maintest"
compileSdk = 33
defaultConfig {
applicationId = "com.target33.maintest"
minSdk = 21
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
kotlinOptions {
jvmTarget = "1.8"
//comment following lines (freeCompilerArgs) to disable compose-metrics
freeCompilerArgs += listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" + project.buildDir.absolutePath + "/compose_metrics")
freeCompilerArgs += listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" + project.buildDir.absolutePath + "/compose_metrics")
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.4.4"
}
packagingOptions {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}
dependencies {
implementation("androidx.activity:activity-compose:1.7.0")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.1")
implementation("androidx.compose.ui:ui-tooling-preview:1.5.0-alpha01")
implementation("androidx.compose.material3:material3:1.1.0-beta01")
implementation("androidx.compose.material3:material3-window-size-class:1.1.0-beta01")
implementation("androidx.window:window:1.0.0")
implementation("androidx.datastore:datastore-preferences:1.0.0")
//Hilt libraries
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
//Navigation
implementation("androidx.navigation:navigation-compose:2.5.3")
//Splash screen
implementation("androidx.core:core-splashscreen:1.0.0")
//Immutable Collections Library for Kotlin (to make list, map, etc.. stable)
implementation("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.5")
}
kapt {
correctErrorTypes = true
}
使用这种方式:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.myapplication'
compileSdk 33
defaultConfig {
//..
}
buildTypes {
//..
}
}
dependencies {
//..
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += [
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
project.buildDir.absolutePath + "/compose_metrics",
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination="+
project.buildDir.absolutePath + "/compose_metrics",
]
}
}
不要将其添加到根 gradle 文件中。您可以使用 compose 将此代码添加到模块 gradle 文件中:
android {
kotlinOptions {
if (project.findProperty("enableComposeReports") == "true") {
val outputDir = project.buildDir.path + "/compose-reports"
freeCompilerArgs = freeCompilerArgs + listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=$outputDir",
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=$outputDir"
)
}
}
}
或者使用buildSrc脚本来防止代码重复。例如,请参阅此存储库https://github.com/olshevski/compose-navigation-reimagined/blob/main/buildSrc/src/main/kotlin/compose-compiler-reports.gradle.kts
将此脚本复制到根build.gradle中,不要忘记将“com.android.myapplication”更改为您的应用程序包。之后,你将运行这个
./gradlew assembleRelease -P com.android.myapplication.enableComposeCompilerReports=true
选项 build.gradle.kts:将此脚本复制到插件下方。
注意:如果您遇到导入问题,请检查顶级文件(如果您导入了名为
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
的包)
subprojects {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
if (project.findProperty("com.android.myapplication.enableComposeCompilerReports") == "true") {
freeCompilerArgs += listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" +
strong text File(project.buildDir, "compose_metrics").absolutePath
)
freeCompilerArgs += listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
File(project.buildDir, "compose_metrics").absolutePath
)
}
}
}
}