Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Android variant compile classpath from compileConfiguration #5152

Merged
merged 2 commits into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,7 +3,6 @@ package io.gitlab.arturbosch.detekt
import io.gitlab.arturbosch.detekt.testkit.DslGradleRunner
import io.gitlab.arturbosch.detekt.testkit.ProjectLayout
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -274,7 +273,6 @@ class DetektAndroidSpec {

@Test
@DisplayName("task :android_lib:detektMain")
@Disabled("https://github.com/detekt/detekt/issues/5150")
fun libDetektMain() {
gradleRunner.runTasksAndCheckResult(
"--configuration-cache",
Expand All @@ -293,6 +291,31 @@ class DetektAndroidSpec {
)
}
}

@Test
@DisplayName("task :android_lib:detektTest")
fun libDetektTest() {
gradleRunner.runTasksAndCheckResult(
"--configuration-cache",
":android_lib:detektTest",
) { buildResult ->
assertThat(buildResult.output).contains("Configuration cache")
assertThat(buildResult.output).containsPattern(
"""--baseline \S*[/\\]detekt-baseline-debugUnitTest.xml """
)
assertThat(buildResult.output).containsPattern(
"""--baseline \S*[/\\]detekt-baseline-debugAndroidTest.xml """
)
assertThat(buildResult.output).contains("--report xml:")
assertThat(buildResult.output).contains("--report sarif:")
assertThat(buildResult.output).doesNotContain("--report txt:")
assertThat(buildResult.tasks.map { it.path }).containsAll(
listOf(
":android_lib:detektTest",
)
)
}
}
}

@Nested
Expand Down
Expand Up @@ -14,6 +14,7 @@ import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.attributes.Attribute
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.TaskProvider
Expand Down Expand Up @@ -117,7 +118,11 @@ internal fun Project.registerAndroidDetektTask(
setSource(variant.sourceSets.map { it.javaDirectories + it.kotlinDirectories })
extraInputSource?.let { source(it) }
classpath.setFrom(
variant.getCompileClasspath(null).filter { it.exists() },
variant.compileConfiguration.incoming.artifactView { view ->
view.attributes {
it.attribute(Attribute.of("artifactType", String::class.java), "jar")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why do we need to restrict on this attribute?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it there's a long error from Gradle for multiple detekt tasks. The reason is outlined in this comment: gradle/gradle#5953 (comment), specifically:

There is no single artifact for an Android project. Each project provides an AARs, a JAR, a classes folder, a resources folder etc). The Android team decided not to have a default fallback to prevent silent failures.

And gradle/gradle#5953 (comment)

as a consumer you need to be explicit what you are interested in. Do you want classes, resources, manifests, whole AAR files etc.

The error:

    Could not determine the dependencies of task ':app:detektTest'.
    > Could not create task ':app:detektDebugAndroidTest'.
       > Could not resolve all files for configuration ':app:debugAndroidTestCompileClasspath'.
          > The consumer was configured to find an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'. However we cannot choose between the following variants of project :app:
              - Configuration ':app:debugApiElements' variant android-base-module-metadata declares an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
                  - Unmatched attributes:
                      - Provides attribute 'artifactType' with value 'android-base-module-metadata' but the consumer didn't ask for it
                      - Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'debug' but the consumer didn't ask for it
                      - Provides a library but the consumer didn't ask for it
              - Configuration ':app:debugApiElements' variant android-feature-all-metadata declares an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
                  - Unmatched attributes:
                      - Provides attribute 'artifactType' with value 'android-feature-all-metadata' but the consumer didn't ask for it
                      - Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'debug' but the consumer didn't ask for it
                      - Provides a library but the consumer didn't ask for it
              - Configuration ':app:debugApiElements' variant android-feature-res-ap_ declares an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
                  - Unmatched attributes:
                      - Provides attribute 'artifactType' with value 'android-feature-res-ap_' but the consumer didn't ask for it
                      - Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'debug' but the consumer didn't ask for it
                      - Provides a library but the consumer didn't ask for it
              - Configuration ':app:debugApiElements' variant android-feature-signing-config-data declares an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
                  - Unmatched attributes:
                      - Provides attribute 'artifactType' with value 'android-feature-signing-config-data' but the consumer didn't ask for it
                      - Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'debug' but the consumer didn't ask for it
                      - Provides a library but the consumer didn't ask for it
              - Configuration ':app:debugApiElements' variant android-feature-signing-config-versions declares an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
                  - Unmatched attributes:
                      - Provides attribute 'artifactType' with value 'android-feature-signing-config-versions' but the consumer didn't ask for it
                      - Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'debug' but the consumer didn't ask for it
                      - Provides a library but the consumer didn't ask for it
              - Configuration ':app:debugApiElements' variant android-java-res declares an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
                  - Unmatched attributes:
                      - Provides attribute 'artifactType' with value 'android-java-res' but the consumer didn't ask for it
                      - Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'debug' but the consumer didn't ask for it
                      - Provides a library but the consumer didn't ask for it
              - Configuration ':app:debugApiElements' variant android-manifest-metadata declares an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
                  - Unmatched attributes:
                      - Provides attribute 'artifactType' with value 'android-manifest-metadata' but the consumer didn't ask for it
                      - Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'debug' but the consumer didn't ask for it
                      - Provides a library but the consumer didn't ask for it

}
}.files,
bootClasspath,
javaCompileDestination(variant),
)
Expand All @@ -141,7 +146,11 @@ internal fun Project.registerAndroidCreateBaselineTask(
setSource(variant.sourceSets.map { it.javaDirectories + it.kotlinDirectories })
extraInputSource?.let { source(it) }
classpath.setFrom(
variant.getCompileClasspath(null).filter { it.exists() },
variant.compileConfiguration.incoming.artifactView { view ->
view.attributes {
it.attribute(Attribute.of("artifactType", String::class.java), "jar")
}
}.files,
bootClasspath,
javaCompileDestination(variant),
)
Expand Down