Clean up: use constants where we should be using them.
[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 import com.android.build.gradle.internal.CompileOptions
18 import com.android.build.gradle.internal.dsl.AaptOptionsImpl
19 import com.android.build.gradle.internal.dsl.AndroidSourceSetFactory
20 import com.android.build.gradle.internal.dsl.DexOptionsImpl
21 import com.android.build.gradle.internal.dsl.ProductFlavorDsl
22 import com.android.build.gradle.internal.test.TestOptions
23 import com.android.builder.BuilderConstants
24 import com.android.builder.ProductFlavor
25 import org.gradle.api.Action
26 import org.gradle.api.NamedDomainObjectContainer
27 import org.gradle.api.artifacts.Configuration
28 import org.gradle.api.artifacts.ConfigurationContainer
29 import org.gradle.api.internal.DefaultDomainObjectSet
30 import org.gradle.api.internal.project.ProjectInternal
31 import org.gradle.internal.reflect.Instantiator
32 /**
33  * Base android extension for all android plugins.
34  */
35 public abstract class BaseExtension {
36
37     private String target
38
39     final ProductFlavor defaultConfig
40     final AaptOptionsImpl aaptOptions
41     final DexOptionsImpl dexOptions
42     final TestOptions testOptions
43     final CompileOptions compileOptions
44
45
46     private final BasePlugin plugin
47     private final DefaultDomainObjectSet<BuildVariant> buildVariants =
48         new DefaultDomainObjectSet<BuildVariant>(BuildVariant.class)
49     private final DefaultDomainObjectSet<BuildVariant> testBuildVariants =
50         new DefaultDomainObjectSet<BuildVariant>(BuildVariant.class)
51
52     /**
53      * The source sets container.
54      */
55     final NamedDomainObjectContainer<AndroidSourceSet> sourceSetsContainer
56
57     BaseExtension(BasePlugin plugin, ProjectInternal project, Instantiator instantiator) {
58         this.plugin = plugin
59
60         defaultConfig = instantiator.newInstance(ProductFlavorDsl.class, BuilderConstants.MAIN)
61
62         aaptOptions = instantiator.newInstance(AaptOptionsImpl.class)
63         dexOptions = instantiator.newInstance(DexOptionsImpl.class)
64         testOptions = instantiator.newInstance(TestOptions.class)
65         compileOptions = instantiator.newInstance(CompileOptions.class)
66
67         sourceSetsContainer = project.container(AndroidSourceSet,
68                 new AndroidSourceSetFactory(instantiator, project.fileResolver))
69
70         sourceSetsContainer.whenObjectAdded { AndroidSourceSet sourceSet ->
71             ConfigurationContainer configurations = project.getConfigurations()
72
73             Configuration compileConfiguration = configurations.findByName(
74                     sourceSet.getCompileConfigurationName())
75             if (compileConfiguration == null) {
76                 compileConfiguration = configurations.add(sourceSet.getCompileConfigurationName())
77             }
78             compileConfiguration.setVisible(false);
79             compileConfiguration.setDescription(
80                     String.format("Classpath for compiling the %s sources.", sourceSet.getName()))
81
82             Configuration packageConfiguration = configurations.findByName(
83                     sourceSet.getPackageConfigurationName())
84             if (packageConfiguration == null) {
85                 packageConfiguration = configurations.add(sourceSet.getPackageConfigurationName())
86             }
87             packageConfiguration.setVisible(false)
88             packageConfiguration.extendsFrom(compileConfiguration)
89             packageConfiguration.setDescription(
90                     String.format("Classpath packaged with the compiled %s classes.",
91                             sourceSet.getName()));
92
93             sourceSet.setRoot(String.format("src/%s", sourceSet.getName()))
94         }
95     }
96
97     void compileSdkVersion(int apiLevel) {
98         this.target = "android-" + apiLevel
99     }
100
101     void setCompileSdkVersion(int apiLevel) {
102         compileSdkVersion(apiLevel)
103     }
104
105     void compileSdkVersion(String target) {
106         this.target = target
107     }
108
109     void setCompileSdkVersion(String target) {
110         compileSdkVersion(target)
111     }
112
113     void sourceSets(Action<NamedDomainObjectContainer<AndroidSourceSet>> action) {
114         action.execute(sourceSetsContainer)
115     }
116
117     void defaultConfig(Action<ProductFlavor> action) {
118         action.execute(defaultConfig)
119     }
120
121     void aaptOptions(Action<AaptOptionsImpl> action) {
122         action.execute(aaptOptions)
123     }
124
125     void dexOptions(Action<DexOptionsImpl> action) {
126         action.execute(dexOptions)
127     }
128
129     void testOptions(Action<TestOptions> action) {
130         action.execute(testOptions)
131     }
132
133     void compileOptions(Action<CompileOptions> action) {
134         action.execute(testOptions)
135     }
136
137     public DefaultDomainObjectSet<BuildVariant> getBuildVariants() {
138         plugin.createAndroidTasks()
139         return buildVariants
140     }
141
142     public DefaultDomainObjectSet<BuildVariant> getTestBuildVariants() {
143         plugin.createAndroidTasks()
144         return testBuildVariants
145     }
146
147     public String getCompileSdkVersion() {
148         return target
149     }
150 }