10

I want to enable compose metrics by this official docs.

In root gradle I added this:

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"
            )
        }
    }
}

}

and start the building by this command:

./gradlew assembleRelease -PcomposeCompilerReports=true  --rerun-tasks

the building starts and even one report for one application module in its build folder created. But as I understand the further process stuck with an error:

   > 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.

Also, I noticed that every time the process give the error with different module after one successful and can give my 2 same errors or three - and finally stackoverflow error. please, help with idea how to overcome this

gradle plugin ver 7.4.1

P.S. As I understand from investigations, the modules without in their gradle

   kotlin("kapt")
id("dagger.hilt.android.plugin")

creates the report. The using of kotlin("kapt") gives that error. But I do not know how to compile the project without it, because I am using hilt.

P.P.S. As I am trying more, I have managed to make reports after deleting hilt from build.gradle in modules. In this case the command runs till 100%. But application will not run of course. This is a little "inconvenient" to make report in such a way. Please, if you have an idea..

0

6 Answers 6

6

To the ones with the same problem: I just solved the same issue by migrating my gradle files to kts (build.gradle to build.gradle.kts) and now the compose metrics are working correctly (I use Hilt and Kapt).

Please refer to this link

EDIT: this is my build.gradle.kts (that's the only file, single module application):

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
}
    

EDIT:

To avoid deprecation on project.buildDir.absolutePath

'getter for buildDir: File' is deprecated. Deprecated in Java

just replace

project.buildDir.absolutePath + "/compose_metrics"

with

project.projectDir.toPath().toString() + "/build/compose_metrics"
Sign up to request clarification or add additional context in comments.

2 Comments

As you see I used kts from the begining. But have this problem. Can you provide your gradle file with your working skript?
I edited my original post with my build.gradle.kts
2

Use this way :

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",
        ]
    }
}

1 Comment

If I had the build.gradle as in your snippet everything would be OK. But I have id("dagger.hilt.android.plugin") kotlin("kapt") in plugins section and the error is still here. Project will not compile until I remove this.
1

Do not add it to root gradle file. You may add this code to module gradle file with compose:

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"
        )
    }
}
}

or use buildSrc scripts to prevent code duplication. See this repo for example https://github.com/olshevski/compose-navigation-reimagined/blob/main/buildSrc/src/main/kotlin/compose-compiler-reports.gradle.kts

1 Comment

Thanks for the reply. Unfortunately this wasn't help, I tried this first before asking the question. Also, in the repo you provided there is not hilt. Example with kotlin("kapt") id("dagger.hilt.android.plugin") and script at the same time would help.
0

Copy this script in the root build.gradle, don't forget change "com.android.myapplication" by your application package. After that, you will do run this ./gradlew assembleRelease -P com.android.myapplication.enableComposeCompilerReports=true

Option build.gradle.kts: Copy this script below plugins.
Note: If you have a imports problems check top-level file if you imported a package called import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
When you run this script for second time you must use ./gradlew assembleRelease -P com.android.myapplication.enableComposeCompilerReports=true --rerun-tasks

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
            )
        }
    }
}

}

Comments

0

More simply way:

Setup

Compose compiler reports are not enabled by default. You can activate them with a compiler flag. The exact setup varies depending on your project, but for projects using the Compose compiler gradle plugin you can add the following in each modules build.gradle file.

  android { ... }

  composeCompiler {
    reportsDestination = layout.buildDirectory.dir("compose_compiler")
    metricsDestination = layout.buildDirectory.dir("compose_compiler")
  }

Compose compiler reports will now be generated when building your project.

Important: Make sure to always run this on a release build to ensure accurate results.

See at https://developer.android.com/develop/ui/compose/performance/stability/diagnose

copy in each module with compose for a multi module app to generate report run the comands:

./gradlew assembleRelease -PcomposeCompilerReports=true  --rerun-tasks

Comments

-1

I had to add this to gradle.properties so it would generate the reports

kotlin.experimental.compose.compiler.reports.enabled

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.