aboutsummaryrefslogtreecommitdiff
path: root/integration-tests/gradle/build.gradle.kts
blob: 963b7187329f9b07331e91b210714cff829dd691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
val runningGradle5 = gradle.gradleVersion.startsWith("5.")

val guavaVersionJre =
  "<version>(.*)</version>".toRegex().find(file("../../pom.xml").readText())?.groups?.get(1)?.value
    ?: error("version not found in pom")

val expectedReducedRuntimeClasspathAndroidVersion =
  setOf(
    "guava-${guavaVersionJre.replace("jre", "android")}.jar",
    "failureaccess-1.0.2.jar",
    "jsr305-3.0.2.jar",
    "checker-qual-3.41.0.jar",
    "error_prone_annotations-2.23.0.jar",
    "listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar"
  )
val expectedReducedRuntimeClasspathJreVersion =
  setOf(
    "guava-$guavaVersionJre.jar",
    "failureaccess-1.0.2.jar",
    "jsr305-3.0.2.jar",
    "checker-qual-3.41.0.jar",
    "error_prone_annotations-2.23.0.jar",
    "listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar"
  )
val expectedCompileClasspathAndroidVersion =
  expectedReducedRuntimeClasspathAndroidVersion + setOf("j2objc-annotations-2.8.jar")
val expectedCompileClasspathJreVersion =
  expectedReducedRuntimeClasspathJreVersion + setOf("j2objc-annotations-2.8.jar")

val extraLegacyDependencies = setOf("google-collections-1.0.jar")

buildscript {
  val agpVersion = if (gradle.gradleVersion.startsWith("5.")) "3.6.4" else "7.0.4"
  repositories {
    google()
    mavenCentral()
  }
  dependencies {
    classpath("com.android.tools.build:gradle:$agpVersion") {
      exclude(
        group = "org.jetbrains.trove4j"
      ) // Might not be available on Maven Central and not needed for this test
    }
  }
}

subprojects {
  if (name.endsWith("Java")) {
    apply(plugin = "java-library")
  } else {
    apply(plugin = "com.android.application")
    the<com.android.build.gradle.AppExtension>().compileSdkVersion(30)
  }

  var expectedClasspath =
    if (runningGradle5) {
      // without Gradle Module Metadata (only the POM is used)
      // - variant decision is made based on version suffix (android/jre) and not on the actual
      // environment
      // - runtime classpath equals the compile classpath
      // - dependency conflict with Google Collections is not detected
      if (name.startsWith("android")) {
        expectedCompileClasspathAndroidVersion + extraLegacyDependencies
      } else {
        expectedCompileClasspathJreVersion + extraLegacyDependencies
      }
    } else {
      // with Gradle Module Metadata
      // - variant is chosen based on the actual environment, independent of version suffix
      // - reduced runtime classpath is used (w/o annotation libraries)
      // - capability conflicts are detected with Google Collections
      if (name.contains("Android") && !name.contains("JreConstraint")) {
        when {
          name.contains("RuntimeClasspath") -> {
            expectedReducedRuntimeClasspathAndroidVersion
          }
          name.contains("CompileClasspath") -> {
            expectedCompileClasspathAndroidVersion
          }
          else -> {
            error("unexpected classpath type: $name")
          }
        }
      } else {
        when {
          name.contains("RuntimeClasspath") -> {
            expectedReducedRuntimeClasspathJreVersion
          }
          name.contains("CompileClasspath") -> {
            expectedCompileClasspathJreVersion
          }
          else -> {
            error("unexpected classpath type: $name")
          }
        }
      }
    }
  val guavaVersion =
    if (name.startsWith("android")) {
      guavaVersionJre.replace("jre", "android")
    } else {
      guavaVersionJre
    }
  val javaVersion = JavaVersion.VERSION_1_8

  repositories {
    mavenCentral()
    mavenLocal()
  }
  val java = the<JavaPluginExtension>()
  java.targetCompatibility = javaVersion
  java.sourceCompatibility = javaVersion

  if (!runningGradle5) {
    configurations.all {
      resolutionStrategy.capabilitiesResolution {
        withCapability("com.google.collections:google-collections") {
          candidates
            .find {
              val idField =
                it.javaClass.getDeclaredMethod(
                  "getId"
                ) // reflective access to make this compile with Gradle 5
              (idField.invoke(it) as ModuleComponentIdentifier).module == "guava"
            }
            ?.apply { select(this) }
        }
      }
    }

    if (name.contains("AndroidConstraint")) {
      dependencies {
        constraints {
          "api"("com.google.guava:guava") {
            attributes {
              // if the Gradle version is 7+, you can use
              // TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE
              attribute(Attribute.of("org.gradle.jvm.environment", String::class.java), "android")
            }
          }
        }
      }
      configurations.all {
        resolutionStrategy.capabilitiesResolution {
          withCapability("com.google.guava:guava") {
            candidates
              .find {
                val variantName = it.javaClass.getDeclaredMethod("getVariantName")
                (variantName.invoke(it) as String).contains("android")
              }
              ?.apply { select(this) }
          }
        }
      }
    }

    if (name.contains("JreConstraint")) {
      dependencies {
        constraints {
          "api"("com.google.guava:guava") {
            attributes {
              // if the Gradle version is 7+, you can use
              // TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE
              attribute(
                Attribute.of("org.gradle.jvm.environment", String::class.java),
                "standard-jvm"
              )
            }
          }
        }
      }
      configurations.all {
        resolutionStrategy.capabilitiesResolution {
          withCapability("com.google.guava:guava") {
            candidates
              .find {
                val variantName = it.javaClass.getDeclaredMethod("getVariantName")
                (variantName.invoke(it) as String).contains("jre")
              }
              ?.apply { select(this) }
          }
        }
      }
    }
  }

  dependencies {
    "api"("com.google.collections:google-collections:1.0")
    "api"("com.google.guava:listenablefuture:1.0")
    "api"("com.google.guava:guava:$guavaVersion")
  }

  tasks.register("testClasspath") {
    doLast {
      val classpathConfiguration =
        if (project.name.contains("RuntimeClasspath")) {
          if (project.name.endsWith("Java")) configurations["runtimeClasspath"]
          else configurations["debugRuntimeClasspath"]
        } else if (project.name.contains("CompileClasspath")) {
          if (project.name.endsWith("Java")) configurations["compileClasspath"]
          else configurations["debugCompileClasspath"]
        } else {
          error("unexpected classpath type: " + project.name)
        }

      val actualClasspath = classpathConfiguration.files.map { it.name }.toSet()
      if (actualClasspath != expectedClasspath) {
        throw RuntimeException(
          """
                    Expected: ${expectedClasspath.sorted()}
                    Actual:   ${actualClasspath.sorted()}
          """
            .trimIndent()
        )
      }
    }
  }
}