Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable compose metrics in android application

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

like image 948
Foenix Avatar asked Apr 29 '26 17:04

Foenix


2 Answers

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"
like image 195
target33 Avatar answered May 01 '26 07:05

target33


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",
        ]
    }
}
like image 40
Rasoul Miri Avatar answered May 01 '26 05:05

Rasoul Miri