Add r8-sources.jar to output

Bug: b/270105162
Change-Id: Id6b9ff75179259a1a504112b7f9a7cebe518e357
diff --git a/d8_r8/keepanno/build.gradle.kts b/d8_r8/keepanno/build.gradle.kts
index 97cd877..56d1ea0 100644
--- a/d8_r8/keepanno/build.gradle.kts
+++ b/d8_r8/keepanno/build.gradle.kts
@@ -13,6 +13,7 @@
   }
   sourceCompatibility = JvmCompatibility.sourceCompatibility
   targetCompatibility = JvmCompatibility.targetCompatibility
+  withSourcesJar()
 }
 
 dependencies {
diff --git a/d8_r8/main/build.gradle.kts b/d8_r8/main/build.gradle.kts
index b87fa92..b1a2db5 100644
--- a/d8_r8/main/build.gradle.kts
+++ b/d8_r8/main/build.gradle.kts
@@ -19,6 +19,7 @@
   }
   sourceCompatibility = JvmCompatibility.sourceCompatibility
   targetCompatibility = JvmCompatibility.targetCompatibility
+  withSourcesJar()
 }
 
 dependencies {
diff --git a/d8_r8/resourceshrinker/build.gradle.kts b/d8_r8/resourceshrinker/build.gradle.kts
index 62e44f6..750f076 100644
--- a/d8_r8/resourceshrinker/build.gradle.kts
+++ b/d8_r8/resourceshrinker/build.gradle.kts
@@ -16,6 +16,7 @@
   }
   sourceCompatibility = JvmCompatibility.sourceCompatibility
   targetCompatibility = JvmCompatibility.targetCompatibility
+  withSourcesJar()
 }
 
 fun jarDependencies() : FileCollection {
diff --git a/d8_r8/test/build.gradle.kts b/d8_r8/test/build.gradle.kts
index 884a536..fa1729c 100644
--- a/d8_r8/test/build.gradle.kts
+++ b/d8_r8/test/build.gradle.kts
@@ -21,9 +21,12 @@
 dependencies { }
 
 val keepAnnoCompileTask = projectTask("keepanno", "compileJava")
+val keepAnnoSourcesTask = projectTask("keepanno", "sourcesJar")
 val mainDepsJarTask = projectTask("main", "depsJar")
 val swissArmyKnifeTask = projectTask("main", "swissArmyKnife")
 val r8WithRelocatedDepsTask = projectTask("main", "r8WithRelocatedDeps")
+val mainSourcesTask = projectTask("main", "sourcesJar")
+val resourceShrinkerSourcesTask = projectTask("resourceshrinker", "sourcesJar")
 val java8TestJarTask = projectTask("tests_java_8", "testJar")
 val java8TestsDepsJarTask = projectTask("tests_java_8", "depsJar")
 val bootstrapTestsDepsJarTask = projectTask("tests_bootstrap", "depsJar")
@@ -230,7 +233,6 @@
     dependsOn(allTestsJarRelocated)
     dependsOn(r8WithRelocatedDepsTask)
     dependsOn(allTestWithApplyMappingProguardConfiguration)
-    // dependsOn(thirdPartyRuntimeDependenciesTask)
     val r8 = r8WithRelocatedDepsTask.outputs.files.singleFile
     val allTests = allTestsJarRelocated.get().outputs.files.singleFile
     val pgConf = allTestWithApplyMappingProguardConfiguration.get().outputs.files.singleFile
@@ -305,6 +307,19 @@
     systemProperty("com.android.tools.r8.artprofilerewritingcompletenesscheck", "true")
   }
 
+  val sourcesJar by registering(Jar::class) {
+    dependsOn(mainSourcesTask)
+    dependsOn(resourceShrinkerSourcesTask)
+    dependsOn(keepAnnoSourcesTask)
+    from(mainSourcesTask.outputs.files.map(::zipTree))
+    from(resourceShrinkerSourcesTask.outputs.files.map(::zipTree))
+    from(keepAnnoSourcesTask.outputs.files.map(::zipTree))
+    archiveClassifier.set("sources")
+    // Because we use the sources classifier this will be r8-sources.jar.
+    archiveBaseName.set("r8")
+    destinationDirectory.set(getRoot().resolveAll("build", "libs"))
+  }
+
   test {
     if (project.hasProperty("r8lib")) {
       dependsOn(r8LibTest)
diff --git a/d8_r8/test/settings.gradle.kts b/d8_r8/test/settings.gradle.kts
index 38419b5..375809a 100644
--- a/d8_r8/test/settings.gradle.kts
+++ b/d8_r8/test/settings.gradle.kts
@@ -30,5 +30,6 @@
 includeBuild(root.resolve("shared"))
 includeBuild(root.resolve("keepanno"))
 includeBuild(root.resolve("main"))
+includeBuild(root.resolve("resourceshrinker"))
 includeBuild(root.resolve("test_modules").resolve("tests_java_8"))
 includeBuild(root.resolve("test_modules").resolve("tests_bootstrap"))