13

I'm using Android Studio for Android development with Kotlin. I have more than 40 Android modules in the project: some of them are Java module, some of theme are android module.

Kotlin Version = "1.8.20"
Hilt Version = "2.45"
Compose BOM Version = "2023.01.00"
Compose Compiler Version = "1.4.3"
com.android.library = "7.4.1"
com.android.application = "7.4.1"

I want to run Compose Metrics Considering the following:

buildscript {
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
    id 'com.google.dagger.hilt.android' version '2.45' apply false
}

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            if (project.findProperty("myapp.enableComposeCompilerReports") == "true") {
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" +
                                project.buildDir.absolutePath + "/compose_metrics"
                ]
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
                                project.buildDir.absolutePath + "/compose_metrics"
                ]
            }
        }
    }
}

But I see an error as follows:

Multiple values are not allowed for plugin option androidx.compose.compiler.plugins.kotlin:reportsDestination

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

In another simple project this problem came when I added Hilt

If you add the kapt plugin you'll run into this issue.

Add id 'org.jetbrains.kotlin.kapt' version '1.8.10' apply false to the top-level build.gradle Add id 'org.jetbrains.kotlin.kapt' to the app's build.gradle

Run ./gradlew assembleDebug -Pmyapp.enableComposeCompilerReports=true

And the issue will reproduce without any Hilt dependencies added.

5
  • Seems like a known issue, it has been fixed in 1.8.20-RC of kapt but for me (I need jetpack compose compiler) 1.8.20-RC of kotlin has got no compose support yet: link Commented Mar 27, 2023 at 10:49
  • If you migrate your gradle files to kts and use version catalog and plugin convention. I think your problem will be solved Commented Mar 27, 2023 at 11:05
  • Unfortunately I use Hilt, which currently uses KAPT for annotation processing and doesn't support KSP yet Commented Mar 27, 2023 at 14:10
  • 1
    it doesn't matter. i suggest to try it out Commented Mar 27, 2023 at 14:30
  • You are right! I've converted my build.gradle into build.gradle.kts and now the metrics are working like a charm. Thanks! Commented Mar 27, 2023 at 16:15

1 Answer 1

0

The kapt plugin creates its own special Kotlin compilation tasks (e.g., kaptDebugKotlin) to generate stubs for annotation processing. Your subprojects script, which configures all tasks of type KotlinCompile, is correctly applying the Compose compiler settings to your main code, but it's also applying them to these kapt tasks.

This causes a conflict, leading to the "Multiple values are not allowed" error.

You just need to add a check to skip any task whose name starts with "kapt".

build.gradle

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { task ->
        if (task.name.startsWith("kapt")) {
            return
        }

        kotlinOptions {
            if (project.findProperty("myapp.enableComposeCompilerReports") == "true") {
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" +
                                project.buildDir.absolutePath + "/compose_metrics"
                ]
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
                                project.buildDir.absolutePath + "/compose_metrics"
                ]
            }
        }
    }
}
New contributor
Kergst is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.