| 1 |
/* |
| 2 |
* Copyright (C) 2015 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 |
|
| 17 |
package com.android.build.gradle.model; |
| 18 |
|
| 19 |
import static com.android.build.gradle.model.ModelConstants.ANDROID_BUILDER; |
| 20 |
import static com.android.build.gradle.model.ModelConstants.ANDROID_CONFIG_ADAPTOR; |
| 21 |
import static com.android.build.gradle.model.ModelConstants.BINARIES; |
| 22 |
import static com.android.build.gradle.model.ModelConstants.COMPONENTS; |
| 23 |
import static com.android.build.gradle.model.ModelConstants.EXTRA_MODEL_INFO; |
| 24 |
import static com.android.build.gradle.model.ModelConstants.IS_APPLICATION; |
| 25 |
import static com.android.build.gradle.model.ModelConstants.NDK_HANDLER; |
| 26 |
import static com.android.build.gradle.model.ModelConstants.TASK_MANAGER; |
| 27 |
|
| 28 |
import com.android.build.gradle.AndroidConfig; |
| 29 |
import com.android.build.gradle.internal.ExtraModelInfo; |
| 30 |
import com.android.build.gradle.internal.ndk.NdkHandler; |
| 31 |
import com.android.build.gradle.internal.TaskManager; |
| 32 |
import com.android.build.gradle.internal.VariantManager; |
| 33 |
import com.android.build.gradle.internal.dependency.NativeDependencyResolveResult; |
| 34 |
import com.android.build.gradle.internal.model.ModelBuilder; |
| 35 |
import com.android.build.gradle.managed.NdkAbiOptions; |
| 36 |
import com.android.build.gradle.model.internal.AndroidBinaryInternal; |
| 37 |
import com.android.build.gradle.model.internal.AndroidComponentSpecInternal; |
| 38 |
import com.android.builder.core.AndroidBuilder; |
| 39 |
import com.android.builder.model.AndroidProject; |
| 40 |
import com.google.common.collect.Multimap; |
| 41 |
|
| 42 |
import org.gradle.api.Project; |
| 43 |
import org.gradle.model.ModelMap; |
| 44 |
import org.gradle.model.internal.core.ModelPath; |
| 45 |
import org.gradle.model.internal.registry.ModelRegistry; |
| 46 |
import org.gradle.model.internal.type.ModelType; |
| 47 |
import org.gradle.model.internal.type.ModelTypes; |
| 48 |
import org.gradle.tooling.provider.model.ToolingModelBuilder; |
| 49 |
|
| 50 |
/** |
| 51 |
* A ToolingModelBuilder for creating AndroidProject in the component model plugin. |
| 52 |
* |
| 53 |
* It retrieves models from ModelRegistry and uses ModelBuilder to create AndroidProject. |
| 54 |
* |
| 55 |
* This model builder uses Gradle's internal API as a public API for create tooling model from |
| 56 |
* component model is not yet available. |
| 57 |
*/ |
| 58 |
public class ComponentModelBuilder implements ToolingModelBuilder { |
| 59 |
|
| 60 |
ModelBuilder modelBuilder; |
| 61 |
ModelRegistry registry; |
| 62 |
|
| 63 |
|
| 64 |
public ComponentModelBuilder(ModelRegistry registry) { |
| 65 |
this.registry = registry; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public boolean canBuild(String modelName) { |
| 70 |
return modelName.equals(AndroidProject.class.getName()); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public Object buildAll(String modelName, Project project) { |
| 75 |
if (modelBuilder == null) { |
| 76 |
modelBuilder = createModelBuilder(); |
| 77 |
} |
| 78 |
return modelBuilder.buildAll(modelName, project); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Return the ModelType for a parameterized Multimap. |
| 83 |
*/ |
| 84 |
private static <K, V> ModelType<Multimap<K, V>> multimapModelType( |
| 85 |
Class<K> keyClass, |
| 86 |
Class<V> valueClass) { |
| 87 |
return new ModelType.Builder<Multimap<K, V>>() {} |
| 88 |
.where(new ModelType.Parameter<K>() {}, ModelType.of(keyClass)) |
| 89 |
.where(new ModelType.Parameter<V>() {}, ModelType.of(valueClass)) |
| 90 |
.build(); |
| 91 |
} |
| 92 |
|
| 93 |
private ModelBuilder createModelBuilder() { |
| 94 |
AndroidBuilder androidBuilder = registry.realize( |
| 95 |
new ModelPath(ANDROID_BUILDER), |
| 96 |
ModelType.of(AndroidBuilder.class)); |
| 97 |
AndroidComponentSpecInternal componentSpec = registry.realize( |
| 98 |
new ModelPath(COMPONENTS), |
| 99 |
ModelTypes.modelMap(AndroidComponentSpecInternal.class)) |
| 100 |
.get(AndroidComponentModelPlugin.COMPONENT_NAME); |
| 101 |
VariantManager variantManager = componentSpec.getVariantManager(); |
| 102 |
TaskManager taskManager = registry.realize( |
| 103 |
new ModelPath(TASK_MANAGER), |
| 104 |
ModelType.of(TaskManager.class)); |
| 105 |
AndroidConfig extension = registry.realize( |
| 106 |
new ModelPath(ANDROID_CONFIG_ADAPTOR), |
| 107 |
ModelType.of(AndroidConfig.class)); |
| 108 |
ExtraModelInfo extraModelInfo = registry.realize( |
| 109 |
new ModelPath(EXTRA_MODEL_INFO), |
| 110 |
ModelType.of(ExtraModelInfo.class)); |
| 111 |
Boolean isApplication = registry.realize( |
| 112 |
new ModelPath(IS_APPLICATION), |
| 113 |
ModelType.of(Boolean.class)); |
| 114 |
NdkHandler ndkHandler = registry.realize( |
| 115 |
new ModelPath(NDK_HANDLER), |
| 116 |
ModelType.of(NdkHandler.class)); |
| 117 |
ModelMap<AndroidBinaryInternal> binaries = registry.realize( |
| 118 |
new ModelPath(BINARIES), |
| 119 |
ModelTypes.modelMap(AndroidBinaryInternal.class)); |
| 120 |
ModelMap<NdkAbiOptions> abiOptions = registry.realize( |
| 121 |
new ModelPath(ModelConstants.ABI_OPTIONS), |
| 122 |
ModelTypes.modelMap(NdkAbiOptions.class)); |
| 123 |
Multimap<String, NativeDependencyResolveResult> nativeDependencies = registry.realize( |
| 124 |
new ModelPath(ModelConstants.NATIVE_DEPENDENCIES), |
| 125 |
multimapModelType(String.class, NativeDependencyResolveResult.class)); |
| 126 |
Multimap<String, NativeDependencyResolveResult> jniLibsDependencies = registry.realize( |
| 127 |
new ModelPath(ModelConstants.JNILIBS_DEPENDENCIES), |
| 128 |
multimapModelType(String.class, NativeDependencyResolveResult.class)); |
| 129 |
|
| 130 |
return new ModelBuilder( |
| 131 |
androidBuilder, |
| 132 |
variantManager, |
| 133 |
taskManager, |
| 134 |
extension, |
| 135 |
extraModelInfo, |
| 136 |
ndkHandler, |
| 137 |
new ComponentNativeLibraryFactory( |
| 138 |
binaries, ndkHandler, abiOptions, nativeDependencies, jniLibsDependencies), |
| 139 |
!isApplication, |
| 140 |
AndroidProject.GENERATION_COMPONENT); |
| 141 |
} |
| 142 |
} |