Unify creation of source jars.

Test: ./gradlew assembleDebug
Change-Id: Ia8c043c6e4fb90b8eedfc78215696b581a1331a8
diff --git a/annotations/build.gradle b/annotations/build.gradle
index af4174d..dd4a5bc 100644
--- a/annotations/build.gradle
+++ b/annotations/build.gradle
@@ -27,12 +27,6 @@
     }
 }
 
-// custom tasks for creating source/javadoc jars
-task sourcesJar(type: Jar, dependsOn:classes) {
-    classifier = 'sources'
-    from sourceSets.main.allSource
-}
-
 task javadocJar(type: Jar, dependsOn:javadoc) {
     classifier         'javadoc'
     from               javadoc.destinationDir
@@ -46,7 +40,6 @@
 // add javadoc/source/annotations jar tasks as artifacts
 artifacts {
     archives jar
-    archives sourcesJar
     archives javadocJar
     archives annotationsZip
 }
diff --git a/app-toolkit/init.gradle b/app-toolkit/init.gradle
index cd56153..d317d33 100644
--- a/app-toolkit/init.gradle
+++ b/app-toolkit/init.gradle
@@ -147,35 +147,6 @@
         }
     }
 
-    project.plugins.whenPluginAdded { plugin ->
-        def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
-                .equals(plugin.class.name)
-        def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
-        if (isAndroidLibrary) {
-            // it is an android lib, enable sources.
-            def sourcesTask = project.tasks.create(name: "sourcesJar", type : Jar) {
-                classifier = 'sources'
-                from android.sourceSets.main.getJava().getSrcDirs()
-            }
-            project.artifacts {
-                archives sourcesTask
-            }
-            project.android.defaultConfig {
-                // Update the version meta-data in each Manifest.
-                addManifestPlaceholders(["version" : project.version])
-            }
-        } else if(isJavaLibrary && project.name == "common") {
-            // it is a shared lib, enable sources.
-            def sourcesTask = project.tasks.create(name: "sourcesJar", type : Jar) {
-                classifier = 'sources'
-                from sourceSets.main.allSource
-            }
-            project.artifacts {
-                archives sourcesTask
-            }
-        }
-    }
-
     project.tasks.whenTaskAdded { task ->
         if (task.name.startsWith("assembleAndroidTest")) {
             buildServerAnchorTask.dependsOn task
diff --git a/buildSrc/src/main/groovy/android/support/FlatfootAndroidLibraryPlugin.groovy b/buildSrc/src/main/groovy/android/support/FlatfootAndroidLibraryPlugin.groovy
index c6abac2..47a72d7 100644
--- a/buildSrc/src/main/groovy/android/support/FlatfootAndroidLibraryPlugin.groovy
+++ b/buildSrc/src/main/groovy/android/support/FlatfootAndroidLibraryPlugin.groovy
@@ -16,6 +16,7 @@
 
 package android.support
 
+import com.android.build.gradle.LibraryExtension
 import com.google.common.collect.ImmutableMap
 import org.gradle.api.Plugin
 import org.gradle.api.Project
@@ -33,5 +34,8 @@
         VersionFileWriterTask.setUpAndroidLibrary(project);
 
         project.apply(ImmutableMap.of("plugin", "com.android.library"));
+
+        LibraryExtension library = project.extensions.findByType(LibraryExtension.class);
+        SourceJarTaskHelper.setUpAndroidProject(project, library);
     }
 }
\ No newline at end of file
diff --git a/buildSrc/src/main/groovy/android/support/SupportAndroidLibraryPlugin.groovy b/buildSrc/src/main/groovy/android/support/SupportAndroidLibraryPlugin.groovy
index 2170a50..86469ee 100644
--- a/buildSrc/src/main/groovy/android/support/SupportAndroidLibraryPlugin.groovy
+++ b/buildSrc/src/main/groovy/android/support/SupportAndroidLibraryPlugin.groovy
@@ -135,21 +135,7 @@
             project.uploadArchives.dependsOn "lintRelease"
         }
 
