@@ -75,9 +75,9 @@ class RoomSchemaLocationWorkaround implements Workaround {
75
75
roomSchemaMergeLocations = roomExtension. roomSchemaMergeLocations
76
76
}
77
77
78
- // Make sure that the annotation processor argument has not been explicitly configured in the Android
79
- // configuration (i.e. we only want this configured through the room extension
80
78
def configureVariant = { variant ->
79
+ // Make sure that the annotation processor argument has not been explicitly configured in the Android
80
+ // configuration (i.e. we only want this configured through the room extension
81
81
Map<String , String > arguments = variant. javaCompileOptions. annotationProcessorOptions. arguments
82
82
if (arguments. containsKey(ROOM_SCHEMA_LOCATION )) {
83
83
throw new IllegalStateException (""" ${ this.class.name} cannot be used with an explicit '${ ROOM_SCHEMA_LOCATION} ' annotation processor argument. Please change this to configure the schema location directory via the 'room' project extension:
@@ -86,30 +86,31 @@ class RoomSchemaLocationWorkaround implements Workaround {
86
86
}
87
87
""" )
88
88
}
89
- }
90
89
91
- applyToAllAndroidVariants(project, configureVariant)
90
+ // Configure the annotation processor argument provider on the Java compile task
91
+ javaCompileProvider. configure { JavaCompile task ->
92
+ def taskSpecificSchemaDir = project. objects. directoryProperty()
93
+ taskSpecificSchemaDir. set(getTaskSpecificSchemaDir(task))
92
94
93
- project. tasks. withType(JavaCompile ). configureEach { JavaCompile task ->
94
- def taskSpecificSchemaDir = project. objects. directoryProperty()
95
- taskSpecificSchemaDir. set(getTaskSpecificSchemaDir(task))
95
+ // Add a command line argument provider to the task-specific list of providers
96
+ task. options. compilerArgumentProviders. add(
97
+ new JavaCompilerRoomSchemaLocationArgumentProvider (taskSpecificSchemaDir)
98
+ )
96
99
97
- // Add a command line argument provider to the task-specific list of providers
98
- task. options. compilerArgumentProviders. add(
99
- new JavaCompilerRoomSchemaLocationArgumentProvider (taskSpecificSchemaDir)
100
- )
100
+ // Register the generated schemas to be merged back to the original specified schema directory
101
+ roomExtension. registerOutputDirectory(taskSpecificSchemaDir)
101
102
102
- // Register the generated schemas to be merged back to the original specified schema directory
103
- roomExtension. registerOutputDirectory(taskSpecificSchemaDir)
103
+ // Seed the task-specific generated schema dir with the existing schemas
104
+ task. doFirst {
105
+ copyExistingSchemasToTaskSpecificTmpDir(task, roomExtension. schemaLocationDir, taskSpecificSchemaDir)
106
+ }
104
107
105
- // Seed the task-specific generated schema dir with the existing schemas
106
- task. doFirst {
107
- copyExistingSchemasToTaskSpecificTmpDir(task, roomExtension. schemaLocationDir, taskSpecificSchemaDir)
108
+ task. finalizedBy { roomExtension. schemaLocationDir. isPresent() ? mergeTask : null }
108
109
}
109
-
110
- task. finalizedBy { roomExtension. schemaLocationDir. isPresent() ? mergeTask : null }
111
110
}
112
111
112
+ applyToAllAndroidVariants(project, configureVariant)
113
+
113
114
project. plugins. withId(" kotlin-kapt" ) {
114
115
// The kapt task has a list of annotation processor providers which _is_ the list of providers
115
116
// in the Android variant, so we can't just add a task-specific provider. To handle kapt tasks,
@@ -130,17 +131,17 @@ class RoomSchemaLocationWorkaround implements Workaround {
130
131
// pre-seeding the directory with existing schemas should be a capability of the room annotation processor
131
132
// somehow?
132
133
def configureKaptTask = { Task task ->
133
- task. doFirst {
134
+ task. doFirst onlyIfAnnotationProcessorConfiguredForKapt(task) { KaptRoomSchemaLocationArgumentProvider provider ->
134
135
// Populate the variant-specific schemas dir with the existing schemas
135
- copyExistingSchemasToTaskSpecificTmpDirForKapt(task, roomExtension. schemaLocationDir)
136
+ copyExistingSchemasToTaskSpecificTmpDirForKapt(task, roomExtension. schemaLocationDir, provider )
136
137
}
137
138
138
- task. doLast {
139
+ task. doLast onlyIfAnnotationProcessorConfiguredForKapt(task) { KaptRoomSchemaLocationArgumentProvider provider ->
139
140
// Copy the generated schemas into the registered output directory
140
- copyGeneratedSchemasToOutputDirForKapt(task)
141
+ copyGeneratedSchemasToOutputDirForKapt(task, provider )
141
142
}
142
143
143
- task. finalizedBy { roomExtension. schemaLocationDir. isPresent() ? mergeTask : null }
144
+ task. finalizedBy onlyIfAnnotationProcessorConfiguredForKapt(task) { roomExtension. schemaLocationDir. isPresent() ? mergeTask : null }
144
145
}
145
146
146
147
project. tasks. withType(kaptWithoutKotlincTaskClass). configureEach(configureKaptTask)
@@ -165,11 +166,20 @@ class RoomSchemaLocationWorkaround implements Workaround {
165
166
}
166
167
}
167
168
168
- private static def getKaptRoomSchemaLocationArgumentProvider (Task task ) {
169
+ private static KaptRoomSchemaLocationArgumentProvider getKaptRoomSchemaLocationArgumentProvider (Task task ) {
169
170
def annotationProcessorOptionProviders = getAccessibleField(task. class, " annotationProcessorOptionProviders" ). get(task)
170
171
return annotationProcessorOptionProviders. flatten(). find { it instanceof KaptRoomSchemaLocationArgumentProvider }
171
172
}
172
173
174
+ private static Closure onlyIfAnnotationProcessorConfiguredForKapt (Task task , Closure<?> action ) {
175
+ return {
176
+ def provider = getKaptRoomSchemaLocationArgumentProvider(task)
177
+ if (provider != null ) {
178
+ action. call(provider)
179
+ }
180
+ }
181
+ }
182
+
173
183
private static void applyToAllAndroidVariants (Project project , Closure<?> configureVariant ) {
174
184
project. plugins. withId(" com.android.application" ) {
175
185
def android = project. extensions. findByName(" android" )
@@ -206,21 +216,19 @@ class RoomSchemaLocationWorkaround implements Workaround {
206
216
}
207
217
}
208
218
209
- private static void copyExistingSchemasToTaskSpecificTmpDirForKapt (Task task , Provider<Directory > existingSchemaDir ) {
219
+ private static void copyExistingSchemasToTaskSpecificTmpDirForKapt (Task task , Provider<Directory > existingSchemaDir , KaptRoomSchemaLocationArgumentProvider provider ) {
210
220
// Derive the variant directory from the command line provider it is configured with
211
- def provider = getKaptRoomSchemaLocationArgumentProvider(task)
212
221
def temporaryVariantSpecificSchemaDir = provider. temporarySchemaLocationDir
213
222
214
223
// Populate the variant-specific temporary schema dir with the existing schemas
215
224
copyExistingSchemasToTaskSpecificTmpDir(task, existingSchemaDir, temporaryVariantSpecificSchemaDir)
216
225
}
217
226
218
- private static void copyGeneratedSchemasToOutputDirForKapt (Task task ) {
227
+ private static void copyGeneratedSchemasToOutputDirForKapt (Task task , KaptRoomSchemaLocationArgumentProvider provider ) {
219
228
// Copy the generated generated schemas from the task-specific tmp dir to the
220
229
// task-specific output dir. This dance prevents the kapt task from clearing out
221
230
// the existing schemas before the annotation processors run
222
231
// Derive the variant directory from the command line provider it is configured with
223
- def provider = getKaptRoomSchemaLocationArgumentProvider(task)
224
232
def variantSpecificSchemaDir = provider. schemaLocationDir
225
233
def temporaryVariantSpecificSchemaDir = provider. temporarySchemaLocationDir
226
234
0 commit comments