Clean dependencies of published libraries.
Xavier Ducrohet [Thu, 31 Jan 2013 02:01:08 +0000 (18:01 -0800)]
Change-Id: Icb3a75c7e79cbb58c0e529018f318323c649b475

gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy
gradle/src/main/groovy/com/android/build/gradle/LibraryPlugin.groovy
tests/repo/app/src/main/java/com/example/android/multiproject/MainActivity.java
tests/repo/baseLibrary/build.gradle
tests/repo/util/build.gradle
tests/repo/util/src/main/java/com/example/android/multiproject/person/People.java

index ebbab8f..7f27ff5 100644 (file)
@@ -896,6 +896,7 @@ public abstract class BasePlugin {
             Map<ModuleVersionIdentifier, List<ResolvedArtifact>> artifacts,
             Multimap<AndroidDependency, ConfigurationDependencies> reverseMap) {
 
+        // TODO support package configuration
         def compileClasspath = configDependencies.configuration
 
         // TODO - shouldn't need to do this - fix this in Gradle
index d1be77b..7e0dac6 100644 (file)
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 package com.android.build.gradle
-
 import com.android.SdkConstants
 import com.android.build.gradle.internal.BuildTypeData
 import com.android.build.gradle.internal.DefaultBuildVariant
@@ -38,7 +37,6 @@ import org.gradle.api.tasks.bundling.Zip
 import org.gradle.internal.reflect.Instantiator
 
 import javax.inject.Inject
-
 /**
  * Gradle plugin class for 'library' projects.
  */
@@ -73,22 +71,31 @@ public class LibraryPlugin extends BasePlugin implements Plugin<Project> {
         project.tasks.assemble.dependsOn debugBuildTypeData.assembleTask
         project.tasks.assemble.dependsOn releaseBuildTypeData.assembleTask
 
-        createConfigurations()
+        createConfigurations(releaseSourceSet)
     }
 
-    void createConfigurations() {
-        def debugConfig = project.configurations.add(BuilderConstants.DEBUG)
-        def releaseConfig = project.configurations.add(BuilderConstants.RELEASE)
-        debugConfig.extendsFrom(project.configurations["package"])
-        releaseConfig.extendsFrom(project.configurations["package"])
-        project.configurations["default"].extendsFrom(releaseConfig)
+    void createConfigurations(AndroidSourceSet releaseSourceSet) {
+        // The library artifact is published for the "default" configuration so we make
+        // sure "default" extends from the actual configuration used for building.
+        project.configurations["default"].extendsFrom(
+                project.configurations[mainSourceSet.getPackageConfigurationName()])
+        project.configurations["default"].extendsFrom(
+                project.configurations[releaseSourceSet.getPackageConfigurationName()])
 
-        // Adjust the pom scope mappings
-        // TODO - this should be part of JavaBase plugin. Fix this in Gradle
         project.plugins.withType(MavenPlugin) {
-            project.conf2ScopeMappings.addMapping(300, project.configurations.compile, "runtime")
-            project.conf2ScopeMappings.addMapping(300, project.configurations["package"], "runtime")
-            project.conf2ScopeMappings.addMapping(300, releaseConfig, "runtime")
+            project.conf2ScopeMappings.addMapping(300,
+                    project.configurations[mainSourceSet.getCompileConfigurationName()],
+                    "compile")
+            project.conf2ScopeMappings.addMapping(300,
+                    project.configurations[releaseSourceSet.getCompileConfigurationName()],
+                    "compile")
+            // TODO -- figure out the package configuration
+//            project.conf2ScopeMappings.addMapping(300,
+//                    project.configurations[mainSourceSet.getPackageConfigurationName()],
+//                    "runtime")
+//            project.conf2ScopeMappings.addMapping(300,
+//                    project.configurations[releaseSourceSet.getPackageConfigurationName()],
+//                    "runtime")
         }
     }
 
@@ -100,8 +107,8 @@ public class LibraryPlugin extends BasePlugin implements Plugin<Project> {
         dependencies.add(releaseBuildTypeData)
         resolveDependencies(dependencies)
 
-        ProductionAppVariant testedVariant = createLibraryTasks(debugBuildTypeData)
-        ProductionAppVariant nonTestedVariant = createLibraryTasks(releaseBuildTypeData)
+        ProductionAppVariant testedVariant = createLibraryTasks(debugBuildTypeData, false)
+        ProductionAppVariant nonTestedVariant = createLibraryTasks(releaseBuildTypeData, true)
         TestAppVariant testVariant = createTestTasks(testedVariant)
 
         // add the not-tested build variant.
@@ -117,9 +124,11 @@ public class LibraryPlugin extends BasePlugin implements Plugin<Project> {
         extension.buildVariants.add(
                 instantiator.newInstance(DefaultBuildVariant.class,
                         testedVariant, testBuildVariant))
+
     }
 
-    private ProductionAppVariant createLibraryTasks(BuildTypeData buildTypeData) {
+    private ProductionAppVariant createLibraryTasks(BuildTypeData buildTypeData,
+                                                    boolean publishArtifact) {
         ProductFlavorData defaultConfigData = getDefaultConfigData();
 
         List<ConfigurationDependencies> configDependencies = []
@@ -221,7 +230,10 @@ public class LibraryPlugin extends BasePlugin implements Plugin<Project> {
         variant.packageLibTask = bundle
         variant.outputFile = bundle.archivePath
 
-        project.artifacts.add(buildTypeData.buildType.name, bundle)
+        if (publishArtifact) {
+            // add the artifact that will be published
+            project.artifacts.add("default", bundle)
+        }
 
         buildTypeData.assembleTask.dependsOn bundle
         variant.assembleTask = bundle
index 11d7c32..60dd603 100644 (file)
@@ -5,6 +5,10 @@ import android.view.View;
 import android.content.Intent;
 import android.os.Bundle;
 
+import java.util.List;
+import com.example.android.multiproject.person.Person;
+import com.google.common.collect.Lists;
+
 import com.example.android.multiproject.library.ShowPeopleActivity;
 
 public class MainActivity extends Activity {
@@ -12,6 +16,11 @@ public class MainActivity extends Activity {
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
+
+        // some random code to test dependencies on util and guava
+       Person p = new Person("foo");
+       List<Person> persons = Lists.newArrayList();
+       persons.add(p);
     }
 
     public void sendMessage(View view) {
index fc1a70a..89445f9 100644 (file)
@@ -16,6 +16,7 @@ repositories {
 
 dependencies {
     compile 'com.example.android.multiproject:util:1.0'
+    releaseCompile 'com.google.guava:guava:11.0.2'
 }
 
 android {
index 2baca33..150b997 100644 (file)
@@ -5,10 +5,6 @@ repositories {
     mavenCentral()
 }
 
-dependencies {
-    compile 'com.google.guava:guava:11.0.2'
-}
-
 group = 'com.example.android.multiproject'
 archivesBaseName = 'util'
 version = '1.0'
index 8b99248..2dbc9b5 100644 (file)
@@ -1,10 +1,13 @@
 package com.example.android.multiproject.person;
 
+import java.util.ArrayList;
 import java.util.Iterator;
-import com.google.common.collect.Lists;
 
 public class People implements Iterable<Person> {
     public Iterator<Person> iterator() {
-        return Lists.newArrayList(new Person("Fred"), new Person("Barney")).iterator();
+        ArrayList<Person> list = new ArrayList<Person>();
+        list.add(new Person("Fred"));
+        list.add(new Person("Barney"));
+        return list.iterator();
     }
 }