@@ -234,8 +234,12 @@ class ProtobufPlugin implements Plugin<Project> {
234
234
Configuration compileProtoPath , Collection<Closure > postConfigure ) {
235
235
Provider<ProtobufExtract > extractProtosTask =
236
236
setupExtractProtosTask(sourceSet. name, protobufConfig)
237
+ // In Java projects, the compileClasspath of the 'test' sourceSet includes all the
238
+ // 'resources' of the output of 'main', in which the source protos are placed. This is
239
+ // nicer than the ad-hoc solution that Android has, because it works for any extended
240
+ // configuration, not just 'testCompile'.
237
241
Provider<ProtobufExtract > extractIncludeProtosTask = setupExtractIncludeProtosTask(
238
- sourceSet. name, false , compileProtoPath )
242
+ sourceSet. name, compileProtoPath, sourceSet . compileClasspath )
239
243
Provider<GenerateProtoTask > generateProtoTask = addGenerateProtoTask(
240
244
sourceSet. name, protoSrcDirSet. sourceDirectories, project. files(extractProtosTask),
241
245
extractIncludeProtosTask) {
@@ -290,15 +294,23 @@ class ProtobufPlugin implements Plugin<Project> {
290
294
it. attribute(artifactType, " jar" )
291
295
}
292
296
}. files
293
- FileCollection testClassPathConfig =
294
- variant. hasProperty(" testedVariant" ) ?
295
- variant. testedVariant. compileConfiguration. incoming. artifactView {
297
+ FileCollection testClassPathConfig = project. objects. fileCollection()
298
+ if (Utils . isTest(variant. name)) {
299
+ // TODO(zhangkun83): Android sourceSet doesn't have compileClasspath. If it did, we
300
+ // haven't figured out a way to put source protos in 'resources'. For now we use an
301
+ // ad-hoc solution that manually includes the source protos of 'main' and its
302
+ // dependencies.
303
+ testClassPathConfig = project. android. sourceSets[' main' ]. proto. sourceDirectories
304
+ if (variant. hasProperty(" testedVariant" )) {
305
+ testClassPathConfig + = variant. testedVariant. compileConfiguration. incoming. artifactView {
296
306
attributes {
297
307
it. attribute(artifactType, " jar" )
298
308
}
299
- }. files : null
309
+ }. files
310
+ }
311
+ }
300
312
Provider<ProtobufExtract > extractIncludeProtosTask =
301
- setupExtractIncludeProtosTask(variant. name, true , classPathConfig, testClassPathConfig)
313
+ setupExtractIncludeProtosTask(variant. name, classPathConfig, testClassPathConfig)
302
314
303
315
// GenerateProto task, one per variant (compilation unit).
304
316
FileCollection sourceDirs = project. files(project. providers. provider {
@@ -417,40 +429,23 @@ class ProtobufPlugin implements Plugin<Project> {
417
429
*
418
430
* <p >This task is per-sourceSet for both Java and per variant for Android.
419
431
*/
420
- @TypeChecked (TypeCheckingMode .SKIP ) // Don't depend on AGP
421
432
private Provider<ProtobufExtract > setupExtractIncludeProtosTask (
422
433
String sourceSetOrVariantName ,
423
- boolean isAndroid ,
424
434
FileCollection compileClasspathConfiguration ,
425
- FileCollection testedCompileClasspathConfiguration = null ) {
435
+ FileCollection testedCompileClasspathConfiguration ) {
426
436
String extractIncludeProtosTaskName = ' extractInclude' +
427
437
Utils . getSourceSetSubstringForTaskNames(sourceSetOrVariantName) + ' Proto'
428
438
return project. tasks. register(extractIncludeProtosTaskName, ProtobufExtract ) {
429
439
it. description = " Extracts proto files from compile dependencies for includes"
430
- it. destDir = getExtractedIncludeProtosDir(sourceSetOrVariantName) as File
440
+ it. destDir. set( getExtractedIncludeProtosDir(sourceSetOrVariantName) as File )
431
441
it. inputFiles. from(compileClasspathConfiguration)
432
442
433
443
// TL; DR: Make protos in 'test' sourceSet able to import protos from the 'main'
434
444
// sourceSet. Sub-configurations, e.g., 'testCompile' that extends 'compile', don't
435
445
// depend on the their super configurations. As a result, 'testCompile' doesn't depend on
436
446
// 'compile' and it cannot get the proto files from 'main' sourceSet through the
437
- // configuration. However,
438
- if (isAndroid) {
439
- // TODO(zhangkun83): Android sourceSet doesn't have compileClasspath. If it did, we
440
- // haven't figured out a way to put source protos in 'resources'. For now we use an
441
- // ad-hoc solution that manually includes the source protos of 'main' and its
442
- // dependencies.
443
- if (Utils . isTest(sourceSetOrVariantName)) {
444
- it. inputFiles. from project. android. sourceSets[' main' ]. proto. sourceDirectories
445
- it. inputFiles. from testedCompileClasspathConfiguration
446
- }
447
- } else {
448
- // In Java projects, the compileClasspath of the 'test' sourceSet includes all the
449
- // 'resources' of the output of 'main', in which the source protos are placed. This is
450
- // nicer than the ad-hoc solution that Android has, because it works for any extended
451
- // configuration, not just 'testCompile'.
452
- it. inputFiles. from project. sourceSets[sourceSetOrVariantName]. compileClasspath
453
- }
447
+ // configuration.
448
+ it. inputFiles. from(testedCompileClasspathConfiguration)
454
449
}
455
450
}
456
451
0 commit comments