Skip to content

Commit 17ff9de

Browse files
authored
Merge pull request #98 from gradle/no/compile-library-resources
Fix CompileLibraryResources cache miss.
2 parents cc221cc + b2c3286 commit 17ff9de

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.android.build.gradle.tasks.ProcessAndroidResources
77
import com.android.builder.model.Version
88
import com.google.common.collect.ImmutableList
99
import groovy.transform.CompileStatic
10+
import org.gradle.android.workarounds.CompileLibraryResourcesWorkaround
1011
import org.gradle.android.workarounds.CompilerArgsProcessor
1112
import org.gradle.android.workarounds.MergeJavaResourcesWorkaround
1213
import org.gradle.android.workarounds.MergeNativeLibsWorkaround
@@ -42,7 +43,8 @@ class AndroidCacheFixPlugin implements Plugin<Project> {
4243
WORKAROUNDS.addAll(
4344
new MergeJavaResourcesWorkaround(),
4445
new MergeNativeLibsWorkaround(),
45-
new RoomSchemaLocationWorkaround()
46+
new RoomSchemaLocationWorkaround(),
47+
new CompileLibraryResourcesWorkaround()
4648
)
4749
}
4850
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.gradle.android.workarounds
2+
3+
import org.gradle.android.AndroidIssue
4+
import org.gradle.api.Project
5+
import org.gradle.api.Task
6+
import org.gradle.api.file.DirectoryProperty
7+
import org.gradle.api.tasks.PathSensitivity
8+
9+
import java.lang.reflect.Field
10+
11+
12+
/**
13+
* Fixes the cacheability issue with CompileLibraryResourcesWorkaround where the mergedLibraryResourcesDir field is
14+
* treated as an input with absolute path sensitivity.
15+
*/
16+
@AndroidIssue(introducedIn = "4.0.0", fixedIn = [], link = "https://issuetracker.google.com/issues/155218379")
17+
class CompileLibraryResourcesWorkaround implements Workaround {
18+
// This task is new in AGP 4.0.0 so use Class.forName to allow for backward compatibility with older AGP versions.
19+
static Class<?> getAndroidTaskClass() {
20+
return Class.forName('com.android.build.gradle.tasks.CompileLibraryResourcesTask')
21+
}
22+
23+
String propertyName = "mergedLibraryResourcesDir"
24+
25+
public void setPropertyValue(Task task, DirectoryProperty directoryProperty) {
26+
Field field = task.class.getDeclaredFields().find { it.name == "__${propertyName}__" }
27+
field.setAccessible(true)
28+
field.set(task, directoryProperty)
29+
}
30+
31+
@Override
32+
void apply(WorkaroundContext context) {
33+
Project project = context.project
34+
project.tasks.withType(androidTaskClass).configureEach { Task task ->
35+
DirectoryProperty originalPropertyValue
36+
// Create a synthetic input with the original property value and RELATIVE path sensitivity
37+
project.gradle.taskGraph.beforeTask {
38+
if (it == task) {
39+
originalPropertyValue = task.getProperty(propertyName)
40+
def dummyProperty = project.objects.directoryProperty()
41+
// Non-existent file to give the DirectoryProperty a value.
42+
dummyProperty.set(project.file('/doesnt-exist'))
43+
setPropertyValue(task, dummyProperty)
44+
task.inputs.dir(originalPropertyValue)
45+
.withPathSensitivity(PathSensitivity.RELATIVE)
46+
.withPropertyName("${propertyName}.workaround")
47+
.optional()
48+
}
49+
}
50+
// Set the task property back to its original value
51+
task.doFirst {
52+
setPropertyValue(task, originalPropertyValue)
53+
}
54+
}
55+
}
56+
57+
@Override
58+
boolean canBeApplied(Project project) {
59+
return true
60+
}
61+
}
62+

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ class CrossVersionOutcomeAndRelocationTest extends AbstractTest {
135135
def isAndroid35x = androidVersion >= android("3.5.0") && androidVersion < android("3.6.0")
136136
def isAndroid35xTo36x = androidVersion >= android("3.5.0") && androidVersion <= android("3.6.3")
137137
def isAndroid35xTo40x = androidVersion >= android("3.5.0") && androidVersion <= android("4.1.0-alpha01")
138-
def isAndroid36x = androidVersion >= android("3.6.0") && androidVersion < android("4.0.0-alpha01")
139138
def isAndroid36xOrHigher = androidVersion >= android("3.6.0")
140139
def isAndroid40xOrHigher = androidVersion >= android("4.0.0-beta01")
141140
def isAndroid40x = androidVersion >= android("4.0.0") && androidVersion < android("4.1.0-alpha01")
@@ -162,11 +161,6 @@ class CrossVersionOutcomeAndRelocationTest extends AbstractTest {
162161
android35xTo36xExpectations(builder)
163162
}
164163

165-
// Applies to 3.6.x only
166-
if (isAndroid36x) {
167-
android36xOnlyExpectations(builder)
168-
}
169-
170164
// Applies to anything 3.6.0 or higher
171165
if (isAndroid36xOrHigher) {
172166
android36xOrHigherExpectations(builder)
@@ -414,9 +408,6 @@ class CrossVersionOutcomeAndRelocationTest extends AbstractTest {
414408
builder.expect(':library:parseReleaseLocalResources', FROM_CACHE)
415409
builder.expect(':library:syncDebugLibJars', FROM_CACHE)
416410
builder.expect(':library:syncReleaseLibJars', FROM_CACHE)
417-
}
418-
419-
static void android36xOnlyExpectations(ExpectedOutcomeBuilder builder) {
420411
builder.expect(':library:compileDebugLibraryResources', FROM_CACHE)
421412
builder.expect(':library:compileReleaseLibraryResources', FROM_CACHE)
422413
}
@@ -430,9 +421,7 @@ class CrossVersionOutcomeAndRelocationTest extends AbstractTest {
430421
builder.expect(':app:mergeReleaseResources', SUCCESS)
431422
builder.expect(':app:processDebugResources', SUCCESS)
432423
builder.expect(':app:processReleaseResources', SUCCESS)
433-
builder.expect(':library:compileDebugLibraryResources', SUCCESS)
434424
builder.expect(':library:compileDebugShaders', NO_SOURCE)
435-
builder.expect(':library:compileReleaseLibraryResources', SUCCESS)
436425
builder.expect(':library:compileReleaseShaders', NO_SOURCE)
437426
builder.expect(':library:dataBindingMergeGenClassesDebug', FROM_CACHE)
438427
builder.expect(':library:dataBindingMergeGenClassesRelease', FROM_CACHE)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class WorkaroundTest extends Specification {
1212
workarounds.collect { it.class.simpleName.replaceAll(/Workaround/, "") }.sort() == expectedWorkarounds.sort()
1313
where:
1414
androidVersion | expectedWorkarounds
15-
"4.1.0-alpha10" | ['RoomSchemaLocation']
16-
"4.0.0-beta04" | ['MergeJavaResources', 'MergeNativeLibs', 'RoomSchemaLocation']
15+
"4.1.0-alpha10" | ['RoomSchemaLocation', 'CompileLibraryResources']
16+
"4.0.0" | ['MergeJavaResources', 'MergeNativeLibs', 'RoomSchemaLocation', 'CompileLibraryResources']
1717
"3.6.2" | ['MergeJavaResources', 'MergeNativeLibs', 'RoomSchemaLocation']
1818
"3.6.1" | ['MergeJavaResources', 'MergeNativeLibs', 'RoomSchemaLocation']
1919
"3.6.0" | ['MergeJavaResources', 'MergeNativeLibs', 'RoomSchemaLocation']

0 commit comments

Comments
 (0)