@@ -27,9 +27,11 @@ import com.android.build.api.dsl.CommonExtension
2727import com.android.build.api.variant.AndroidComponentsExtension
2828import dev.shreyaspatil.composeCompilerMetricsGenerator.plugin.task.createComposeCompilerReportGenTaskForVariant
2929import dev.shreyaspatil.composeCompilerMetricsGenerator.plugin.task.executingComposeCompilerReportGenerationGradleTask
30- import dev.shreyaspatil.composeCompilerMetricsGenerator.plugin.utils.kotlinOptions
3130import org.gradle.api.Plugin
3231import org.gradle.api.Project
32+ import org.gradle.kotlin.dsl.getByType
33+ import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
34+ import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
3335
3436@Suppress(" UnstableApiUsage" )
3537class ReportGenPlugin : Plugin <Project > {
@@ -53,33 +55,38 @@ class ReportGenPlugin : Plugin<Project> {
5355 if (isComposeEnabled != true ) {
5456 error(" Jetpack Compose is not found enabled in this module '$name '" )
5557 }
56- }
5758
58- // When this method returns true it means gradle task for generating report is executing otherwise
59- // normal compilation task is executing.
60- val isFromReportGenGradleTask = target.executingComposeCompilerReportGenerationGradleTask()
61- if (isFromReportGenGradleTask) {
62- commonExtension?.configureKotlinOptionsForComposeCompilerReport(reportExt)
59+ // When this method returns true it means gradle task for generating report is executing otherwise
60+ // normal compilation task is executing.
61+ val isFromReportGenGradleTask = target.executingComposeCompilerReportGenerationGradleTask()
62+ if (isFromReportGenGradleTask) {
63+ val kotlinAndroidExt = extensions.getByType<KotlinAndroidProjectExtension >()
64+ kotlinAndroidExt.target {
65+ // Exclude for test variants, no use!
66+ compilations.filter { ! it.name.endsWith(" Test" ) }.forEach {
67+ it.kotlinOptions {
68+ configureKotlinOptionsForComposeCompilerReport(this @afterEvaluate)
69+ }
70+ }
71+ }
72+ }
6373 }
6474 }
6575
66- private fun CommonExtension <* , * , * , * >.configureKotlinOptionsForComposeCompilerReport (
67- reportExtension : ComposeCompilerReportExtension ,
68- ) {
69- kotlinOptions {
70- val outputPath = reportExtension.composeRawMetricsOutputDirectory.absolutePath
71- if (reportExtension.enableReport.get()) {
72- freeCompilerArgs + = listOf (
73- " -P" ,
74- " plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=$outputPath "
75- )
76- }
77- if (reportExtension.enableMetrics.get()) {
78- freeCompilerArgs + = listOf (
79- " -P" ,
80- " plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=$outputPath "
81- )
82- }
76+ private fun KotlinJvmOptions.configureKotlinOptionsForComposeCompilerReport (project : Project ) {
77+ val reportExtension = project.extensions.getByType<ComposeCompilerReportExtension >()
78+ val outputPath = reportExtension.composeRawMetricsOutputDirectory.absolutePath
79+ if (reportExtension.enableReport.get()) {
80+ freeCompilerArgs + = listOf (
81+ " -P" ,
82+ " plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=$outputPath "
83+ )
84+ }
85+ if (reportExtension.enableMetrics.get()) {
86+ freeCompilerArgs + = listOf (
87+ " -P" ,
88+ " plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=$outputPath "
89+ )
8390 }
8491 }
8592}
0 commit comments