Search in sources :

Example 1 with InstantRunPatchingPolicy

use of com.android.build.gradle.internal.incremental.InstantRunPatchingPolicy 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;
                            }
                        }
                    }
                });
            }
        }
    });
}
Also used : TPatchDiffApkBuildTask(com.taobao.android.builder.tasks.tpatch.TPatchDiffApkBuildTask) AwbDataBindingMergeArtifactsTask(com.taobao.android.builder.tasks.app.databinding.AwbDataBindingMergeArtifactsTask) AwbDataBindingExportBuildInfoTask(com.taobao.android.builder.tasks.app.databinding.AwbDataBindingExportBuildInfoTask) PackageAwbsTask(com.taobao.android.builder.tasks.app.bundle.PackageAwbsTask) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) AwbDataBindingRenameTask(com.taobao.android.builder.tasks.app.databinding.AwbDataBindingRenameTask) PrepareAPTask(com.taobao.android.builder.tasks.PrepareAPTask) TPatchDiffResAPBuildTask(com.taobao.android.builder.tasks.tpatch.TPatchDiffResAPBuildTask) ProcessResAwbsTask(com.taobao.android.builder.tasks.app.bundle.ProcessResAwbsTask) TPatchTask(com.taobao.android.builder.tasks.tpatch.TPatchTask) BuildAtlasEnvTask(com.taobao.android.builder.tasks.app.BuildAtlasEnvTask) AbstractTask(org.gradle.api.internal.AbstractTask) PreparePackageIdsTask(com.taobao.android.builder.tasks.app.prepare.PreparePackageIdsTask) PrepareAaptTask(com.taobao.android.builder.tasks.app.prepare.PrepareAaptTask) Task(org.gradle.api.Task) JavacAwbsTask(com.taobao.android.builder.tasks.app.bundle.JavacAwbsTask) StandardizeLibManifestTask(com.taobao.android.builder.tasks.app.manifest.StandardizeLibManifestTask) PrepareBundleInfoTask(com.taobao.android.builder.tasks.app.prepare.PrepareBundleInfoTask) DiffBundleInfoTask(com.taobao.android.builder.tasks.tpatch.DiffBundleInfoTask) AwbDataBindingExportBuildInfoTask(com.taobao.android.builder.tasks.app.databinding.AwbDataBindingExportBuildInfoTask) ApplicationVariant(com.android.build.gradle.api.ApplicationVariant) TPatchTask(com.taobao.android.builder.tasks.tpatch.TPatchTask) ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection) UncheckedIOException(java.io.UncheckedIOException) ExtractTryWithResourcesSupportJar(com.android.build.gradle.internal.tasks.ExtractTryWithResourcesSupportJar) BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) StandardizeLibManifestTask(com.taobao.android.builder.tasks.app.manifest.StandardizeLibManifestTask) MtlTaskContext(com.taobao.android.builder.tasks.manager.MtlTaskContext) PrepareAPTask(com.taobao.android.builder.tasks.PrepareAPTask) ImmutableList(com.google.common.collect.ImmutableList) TPatchDiffResAPBuildTask(com.taobao.android.builder.tasks.tpatch.TPatchDiffResAPBuildTask) BuildAtlasEnvTask(com.taobao.android.builder.tasks.app.BuildAtlasEnvTask) MtlTransformContext(com.taobao.android.builder.tasks.manager.transform.MtlTransformContext) AtlasMainDexHelper(com.taobao.android.builder.AtlasMainDexHelper) InstantRunPatchingPolicy(com.android.build.gradle.internal.incremental.InstantRunPatchingPolicy) AbstractFileCollection(org.gradle.api.internal.file.AbstractFileCollection) TaskCollection(org.gradle.api.tasks.TaskCollection) AbstractFileCollection(org.gradle.api.internal.file.AbstractFileCollection) ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection) FileCollection(org.gradle.api.file.FileCollection) AwbDataBindingRenameTask(com.taobao.android.builder.tasks.app.databinding.AwbDataBindingRenameTask) File(java.io.File) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) Action(org.gradle.api.Action) PrepareBundleInfoTask(com.taobao.android.builder.tasks.app.prepare.PrepareBundleInfoTask) AwbDataBindingMergeArtifactsTask(com.taobao.android.builder.tasks.app.databinding.AwbDataBindingMergeArtifactsTask) TaskContainerAdaptor(com.android.build.gradle.internal.TaskContainerAdaptor) TPatchDiffApkBuildTask(com.taobao.android.builder.tasks.tpatch.TPatchDiffApkBuildTask) MtlTransformInjector(com.taobao.android.builder.tasks.manager.transform.MtlTransformInjector) TransformReplacer(com.taobao.android.builder.tasks.transform.TransformReplacer) PackageAwbsTask(com.taobao.android.builder.tasks.app.bundle.PackageAwbsTask) DiffBundleInfoTask(com.taobao.android.builder.tasks.tpatch.DiffBundleInfoTask) Supplier(com.google.common.base.Supplier) ApkData(com.android.ide.common.build.ApkData) JavacAwbsTask(com.taobao.android.builder.tasks.app.bundle.JavacAwbsTask) VariantScope(com.android.build.gradle.internal.scope.VariantScope) BaseVariantOutputImpl(com.android.build.gradle.internal.api.BaseVariantOutputImpl) AtlasBundleInstantApp(com.taobao.android.builder.tasks.instantapp.AtlasBundleInstantApp) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) MtlTaskInjector(com.taobao.android.builder.tasks.manager.MtlTaskInjector) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) TaskCollection(org.gradle.api.tasks.TaskCollection) TaskFactory(com.android.build.gradle.internal.TaskFactory) PreparePackageIdsTask(com.taobao.android.builder.tasks.app.prepare.PreparePackageIdsTask) PrepareAaptTask(com.taobao.android.builder.tasks.app.prepare.PrepareAaptTask) ProcessResAwbsTask(com.taobao.android.builder.tasks.app.bundle.ProcessResAwbsTask) AppVariantContext(com.android.build.gradle.internal.api.AppVariantContext)

Aggregations

ApplicationVariant (com.android.build.gradle.api.ApplicationVariant)1 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)1 TaskContainerAdaptor (com.android.build.gradle.internal.TaskContainerAdaptor)1 TaskFactory (com.android.build.gradle.internal.TaskFactory)1 AppVariantContext (com.android.build.gradle.internal.api.AppVariantContext)1 BaseVariantOutputImpl (com.android.build.gradle.internal.api.BaseVariantOutputImpl)1 InstantRunPatchingPolicy (com.android.build.gradle.internal.incremental.InstantRunPatchingPolicy)1 TransformTask (com.android.build.gradle.internal.pipeline.TransformTask)1 VariantScope (com.android.build.gradle.internal.scope.VariantScope)1 ExtractTryWithResourcesSupportJar (com.android.build.gradle.internal.tasks.ExtractTryWithResourcesSupportJar)1 ApkData (com.android.ide.common.build.ApkData)1 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 AtlasMainDexHelper (com.taobao.android.builder.AtlasMainDexHelper)1 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)1 PrepareAPTask (com.taobao.android.builder.tasks.PrepareAPTask)1 BuildAtlasEnvTask (com.taobao.android.builder.tasks.app.BuildAtlasEnvTask)1 JavacAwbsTask (com.taobao.android.builder.tasks.app.bundle.JavacAwbsTask)1 PackageAwbsTask (com.taobao.android.builder.tasks.app.bundle.PackageAwbsTask)1 ProcessResAwbsTask (com.taobao.android.builder.tasks.app.bundle.ProcessResAwbsTask)1