8ae1ba379f7efc7c4dce18002cf7290b403c474b
[android/platform/tools/build.git] / gradle / src / main / groovy / com / android / build / gradle / BaseExtension.groovy
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.build.gradle
17
18 import com.android.build.gradle.internal.CompileOptions
19 import com.android.build.gradle.internal.dsl.AaptOptionsImpl
20 import com.android.build.gradle.internal.dsl.AndroidSourceSetFactory
21 import com.android.build.gradle.internal.dsl.DexOptionsImpl
22 import com.android.build.gradle.internal.dsl.GroupableProductFlavor
23 import com.android.build.gradle.internal.dsl.ProductFlavorDsl
24 import com.android.build.gradle.internal.test.TestOptions
25 import com.android.builder.ProductFlavor
26 import org.gradle.api.Action
27 import org.gradle.api.NamedDomainObjectContainer
28 import org.gradle.api.artifacts.Configuration
29 import org.gradle.api.artifacts.ConfigurationContainer
30 import org.gradle.api.internal.DefaultDomainObjectSet
31 import org.gradle.api.internal.project.ProjectInternal
32 import org.gradle.internal.reflect.Instantiator
33 /**
34  * Base android extension for all android plugins.
35  */
36 public abstract class BaseExtension {
37
38     private String target
39
40     final ProductFlavor defaultConfig
41     final AaptOptionsImpl aaptOptions
42     final DexOptionsImpl dexOptions
43     final TestOptions testOptions
44     final CompileOptions compileOptions
45
46
47     private final BasePlugin plugin
48     private final DefaultDomainObjectSet<BuildVariant> buildVariants =
49         new DefaultDomainObjectSet<BuildVariant>(BuildVariant.class)
50     private final DefaultDomainObjectSet<BuildVariant> testBuildVariants =
51         new DefaultDomainObjectSet<BuildVariant>(BuildVariant.class)
52
53     /**
54      * The source sets container.
55      */
56     final NamedDomainObjectContainer<AndroidSourceSet> sourceSetsContainer
57
58     BaseExtension(BasePlugin plugin, ProjectInternal project, Instantiator instantiator) {
59         this.plugin = plugin
60
61         defaultConfig = instantiator.newInstance(ProductFlavorDsl.class, ProductFlavor.MAIN)
62
63         aaptOptions = instantiator.newInstance(AaptOptionsImpl.class)
64         dexOptions = instantiator.newInstance(DexOptionsImpl.class)
65         testOptions = instantiator.newInstance(TestOptions.class)
66         compileOptions = instantiator.newInstance(CompileOptions.class)
67
68         sourceSetsContainer = project.container(AndroidSourceSet,
69                 new AndroidSourceSetFactory(instantiator, project.fileResolver))
70
71         sourceSetsContainer.whenObjectAdded { AndroidSourceSet sourceSet ->
72             ConfigurationContainer configurations = project.getConfigurations()
73
74             Configuration compileConfiguration = configurations.findByName(
75                     sourceSet.getCompileConfigurationName())
76             if (compileConfiguration == null) {
77                 compileConfiguration = configurations.add(sourceSet.getCompileConfigurationName())
78             }
79             compileConfiguration.setVisible(false);
80             compileConfiguration.setDescription(
81                     String.format("Classpath for compiling the %s sources.", sourceSet.getName()))
82
83             Configuration packageConfiguration = configurations.findByName(
84                     sourceSet.getPackageConfigurationName())
85             if (packageConfiguration == null) {
86                 packageConfiguration = configurations.add(sourceSet.getPackageConfigurationName())
87             }
88             packageConfiguration.setVisible(false)
89             packageConfiguration.extendsFrom(compileConfiguration)
90             packageConfiguration.setDescription(
91                     String.format("Classpath packaged with the compiled %s classes.",
92                             sourceSet.getName()));
93
94             sourceSet.setRoot(String.format("src/%s", sourceSet.getName()))
95         }
96     }
97
98     void compileSdkVersion(int apiLevel) {
99         this.target = "android-" + apiLevel
100     }
101
102     void setCompileSdkVersion(int apiLevel) {
103         compileSdkVersion(apiLevel)
104     }
105
106     void compileSdkVersion(String target) {
107         this.target = target
108     }
109
110     void setCompileSdkVersion(String target) {
111         compileSdkVersion(target)
112     }
113
114     void sourceSets(Action<NamedDomainObjectContainer<AndroidSourceSet>> action) {
115         action.execute(sourceSetsContainer)
116     }
117
118     void defaultConfig(Action<GroupableProductFlavor> action) {
119         action.execute(defaultConfig)
120     }
121
122     void aaptOptions(Action<AaptOptionsImpl> action) {
123         action.execute(aaptOptions)
124     }
125
126     void dexOptions(Action<DexOptionsImpl> action) {
127         action.execute(dexOptions)
128     }
129
130     void testOptions(Action<TestOptions> action) {
131         action.execute(testOptions)
132     }
133
134     void compileOptions(Action<CompileOptions> action) {
135         action.execute(testOptions)
136     }
137
138     public DefaultDomainObjectSet<BuildVariant> getBuildVariants() {
139         plugin.createAndroidTasks()
140         return buildVariants
141     }
142
143     public DefaultDomainObjectSet<BuildVariant> getTestBuildVariants() {
144         plugin.createAndroidTasks()
145         return testBuildVariants
146     }
147
148     public String getCompileSdkVersion() {
149         return target
150     }
151 }