Skip to content

Commit b8cc54d

Browse files
authored
Merge pull request #180 from gradle/gh/7.0experimental
Change CompileLibraryResourcesWorkaround to warn when experimental flags are not used
2 parents 4fa1fa8 + 39ca807 commit b8cc54d

8 files changed

+179
-36
lines changed

src/main/groovy/org/gradle/android/AndroidCacheFixPlugin.groovy

+12-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.gradle.android
22

3-
import com.android.builder.model.Version
43
import com.google.common.collect.ImmutableList
54
import groovy.transform.CompileStatic
65
import org.gradle.android.workarounds.CompileLibraryResourcesWorkaround_4_0
@@ -17,22 +16,17 @@ import org.gradle.android.workarounds.Workaround
1716
import org.gradle.android.workarounds.WorkaroundContext
1817
import org.gradle.api.Plugin
1918
import org.gradle.api.Project
19+
import org.gradle.util.GradleVersion
2020
import org.gradle.util.VersionNumber
2121
import org.slf4j.Logger
2222
import org.slf4j.LoggerFactory
2323

24-
import java.util.concurrent.atomic.AtomicBoolean
25-
26-
import static org.gradle.android.Versions.SUPPORTED_ANDROID_VERSIONS
27-
import static org.gradle.android.Versions.android
24+
import static org.gradle.android.Versions.*
2825

