use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.
the class MtlTaskInjector method injectTasks.
public void injectTasks(List<MtlTaskContext> mtlTaskContexts, AndroidBuilder androidBuilder) {
Collection<BaseVariantOutput> baseVariantOutputDataList = variantContext.getVariantOutputData();
for (final BaseVariantOutput vod : baseVariantOutputDataList) {
// TODO What if the intermediate step does not generate task
List<Task> beforeTasks = new ArrayList<Task>();
for (MtlTaskContext mtlTaskContext : mtlTaskContexts) {
// Get the task instance
List<Task> tasks = new ArrayList<Task>();
if (mtlTaskContext.isSystemTask()) {
if (null != mtlTaskContext.getSysTask()) {
tasks.add(mtlTaskContext.getSysTask());
} else if (null != mtlTaskContext.getSysTaskClazz()) {
Class taskClazz = mtlTaskContext.getSysTaskClazz();
tasks.addAll(findTask(taskClazz, variantContext.getVariantName()));
} else {
Task task = project.getTasks().findByName(variantContext.getBaseVariantData().getTaskName(mtlTaskContext.getTaskName(), ""));
if (null != task) {
tasks.add(task);
}
}
} else {
Task task = mtlTaskFactory.createTask(variantContext, vod, mtlTaskContext.getTaskActionClazz());
if (null != task) {
if (null != androidBuilder && task instanceof BaseTask) {
((BaseTask) task).setAndroidBuilder(androidBuilder);
}
tasks.add(task);
}
}
if (tasks.isEmpty()) {
// throw new RuntimeException("task is not found " + mtlTaskContext);
project.getLogger().info("task is not found " + vod.getName() + mtlTaskContext);
} else {
if (!beforeTasks.isEmpty()) {
for (Task task : tasks) {
project.getLogger().debug("[MtlTaskInjector]" + vod.getName() + ":" + task + "->" + StringUtils.join(beforeTasks.toArray()));
for (Task before : beforeTasks) {
project.getLogger().info("[tasks] set task " + task.getProject().getName() + ":" + task.getName() + "->" + before.getProject().getName() + ":" + before.getName());
task.dependsOn(before);
}
}
}
beforeTasks.clear();
beforeTasks.addAll(tasks);
if (null != mtlTaskContext.getTaskFilter()) {
List<Task> beforeTasks2 = mtlTaskContext.getTaskFilter().getBeforeTasks(project, vod);
for (Task task : tasks) {
project.getLogger().debug("[MtlTaskInjector]" + vod.getName() + ":" + task + "->" + StringUtils.join(beforeTasks2.toArray()));
for (Task before : beforeTasks2) {
project.getLogger().info("[tasks] set task " + task.getProject().getName() + ":" + task.getName() + "->" + before.getProject().getName() + ":" + before.getName());
task.dependsOn(before);
}
}
}
}
}
}
}
use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.
the class AtlasAppTaskManager method runTask.
@Override
public void runTask() {
appExtension.getApplicationVariants().forEach(new Consumer<ApplicationVariant>() {
@Override
public void accept(ApplicationVariant applicationVariant) {
AppVariantContext appVariantContext = AtlasBuildContext.sBuilderAdapter.appVariantContextFactory.getAppVariantContext(project, applicationVariant);
if (!AtlasBuildContext.atlasMainDexHelperMap.containsKey(appVariantContext.getVariantName())) {
AtlasBuildContext.atlasMainDexHelperMap.put(appVariantContext.getVariantName(), new AtlasMainDexHelper());
}
TransformReplacer transformReplacer = new TransformReplacer(appVariantContext);
repalceAndroidBuilder(applicationVariant);
List<MtlTaskContext> mtlTaskContextList = new ArrayList<MtlTaskContext>();
mtlTaskContextList.add(new MtlTaskContext(appVariantContext.getVariantData().preBuildTask));
mtlTaskContextList.add(new MtlTaskContext(BuildAtlasEnvTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(ScanDupResTask.ConfigActon.class, null));
mtlTaskContextList.add(new MtlTaskContext(LogDependenciesTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(PrepareAPTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(appVariantContext.getVariantData().mergeAssetsTask));
mtlTaskContextList.add(new MtlTaskContext(RenderscriptCompile.class));
mtlTaskContextList.add(new MtlTaskContext(StandardizeLibManifestTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(PrepareBundleInfoTask.ConfigAction.class, null));
if (!atlasExtension.getTBuildConfig().getClassInject() && atlasExtension.isAtlasEnabled()) {
mtlTaskContextList.add(new MtlTaskContext(GenerateAtlasSourceTask.ConfigAction.class, null));
}
mtlTaskContextList.add(new MtlTaskContext(PreparePackageIdsTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(PrepareAaptTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(AidlCompile.class));
mtlTaskContextList.add(new MtlTaskContext(GenerateBuildConfig.class));
mtlTaskContextList.add(new MtlTaskContext(MergeResAwbsConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(MergeAssetAwbsConfigAction.class, null));
if (null != androidExtension.getDataBinding() && androidExtension.getDataBinding().isEnabled() && !appVariantContext.getAtlasExtension().getTBuildConfig().getAllBundlesToMdex()) {
// mtlTaskContextList.add(
// new MtlTaskContext(AwbDataBindingProcessLayoutTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(AwbDataBindingExportBuildInfoTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(AwbDataBindingMergeArtifactsTask.ConfigAction.class, null));
}
mtlTaskContextList.add(new MtlTaskContext(MergeManifests.class));
mtlTaskContextList.add(new MtlTaskContext(MergeManifestAwbsConfigAction.class, null));
// mtlTaskContextList.add(new MtlTaskContext(MergeResV4Dir.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(ProcessAndroidResources.class));
ProcessAndroidResources processAndroidResources = appVariantContext.getScope().getProcessResourcesTask().get(new TaskContainerAdaptor(appVariantContext.getProject().getTasks()));
if (processAndroidResources.isAapt2Enabled()) {
processAndroidResources.doLast(new Action<Task>() {
@Override
public void execute(Task task) {
File processResourcePackageOutputDirectory = appVariantContext.getScope().getProcessResourcePackageOutputDirectory();
File[] files = processResourcePackageOutputDirectory.listFiles((file, name) -> name.endsWith(SdkConstants.DOT_RES));
for (File file : files) {
try {
ResourcePatch.makePatchable(file);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
});
}
mtlTaskContextList.add(new MtlTaskContext(ProcessResAwbsTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(JavacAwbsTask.ConfigAction.class, null));
if (null != androidExtension.getDataBinding() && androidExtension.getDataBinding().isEnabled() && !appVariantContext.getAtlasExtension().getTBuildConfig().getAllBundlesToMdex()) {
mtlTaskContextList.add(new MtlTaskContext(AwbDataBindingRenameTask.ConfigAction.class, null));
}
mtlTaskContextList.add(new MtlTaskContext(TransformTask.class));
mtlTaskContextList.add(new MtlTaskContext(PackageAwbsTask.ConfigAction.class, null));
if (appVariantContext.getAtlasExtension().getTBuildConfig().isIncremental() && (appVariantContext.getBuildType().getPatchConfig() == null || !appVariantContext.getBuildType().getPatchConfig().isCreateTPatch())) {
// mtlTaskContextList.add(new MtlTaskContext(PrepareBaseApkTask.ConfigAction.class, null));
final TaskFactory tasks = new TaskContainerAdaptor(project.getTasks());
VariantScope variantScope = appVariantContext.getVariantData().getScope();
// create the stream generated from this task
variantScope.getTransformManager().addStream(OriginalStream.builder(project, applicationVariant.getName()).addContentType(QualifiedContent.DefaultContentType.RESOURCES).addScope(QualifiedContent.Scope.PROJECT).setFolders(new Supplier<Collection<File>>() {
@Override
public Collection<File> get() {
return ImmutableList.of(new File(appVariantContext.apContext.getBaseApk() + "_"));
}
}).build());
}
final TaskFactory tasks = new TaskContainerAdaptor(project.getTasks());
VariantScope variantScope = appVariantContext.getVariantData().getScope();
mtlTaskContextList.add(new MtlTaskContext(PackageApplication.class));
if (appVariantContext.getAtlasExtension().isInstantAppEnabled()) {
mtlTaskContextList.add(new MtlTaskContext(AtlasBundleInstantApp.ConfigAction.class, null));
}
mtlTaskContextList.add(new MtlTaskContext(ApBuildTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(DiffBundleInfoTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(TPatchDiffResAPBuildTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(TPatchDiffApkBuildTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext(TPatchTask.ConfigAction.class, null));
mtlTaskContextList.add(new MtlTaskContext("assemble"));
new MtlTaskInjector(appVariantContext).injectTasks(mtlTaskContextList, tAndroidBuilder);
List<MtlTransformContext> mtlTransformContextList = new ArrayList<MtlTransformContext>();
if (atlasExtension.getTBuildConfig().getClassInject()) {
mtlTransformContextList.add(new MtlTransformContext(ClassInjectTransform.class, ProGuardTransform.class, DexTransform.class));
}
if (variantScope.getInstantRunBuildContext().isInInstantRunMode() && appVariantContext.getVariantConfiguration().getBuildType().isMinifyEnabled()) {
mtlTransformContextList.add(new MtlTransformContext(DelegateProguardTransform.class, ExtractJarsTransform.class, InstantRunTransform.class));
}
if (!mtlTransformContextList.isEmpty()) {
new MtlTransformInjector(appVariantContext).injectTasks(mtlTransformContextList);
}
Collection<BaseVariantOutput> baseVariantOutputDataList = appVariantContext.getVariantOutputData();
boolean multiDexEnabled = appVariantContext.getVariantData().getVariantConfiguration().isMultiDexEnabled();
if (atlasExtension.getTBuildConfig().isAtlasMultiDex() && multiDexEnabled) {
transformReplacer.replaceMultiDexListTransform();
}
transformReplacer.replaceProguardTransform();
transformReplacer.disableCache();
if (variantScope.getGlobalScope().getExtension().getDataBinding().isEnabled() && !appVariantContext.getAtlasExtension().getTBuildConfig().getAllBundlesToMdex()) {
transformReplacer.replaceDataBindingMergeArtifactsTransform();
}
for (final BaseVariantOutput vod : baseVariantOutputDataList) {
transformReplacer.replaceFixStackFramesTransform(vod);
transformReplacer.replaceDesugarTransform(vod);
transformReplacer.replaceDexArchiveBuilderTransform(vod);
transformReplacer.replaceDexExternalLibMerge(vod);
transformReplacer.replaceDexMerge(vod);
transformReplacer.replaceDexTransform(appVariantContext, vod);
transformReplacer.replaceShrinkResourcesTransform();
transformReplacer.replaceMergeJavaResourcesTransform(appVariantContext, vod);
transformReplacer.repalaceSomeInstantTransform(vod);
if (atlasExtension.getTBuildConfig().isIncremental()) {
InstantRunPatchingPolicy patchingPolicy = variantScope.getInstantRunBuildContext().getPatchingPolicy();
BaseVariantOutputImpl variantOutput = (BaseVariantOutputImpl) vod;
ApkData data = ApkDataUtils.get(variantOutput);
AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getFullVariantName());
//
}
}
// Boolean includeCompileClasspath =
// appVariantContext.getScope().getVariantConfiguration()
// .getJavaCompileOptions()
// .getAnnotationProcessorOptions()
// .getIncludeCompileClasspath();
// appVariantContext.getVariantData().javaCompilerTask.doFirst(task -> {
// JavaCompile compile = (JavaCompile) task;
// Set<File> mainDexFiles = new MainFilesCollection(appVariantContext.getVariantName()).getFiles();
// FileCollection mainFiles = appVariantContext.getProject().files(mainDexFiles);
// FileCollection files = appVariantContext.getScope().getArtifactFileCollection(ANNOTATION_PROCESSOR, ALL, JAR);
// FileCollection bootFiles = appVariantContext.getProject().files(appVariantContext.getScope().getGlobalScope().getAndroidBuilder().getBootClasspath(false));
// mainFiles = mainFiles.plus(bootFiles);
// FileCollection fileCollection = compile.getClasspath();
// File kotlinClasses = null;
// for (File file : fileCollection) {
// if (file.getAbsolutePath().contains("kotlin-classes")) {
// mainFiles = mainFiles.plus(appVariantContext.getProject().files(file));
// kotlinClasses = file;
// break;
// }
// }
// compile.setClasspath(mainFiles);
// if (Boolean.TRUE.equals(includeCompileClasspath)) {
// compile.getOptions().setAnnotationProcessorPath(files.plus(mainFiles));
// }
// });
appVariantContext.getVariantData().javaCompilerTask.doLast(new Action<Task>() {
@Override
public void execute(Task task) {
JavaCompile compile = (JavaCompile) task;
AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).getInputDirs().add(compile.getDestinationDir());
}
});
PackageAndroidArtifact packageAndroidArtifact = appVariantContext.getVariantData().getTaskByType(PackageAndroidArtifact.class);
if (packageAndroidArtifact != null) {
ReflectUtils.updateField(packageAndroidArtifact, "javaResourceFiles", new AbstractFileCollection() {
@Override
public String getDisplayName() {
return "java-merge-res.jar";
}
@Override
public Set<File> getFiles() {
if (AtlasBuildContext.atlasMainDexHelperMap.get(packageAndroidArtifact.getVariantName()).getMainJavaRes() == null) {
return Sets.newHashSet();
}
return Sets.newHashSet(AtlasBuildContext.atlasMainDexHelperMap.get(packageAndroidArtifact.getVariantName()).getMainJavaRes());
}
});
}
TaskCollection<ExtractTryWithResourcesSupportJar> taskCollection = appVariantContext.getProject().getTasks().withType(ExtractTryWithResourcesSupportJar.class);
for (ExtractTryWithResourcesSupportJar task : taskCollection) {
task.doLast(new Action<Task>() {
@Override
public void execute(Task task) {
ConfigurableFileCollection fileCollection = variantScope.getTryWithResourceRuntimeSupportJar();
for (File file : fileCollection.getFiles()) {
if (file.exists()) {
AtlasBuildContext.atlasMainDexHelperMap.get(variantScope.getFullVariantName()).addMainDex(new BuildAtlasEnvTask.FileIdentity("runtime-deps-try-with-resources", file, false, false));
break;
}
}
}
});
}
}
});
}
use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.
the class AtlasLibTaskManager method runTask.
@Override
public void runTask() {
new UpdatePomTask(project).updatePom();
libraryExtension.getLibraryVariants().forEach(libraryVariant -> {
LibVariantContext libVariantContext = new LibVariantContext((LibraryVariantImpl) libraryVariant, project, atlasExtension, libraryExtension);
TBuildType tBuildType = libVariantContext.getBuildType();
if (null != tBuildType) {
try {
new AwoPropHandler().process(tBuildType, atlasExtension.getBundleConfig());
} catch (Exception e) {
throw new GradleException("process awo exception", e);
}
}
AwbGenerator awbGenerator = new AwbGenerator(atlasExtension);
Collection<BaseVariantOutput> list = libVariantContext.getBaseVariant().getOutputs();
if (null != list) {
for (BaseVariantOutput libVariantOutputData : list) {
Zip zipTask = ((LibraryVariantOutput) (libVariantOutputData)).getPackageLibrary();
if (atlasExtension.getBundleConfig().isJarEnabled()) {
new JarExtractTask().generateJarArtifict(zipTask);
}
// Build the awb and extension
if (atlasExtension.getBundleConfig().isAwbBundle()) {
awbGenerator.generateAwbArtifict(zipTask, libVariantContext);
}
if (null != tBuildType && (StringUtils.isNotEmpty(tBuildType.getBaseApDependency()) || null != tBuildType.getBaseApFile()) && libraryVariant.getName().equals("debug")) {
atlasExtension.getTBuildConfig().setUseCustomAapt(true);
libVariantContext.setBundleTask(zipTask);
try {
libVariantContext.setAwbBundle(awbGenerator.createAwbBundle(libVariantContext));
} catch (IOException e) {
throw new GradleException("set awb bundle error");
}
// if (atlasExtension.getBundleConfig().isAwbBundle()) {
// createAwoTask(libVariantContext, zipTask);
// } else {
// createDexTask(libVariantContext, zipTask);
// }
}
}
// List<TransformTask>transformTasks = TransformManager.findTransformTaskByTransformType(libVariantContext,LibraryAarJarsTransform.class);
// for (TransformTask transformTask: transformTasks){
// Transform transform = transformTask.getTransform();
// if (transform instanceof LibraryBaseTransform){
// ReflectUtils.updateField(transform,"excludeListProviders", Lists.newArrayList(new AtlasExcludeListProvider()));
// }
// }
}
});
}
use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.
the class DelegateProguardTransform method applyBundleInOutConfigration.
public File applyBundleInOutConfigration(final AppVariantContext appVariantContext) {
VariantScope variantScope = appVariantContext.getScope();
GlobalScope globalScope = variantScope.getGlobalScope();
File proguardOut = new File(Joiner.on(File.separatorChar).join(String.valueOf(globalScope.getBuildDir()), FD_OUTPUTS, "mapping", variantScope.getVariantConfiguration().getDirName()));
File awbInOutConfig = new File(proguardOut, "awb_inout_config.cfg");
// Add awb configuration
AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName());
if (dependencyTree.getAwbBundles().size() > 0) {
BaseVariantOutput vod = (BaseVariantOutput) appVariantContext.getVariantOutputData().iterator().next();
AppVariantOutputContext appVariantOutputContext = appVariantContext.getAppVariantOutputContext(ApkDataUtils.get(vod));
File awbObfuscatedDir = new File(globalScope.getIntermediatesDir(), "/classes-proguard/" + variantScope.getVariantConfiguration().getDirName());
AwbProguardConfiguration awbProguardConfiguration = new AwbProguardConfiguration(appVariantOutputContext.getAwbTransformMap().values(), awbObfuscatedDir, appVariantOutputContext);
try {
awbProguardConfiguration.printConfigFile(awbInOutConfig);
} catch (IOException e) {
throw new GradleException("", e);
}
defaultProguardFiles.add(awbInOutConfig);
}
return awbInOutConfig;
}
use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.
the class AtlasFeatureTaskManager method runTask.
@Override
public void runTask() {
if (featureExtension.getBaseFeature()) {
return;
}
featureExtension.getFeatureVariants().forEach(featureVariant -> {
FeatureVariantContext featureVariantContext = new FeatureVariantContext((FeatureVariantImpl) featureVariant, project, atlasExtension, featureExtension);
ArtifactCollection allArtifacts = featureVariantContext.getScope().getArtifactCollection(AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.EXTERNAL, AndroidArtifacts.ArtifactType.CLASSES);
ArtifactCollection artifacts = featureVariantContext.getScope().getArtifactCollection(AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.ALL, AndroidArtifacts.ArtifactType.FEATURE_TRANSITIVE_DEPS);
ArtifactCollection filterArtifacts = new FilteredArtifactCollection(featureVariantContext.getProject(), allArtifacts, artifacts.getArtifactFiles());
List<TransformTask> transformTasks = TransformManager.findTransformTaskByTransformType(featureVariantContext, FixStackFramesTransform.class);
if (transformTasks != null) {
for (TransformTask transformTask : transformTasks) {
try {
Field field = StreamBasedTask.class.getDeclaredField("consumedInputStreams");
Field field1 = StreamBasedTask.class.getDeclaredField("referencedInputStreams");
field1.setAccessible(true);
field.setAccessible(true);
Collection<TransformStream> consumedInputStreams = (Collection<TransformStream>) field.get(transformTask);
Collection<TransformStream> referencedInputStreams = (Collection<TransformStream>) field1.get(transformTask);
for (TransformStream stream : consumedInputStreams) {
if (stream.getContentTypes().contains(QualifiedContent.DefaultContentType.CLASSES) && stream.getScopes().contains(QualifiedContent.Scope.EXTERNAL_LIBRARIES)) {
ReflectUtils.updateField(stream, "fileCollection", filterArtifacts.getArtifactFiles());
ReflectUtils.updateField(stream, "artifactCollection", filterArtifacts);
break;
}
}
for (TransformStream transformStream : referencedInputStreams) {
if (transformStream.getContentTypes().contains(QualifiedContent.DefaultContentType.CLASSES) && transformStream.getScopes().contains(QualifiedContent.Scope.PROVIDED_ONLY)) {
ReflectUtils.updateField(transformStream, "fileCollection", project.files());
// ReflectUtils.updateField(transformStream, "artifactCollection", filterArtifacts);
}
}
} catch (Exception e) {
}
}
}
featureVariantContext.getScope().getProcessResourcesTask().get(new TaskContainerAdaptor(featureVariantContext.getProject().getTasks())).setEnableAapt2(true);
});
featureExtension.getLibraryVariants().forEach(libraryVariant -> {
LibVariantContext libVariantContext = new LibVariantContext((LibraryVariantImpl) libraryVariant, project, atlasExtension, featureExtension);
TBuildType tBuildType = libVariantContext.getBuildType();
if (null != tBuildType) {
try {
new AwoPropHandler().process(tBuildType, atlasExtension.getBundleConfig());
} catch (Exception e) {
throw new GradleException("process awo exception", e);
}
}
AwbGenerator awbGenerator = new AwbGenerator(atlasExtension);
Collection<BaseVariantOutput> list = libVariantContext.getBaseVariant().getOutputs();
if (null != list) {
for (BaseVariantOutput libVariantOutputData : list) {
Zip zipTask = ((LibraryVariantOutput) (libVariantOutputData)).getPackageLibrary();
if (atlasExtension.getBundleConfig().isJarEnabled()) {
new JarExtractTask().generateJarArtifict(zipTask);
}
// Build the awb and extension
// if (atlasExtension.getBundleConfig().isAwbBundle()) {
awbGenerator.generateAwbArtifict(zipTask, libVariantContext);
if (null != tBuildType && (StringUtils.isNotEmpty(tBuildType.getBaseApDependency()) || null != tBuildType.getBaseApFile()) && libraryVariant.getName().equals("debug")) {
atlasExtension.getTBuildConfig().setUseCustomAapt(true);
libVariantContext.setBundleTask(zipTask);
try {
libVariantContext.setAwbBundle(awbGenerator.createAwbBundle(libVariantContext));
} catch (IOException e) {
throw new GradleException("set awb bundle error");
}
}
}
}
});
}
Aggregations