-        // Create sources jar for release builds
-        library.getLibraryVariants().all(new Action<LibraryVariant>() {
-            @Override
-            public void execute(LibraryVariant libraryVariant) {
-                if (!libraryVariant.getBuildType().getName().equals(BuilderConstants.RELEASE)) {
-                    return; // Skip non-release builds.
-                }
-
-                Jar sourceJar = project.getTasks().create("sourceJarRelease", Jar.class);
-                sourceJar.preserveFileTimestamps = false;
-                sourceJar.setClassifier("sources");
-                sourceJar.from(library.getSourceSets().findByName("main").getJava().getSrcDirs());
-                project.getArtifacts().add("archives", sourceJar);
-            }
-        });
+        SourceJarTaskHelper.setUpAndroidProject(project, library);
 
         final ErrorProneToolChain toolChain = ErrorProneToolChain.create(project);
         library.getBuildTypes().create("errorProne")
diff --git a/buildSrc/src/main/groovy/android/support/SupportJavaLibraryPlugin.groovy b/buildSrc/src/main/groovy/android/support/SupportJavaLibraryPlugin.groovy
index ae0d55c..3113e6c 100644
--- a/buildSrc/src/main/groovy/android/support/SupportJavaLibraryPlugin.groovy
+++ b/buildSrc/src/main/groovy/android/support/SupportJavaLibraryPlugin.groovy
@@ -20,6 +20,7 @@
 import org.gradle.api.JavaVersion
 import org.gradle.api.Plugin
 import org.gradle.api.Project
+import org.gradle.api.plugins.JavaPluginConvention
 
 /**
  * Support java library specific plugin that sets common configurations needed for
@@ -41,5 +42,7 @@
                 targetCompatibility = version
             }
         }
+
+        SourceJarTaskHelper.setUpJavaProject(project);
     }
 }
\ No newline at end of file
diff --git a/buildSrc/src/main/java/android/support/SourceJarTaskHelper.java b/buildSrc/src/main/java/android/support/SourceJarTaskHelper.java
new file mode 100644
index 0000000..9fbd1db
--- /dev/null
+++ b/buildSrc/src/main/java/android/support/SourceJarTaskHelper.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.support;
+
+import com.android.build.gradle.LibraryExtension;
+import com.android.builder.core.BuilderConstants;
+
+import org.gradle.api.Project;
+import org.gradle.api.plugins.JavaPluginConvention;
+import org.gradle.api.tasks.bundling.Jar;
+
+/**
+ * Helper class to handle creation of source jars.
+ */
+public class SourceJarTaskHelper {
+    /**
+     * Sets up a source jar task for an Android library project.
+     */
+    public static void setUpAndroidProject(Project project, LibraryExtension extension) {
+        // Create sources jar for release builds
+        extension.getLibraryVariants().all(libraryVariant -> {
+            if (!libraryVariant.getBuildType().getName().equals(BuilderConstants.RELEASE)) {
+                return; // Skip non-release builds.
+            }
+
+            Jar sourceJar = project.getTasks().create("sourceJarRelease", Jar.class);
+            sourceJar.setPreserveFileTimestamps(false);
+            sourceJar.setClassifier("sources");
+            sourceJar.from(extension.getSourceSets().findByName("main").getJava().getSrcDirs());
+            project.getArtifacts().add("archives", sourceJar);
+        });
+    }
+
+    /**
+     * Sets up a source jar task for a Java library project.
+     */
+    public static void setUpJavaProject(Project project) {
+        Jar sourceJar = project.getTasks().create("sourceJar", Jar.class);
+        sourceJar.setPreserveFileTimestamps(false);
+        sourceJar.setClassifier("sources");
+        JavaPluginConvention convention =
+                project.getConvention().getPlugin(JavaPluginConvention.class);
+        sourceJar.from(convention.getSourceSets().findByName("main").getAllSource().getSrcDirs());
+        project.getArtifacts().add("archives", sourceJar);
+    }
+}