2926
@CompileStatic
3027
class AndroidCacheFixPlugin implements Plugin<Project> {
3128
private static final Logger LOGGER = LoggerFactory.getLogger(AndroidCacheFixPlugin)
3229

33-
static final String IGNORE_VERSION_CHECK_PROPERTY = "org.gradle.android.cache-fix.ignoreVersionCheck"
34-
public static final VersionNumber CURRENT_ANDROID_VERSION = android(Version.ANDROID_GRADLE_PLUGIN_VERSION)
35-
3630
private final List<Workaround> workarounds = [] as List<Workaround>
3731

3832
private static boolean isSupportedAndroidVersion(Project project) {
@@ -42,8 +36,8 @@ class AndroidCacheFixPlugin implements Plugin<Project> {
4236

4337
private static boolean isMaybeSupportedAndroidVersion(Project project) {
4438
return SystemPropertiesCompat.getBoolean(IGNORE_VERSION_CHECK_PROPERTY, project) ||
45-
(CURRENT_ANDROID_VERSION <= Versions.latestAndroidVersion() &&
46-
CURRENT_ANDROID_VERSION >= Versions.earliestMaybeSupportedAndroidVersion())
39+
(CURRENT_ANDROID_VERSION <= latestAndroidVersion() &&
40+
CURRENT_ANDROID_VERSION >= earliestMaybeSupportedAndroidVersion())
4741
}
4842

4943
static List<Workaround> initializeWorkarounds(Project project) {
@@ -86,6 +80,14 @@ class AndroidCacheFixPlugin implements Plugin<Project> {
8680
workaround.apply(context)
8781
appliedWorkarounds += workaround.getClass().simpleName - "Workaround"
8882
}
83+
84+
if (GradleVersion.current() >= GradleVersion.version('6.1')) {
85+
project.gradle.sharedServices.registerIfAbsent("warnings", WarningsService.class) {}.get()
86+
} else {
87+
project.gradle.buildFinished {
88+
Warnings.resetAll()
89+
}
90+
}
8991
}
9092

9193
static List<Workaround> getWorkaroundsToApply(
@@ -118,21 +120,4 @@ class AndroidCacheFixPlugin implements Plugin<Project> {
118120
}
119121
workaroundsBuilder.build()
120122
}
121-
122-
private enum Warnings {
123-
MAYBE_SUPPORTED_ANDROID_VERSION("WARNING: Android plugin ${CURRENT_ANDROID_VERSION} has not been tested with this version of the Android cache fix plugin, although it may work. We test against only the latest patch release versions of Android Gradle plugin: ${SUPPORTED_ANDROID_VERSIONS.join(", ")}. If ${CURRENT_ANDROID_VERSION} is newly released, we may not have had a chance to release a version tested against it yet. Proceed with caution. You can suppress this warning with with -D${IGNORE_VERSION_CHECK_PROPERTY}=true.")
124-
125-
private final String warning
126-
private final AtomicBoolean warned = new AtomicBoolean()
127-
128-
Warnings(String warning) {
129-
this.warning = warning
130-
}
131-
132-
void warnOnce(org.gradle.api.logging.Logger logger) {
133-
if (!warned.getAndSet(true)) {
134-
logger.warn(warning)
135-
}
136-
}
137-
}
138123
}

src/main/groovy/org/gradle/android/Versions.groovy

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.gradle.android
22

3+
import com.android.builder.model.Version
34
import com.google.common.collect.ImmutableMultimap
45
import com.google.common.collect.ImmutableSortedSet
56
import com.google.common.collect.Multimap
@@ -15,6 +16,8 @@ class Versions {
1516
static final Set<GradleVersion> SUPPORTED_GRADLE_VERSIONS
1617
static final Set<VersionNumber> SUPPORTED_ANDROID_VERSIONS
1718
static final Multimap<VersionNumber, GradleVersion> SUPPORTED_VERSIONS_MATRIX
19+
static final VersionNumber CURRENT_ANDROID_VERSION
20+
static final String IGNORE_VERSION_CHECK_PROPERTY = "org.gradle.android.cache-fix.ignoreVersionCheck"
1821

1922
static {
2023
def versions = new JsonSlurper().parse(AndroidCacheFixPlugin.classLoader.getResource("versions.json"))
@@ -29,6 +32,8 @@ class Versions {
2932
SUPPORTED_VERSIONS_MATRIX = matrix
3033
SUPPORTED_ANDROID_VERSIONS = ImmutableSortedSet.copyOf(matrix.keySet())
3134
SUPPORTED_GRADLE_VERSIONS = ImmutableSortedSet.copyOf(matrix.values())
35+
36+
CURRENT_ANDROID_VERSION = android(Version.ANDROID_GRADLE_PLUGIN_VERSION)
3237
}
3338

3439
static VersionNumber android(String version) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.gradle.android
2+
3+
import org.gradle.android.workarounds.CompileLibraryResourcesWorkaround_7_0
4+
5+
import java.util.concurrent.atomic.AtomicBoolean
6+
7+
enum Warnings {
8+
MAYBE_SUPPORTED_ANDROID_VERSION("WARNING: Android plugin ${Versions.CURRENT_ANDROID_VERSION} has not been tested with this version of the Android cache fix plugin, although it may work. We test against only the latest patch release versions of Android Gradle plugin: ${Versions.SUPPORTED_ANDROID_VERSIONS.join(", ")}. If ${Versions.CURRENT_ANDROID_VERSION} is newly released, we may not have had a chance to release a version tested against it yet. Proceed with caution. You can suppress this warning with with -D${Versions.IGNORE_VERSION_CHECK_PROPERTY}=true."),
9+
USE_COMPILE_LIBRARY_RESOURCES_EXPERIMENTAL("WARNING: Android plugin ${Versions.CURRENT_ANDROID_VERSION} has experimental support for using relative path sensitivity with CompileLibraryResourcesTask inputs which will provide more build cache hits and improve build speed. Set '${CompileLibraryResourcesWorkaround_7_0.CACHE_COMPILE_LIB_RESOURCES}=true' and '${CompileLibraryResourcesWorkaround_7_0.ENABLE_SOURCE_SET_PATHS_MAP}=true' in gradle.properties to enable this support.")
10+
11+
final String warning
12+
private final AtomicBoolean warned = new AtomicBoolean()
13+
14+
Warnings(String warning) {
15+
this.warning = warning
16+
}
17+
18+
void warnOnce(org.gradle.api.logging.Logger logger) {
19+
if (!warned.getAndSet(true)) {
20+
logger.warn(warning)
21+
}
22+
}
23+
24+
void reset() {
25+
warned.set(false)
26+
}
27+
28+
static void resetAll() {
29+
values().each {it.reset() }
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.gradle.android
2+
3+
import org.gradle.api.services.BuildService
4+
import org.gradle.api.services.BuildServiceParameters
5+
6+
abstract class WarningsService implements BuildService<BuildServiceParameters.None>, AutoCloseable {
7+
@Override
8+
void close() throws Exception {
9+
Warnings.resetAll()
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
package org.gradle.android.workarounds
22

33
import org.gradle.android.AndroidIssue
4+
import org.gradle.android.Warnings
5+
import org.gradle.api.Project
46

57
/**
6-
* Fixes the cacheability issue with CompileLibraryResourcesWorkaround where the inputDirectories field is
7-
* treated as an input with absolute path sensitivity.
8-
* This is the same as the {@link CompileLibraryResourcesWorkaround_4_0} but the field was renamed with a new type
9-
* in 4.2.0-alpha09.
8+
* Warns if the user is not using experimental support for relative path sensitivity that was added
9+
* with 7.0.0-alpha09.
1010
*/
1111
@AndroidIssue(introducedIn = "7.0.0-alpha09", fixedIn = [], link = "https://issuetracker.google.com/issues/155218379")
12-
class CompileLibraryResourcesWorkaround_7_0 extends AbstractCompileLibraryResourcesWorkaround_4_2_orHigher {
12+
class CompileLibraryResourcesWorkaround_7_0 implements Workaround {
13+
public static final String ENABLE_SOURCE_SET_PATHS_MAP = "android.experimental.enableSourceSetPathsMap"
14+
public static final String CACHE_COMPILE_LIB_RESOURCES = "android.experimental.cacheCompileLibResources"
15+
16+
@Override
17+
void apply(WorkaroundContext context) {
18+
boolean enableSourceSetPathsMap = Boolean.valueOf(context.project.findProperty(ENABLE_SOURCE_SET_PATHS_MAP) as String)
19+
boolean cacheCompileLibResources = Boolean.valueOf(context.project.findProperty(CACHE_COMPILE_LIB_RESOURCES) as String)
20+
21+
if (!(enableSourceSetPathsMap && cacheCompileLibResources)) {
22+
Warnings.USE_COMPILE_LIBRARY_RESOURCES_EXPERIMENTAL.warnOnce(context.project.logger)
23+
}
24+
}
25+
1326
@Override
14-
String getPropertyName() {
15-
return "inputDirectoriesAsAbsolute"
27+
boolean canBeApplied(Project project) {
28+
return true
1629
}
1730
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.gradle.android
2+
3+
import org.gradle.android.workarounds.CompileLibraryResourcesWorkaround_7_0
4+
import org.junit.Assume
5+
6+
@MultiVersionTest
7+
class CompileLibraryResourcesWorkaround_7_0Test extends AbstractTest {
8+
def "warns when experimental flags are not provided"() {
9+
Assume.assumeTrue(TestVersions.latestAndroidVersionForCurrentJDK() >= Versions.android("7.0.0-alpha09"))
10+
11+
SimpleAndroidApp.builder(temporaryFolder.root, cacheDir)
12+
.withAndroidVersion(TestVersions.latestAndroidVersionForCurrentJDK())
13+
.withKotlinDisabled()
14+
.build()
15+
.writeProject()
16+
17+
when:
18+
def result = withGradleVersion(TestVersions.latestGradleVersion().version)
19+
.withProjectDir(temporaryFolder.root)
20+
.withArguments(
21+
"-P${CompileLibraryResourcesWorkaround_7_0.ENABLE_SOURCE_SET_PATHS_MAP}=false",
22+
"-P${CompileLibraryResourcesWorkaround_7_0.CACHE_COMPILE_LIB_RESOURCES}=false",
23+
'assembleDebug'
24+
)
25+
.build()
26+
27+
then:
28+
result.output.count(warningForAndroidVersion(TestVersions.latestAndroidVersionForCurrentJDK().toString())) == 1
29+
30+
when:
31+
result = withGradleVersion(TestVersions.latestGradleVersion().version)
32+
.withProjectDir(temporaryFolder.root)
33+
.withArguments(
34+
"-P${CompileLibraryResourcesWorkaround_7_0.ENABLE_SOURCE_SET_PATHS_MAP}=false",
35+
"-P${CompileLibraryResourcesWorkaround_7_0.CACHE_COMPILE_LIB_RESOURCES}=false",
36+
'assembleDebug'
37+
)
38+
.build()
39+
40+
then:
41+
result.output.count(warningForAndroidVersion(TestVersions.latestAndroidVersionForCurrentJDK().toString())) == 1
42+
}
43+
44+
def "does not warn when experimental flags are provided"() {
45+
Assume.assumeTrue(TestVersions.latestAndroidVersionForCurrentJDK() >= Versions.android("7.0.0-alpha09"))
46+
47+
SimpleAndroidApp.builder(temporaryFolder.root, cacheDir)
48+
.withAndroidVersion(TestVersions.latestAndroidVersionForCurrentJDK())
49+
.withKotlinDisabled()
50+
.build()
51+
.writeProject()
52+
53+
when:
54+
def result = withGradleVersion(TestVersions.latestGradleVersion().version)
55+
.withProjectDir(temporaryFolder.root)
56+
.withArguments(
57+
"-P${CompileLibraryResourcesWorkaround_7_0.ENABLE_SOURCE_SET_PATHS_MAP}=true",
58+
"-P${CompileLibraryResourcesWorkaround_7_0.CACHE_COMPILE_LIB_RESOURCES}=true",
59+
'assembleDebug'
60+
)
61+
.build()
62+
63+
then:
64+
result.output.count(warningForAndroidVersion(TestVersions.latestAndroidVersionForCurrentJDK().toString())) == 0
65+
}
66+
67+
def "does not warn for versions that do not support experimental flag"() {
68+
Assume.assumeTrue(TestVersions.latestAndroidVersionForCurrentJDK() < Versions.android("7.0.0-alpha09"))
69+
70+
SimpleAndroidApp.builder(temporaryFolder.root, cacheDir)
71+
.withAndroidVersion(TestVersions.latestAndroidVersionForCurrentJDK())
72+
.withKotlinDisabled()
73+
.build()
74+
.writeProject()
75+
76+
when:
77+
def result = withGradleVersion(TestVersions.latestGradleVersion().version)
78+
.withProjectDir(temporaryFolder.root)
79+
.withArguments(
80+
"-P${CompileLibraryResourcesWorkaround_7_0.ENABLE_SOURCE_SET_PATHS_MAP}=false",
81+
"-P${CompileLibraryResourcesWorkaround_7_0.CACHE_COMPILE_LIB_RESOURCES}=false",
82+
'assembleDebug'
83+
)
84+
.build()
85+
86+
then:
87+
result.output.count(warningForAndroidVersion(TestVersions.latestAndroidVersionForCurrentJDK().toString())) == 0
88+
}
89+
90+
private static String warningForAndroidVersion(String androidVersion) {
91+
return Warnings.USE_COMPILE_LIBRARY_RESOURCES_EXPERIMENTAL.warning.replaceAll('Android plugin [^\\s]+', "Android plugin ${androidVersion}")
92+
}
93+
}

src/test/groovy/org/gradle/android/CrossVersionOutcomeAndRelocationTest.groovy

+5-2
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,18 @@ class CrossVersionOutcomeAndRelocationTest extends AbstractTest {
541541
builder.expect(':app:desugarDebugFileDependencies', FROM_CACHE)
542542
builder.expect(':app:desugarReleaseFileDependencies', FROM_CACHE)
543543
builder.expect(':app:extractReleaseNativeSymbolTables', NO_SOURCE)
544+
builder.expect(':app:mapDebugSourceSetPaths', SUCCESS)
545+
builder.expect(':app:mapReleaseSourceSetPaths', SUCCESS)
544546
builder.expect(':app:mergeDebugNativeLibs', NO_SOURCE)
545547
builder.expect(':app:mergeReleaseNativeLibs', NO_SOURCE)
546548
builder.expect(':app:mergeDebugResources', SUCCESS)
547549
builder.expect(':app:mergeReleaseResources', SUCCESS)
548-
builder.expect(':app:processDebugResources', SUCCESS)
549-
builder.expect(':app:processReleaseResources', SUCCESS)
550+
builder.expect(':app:processDebugResources', FROM_CACHE)
551+
builder.expect(':app:processReleaseResources', FROM_CACHE)
550552
// New tasks in 7.0.0-beta04
551553
builder.expect(':library:javaPreCompileDebug', FROM_CACHE)
552554
builder.expect(':library:javaPreCompileRelease', FROM_CACHE)
555+
builder.expect(':library:mapReleaseSourceSetPaths', SUCCESS)
553556
builder.expect(':app:javaPreCompileDebug', FROM_CACHE)
554557
builder.expect(':app:javaPreCompileRelease', FROM_CACHE)
555558
// New non-cacheable tasks in 7.0.0-beta05

src/test/groovy/org/gradle/android/SimpleAndroidApp.groovy

+2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class SimpleAndroidApp {
115115
android.useAndroidX=true
116116
org.gradle.jvmargs=-Xmx2048m
117117
kapt.use.worker.api=${kaptWorkersEnabled}
118+
android.experimental.enableSourceSetPathsMap=true
119+
android.experimental.cacheCompileLibResources=true
118120
""".stripIndent()
119121

120122
configureAndroidSdkHome()

0 commit comments

Comments
 (0)