Skip to content

Commit a75becd

Browse files
committed
Remove isAndroid check from setupExtractIncludeProtosTask
Have Java and Android pass in the appropriate objects instead of looking them up. This was the last isAndroid check in ProtobufPlugin excepting apply() itself.
1 parent 1be9931 commit a75becd

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy

+22-27
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,12 @@ class ProtobufPlugin implements Plugin<Project> {
234234
Configuration compileProtoPath, Collection<Closure> postConfigure) {
235235
Provider<ProtobufExtract> extractProtosTask =
236236
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'.
237241
Provider<ProtobufExtract> extractIncludeProtosTask = setupExtractIncludeProtosTask(
238-
sourceSet.name, false, compileProtoPath)
242+
sourceSet.name, compileProtoPath, sourceSet.compileClasspath)
239243
Provider<GenerateProtoTask> generateProtoTask = addGenerateProtoTask(
240244
sourceSet.name, protoSrcDirSet.sourceDirectories, project.files(extractProtosTask),
241245
extractIncludeProtosTask) {
@@ -290,15 +294,23 @@ class ProtobufPlugin implements Plugin<Project> {
290294
it.attribute(artifactType, "jar")
291295
}
292296
}.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 {
296306
attributes {
297307
it.attribute(artifactType, "jar")
298308
}
299-
}.files : null
309+
}.files
310+
}
311+
}
300312
Provider<ProtobufExtract> extractIncludeProtosTask =
301-
setupExtractIncludeProtosTask(variant.name, true, classPathConfig, testClassPathConfig)
313+
setupExtractIncludeProtosTask(variant.name, classPathConfig, testClassPathConfig)
302314

303315
// GenerateProto task, one per variant (compilation unit).
304316
FileCollection sourceDirs = project.files(project.providers.provider {
@@ -417,40 +429,23 @@ class ProtobufPlugin implements Plugin<Project> {
417429
*
418430
* <p>This task is per-sourceSet for both Java and per variant for Android.
419431
*/
420-
@TypeChecked(TypeCheckingMode.SKIP) // Don't depend on AGP
421432
private Provider<ProtobufExtract> setupExtractIncludeProtosTask(
422433
String sourceSetOrVariantName,
423-
boolean isAndroid,
424434
FileCollection compileClasspathConfiguration,
425-
FileCollection testedCompileClasspathConfiguration = null) {
435+
FileCollection testedCompileClasspathConfiguration) {
426436
String extractIncludeProtosTaskName = 'extractInclude' +
427437
Utils.getSourceSetSubstringForTaskNames(sourceSetOrVariantName) + 'Proto'
428438
return project.tasks.register(extractIncludeProtosTaskName, ProtobufExtract) {
429439
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)
431441
it.inputFiles.from(compileClasspathConfiguration)
432442

433443
// TL; DR: Make protos in 'test' sourceSet able to import protos from the 'main'
434444
// sourceSet. Sub-configurations, e.g., 'testCompile' that extends 'compile', don't
435445
// depend on the their super configurations. As a result, 'testCompile' doesn't depend on
436446
// '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)
454449
}
455450
}
456451

0 commit comments

Comments
 (0)