Foe*_*nix 8 android gradle build.gradle
我想通过此官方文档启用撰写指标。
在 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"
)
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
并通过以下命令启动构建:
./gradlew assembleRelease -PcomposeCompilerReports=true --rerun-tasks
Run Code Online (Sandbox Code Playgroud)
构建开始,甚至会为其构建文件夹中的一个应用程序模块创建一份报告。但据我了解,进一步的过程出现了错误:
> 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.
Run Code Online (Sandbox Code Playgroud)
另外,我注意到,每次成功后,进程都会给出不同模块的错误,并且可能会给出 2 个或 3 个相同的错误 - 最后是 stackoverflow 错误。请帮忙想想如何克服这个问题
gradle 插件版本 7.4.1
PS 据我从调查中了解到,其 gradle 中没有的模块
kotlin("kapt")
id("dagger.hilt.android.plugin")
Run Code Online (Sandbox Code Playgroud)
创建报告。使用 kotlin("kapt") 会出现该错误。但我不知道如何在没有它的情况下编译项目,因为我正在使用 hilt。
PPS 当我尝试更多时,我在模块中的 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
}
Run Code Online (Sandbox Code Playgroud)