|
1 | 1 | import com.android.build.gradle.TestedExtension
|
2 | 2 | import com.dropbox.gradle.plugins.dependencyguard.DependencyGuardPluginExtension
|
3 | 3 |
|
4 |
| -val configurationNames = when { |
5 |
| - // record the root project's *build* classpath |
6 |
| - project == rootProject -> listOf("classpath") |
7 |
| - // Android variants and their configurations are added later in the configuration phase, |
8 |
| - // so we can't look them up now using the `configurations` property. |
9 |
| - // Instead we can just hard-code "releaseRuntimeClasspath" for any module which has AGP applied. |
10 |
| - // This is actually pretty robust, since if this configuration ever changes, |
11 |
| - // dependency-guard will fail when trying to look it up. |
12 |
| - extensions.findByType<TestedExtension>() != null -> listOf("releaseRuntimeClasspath") |
13 |
| - // If we got here, we're either in an empty "parent" module without a build plugin |
14 |
| - // (and no configurations), or we're in a vanilla Kotlin module. In this case, we can just look |
15 |
| - // at configuration names. |
16 |
| - else -> configurations |
17 |
| - .map { it.name } |
18 |
| - // anything ending with 'runtimeClasspath' but not 'testRuntimeClasspath' |
19 |
| - .filter { it.matches("^(?!test)\\w*[rR]untimeClasspath$".toRegex()) } |
20 |
| -} |
| 4 | +// We have to use `afterEvaluate { ... }` because both KMP and AGP create their configurations later |
| 5 | +// in the configuration phase. If we were to try looking up those configurations eagerly as soon |
| 6 | +// as this convention plugin is applied, there would be nothing there. |
| 7 | +afterEvaluate { |
| 8 | + val configurationNames = when { |
| 9 | + // record the root project's *build* classpath |
| 10 | + project == rootProject -> listOf("classpath") |
| 11 | + |
| 12 | + // For Android modules, just hard-code `releaseRuntimeClasspath` for the release variant. |
| 13 | + // This is actually pretty robust, since if this configuration ever changes, dependency-guard |
| 14 | + // will fail when trying to look it up. |
| 15 | + extensions.findByType<TestedExtension>() != null -> listOf("releaseRuntimeClasspath") |
| 16 | + |
| 17 | + // If we got here, we're either in an empty "parent" module without a build plugin |
| 18 | + // (and no configurations), or we're in a vanilla Kotlin module. In this case, we can just look |
| 19 | + // at configuration names. |
| 20 | + else -> configurations |
| 21 | + .map { it.name } |
| 22 | + .filter { |
| 23 | + it.endsWith("runtimeClasspath", ignoreCase = true) && |
| 24 | + !it.endsWith("testRuntimeClasspath", ignoreCase = true) |
| 25 | + } |
| 26 | + } |
21 | 27 |
|
22 |
| -if (configurationNames.isNotEmpty()) { |
23 |
| - apply(plugin = "com.dropbox.dependency-guard") |
| 28 | + if (configurationNames.isNotEmpty()) { |
| 29 | + apply(plugin = "com.dropbox.dependency-guard") |
24 | 30 |
|
25 |
| - configure<DependencyGuardPluginExtension> { |
26 |
| - configurationNames.forEach { configName -> |
27 |
| - // Tell dependency-guard to check the `configName` configuration's dependencies. |
28 |
| - configuration(configName) |
| 31 | + configure<DependencyGuardPluginExtension> { |
| 32 | + configurationNames.forEach { configName -> |
| 33 | + // Tell dependency-guard to check the `configName` configuration's dependencies. |
| 34 | + configuration(configName) |
| 35 | + } |
29 | 36 | }
|
30 | 37 | }
|
31 | 38 | }
|
0 commit comments