Search in sources :

Example 1 with TaskContainerAdaptor

use of com.android.build.gradle.internal.TaskContainerAdaptor 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)

Example 2 with TaskContainerAdaptor

use of com.android.build.gradle.internal.TaskContainerAdaptor in project atlas by alibaba.

the class BuildAtlasEnvTask method generate.

@TaskAction
void generate() throws TransformException {
    Set<ResolvedArtifactResult> compileArtifacts = compileManifests.getArtifacts();
    Set<ResolvedArtifactResult> jarArtifacts = compileJars.getArtifacts();
    Set<ResolvedArtifactResult> nativeLibsArtifacts = nativeLibs.getArtifacts();
    Set<ResolvedArtifactResult> javaResourcesArtifacts = javaResources.getArtifacts();
    Set<ResolvedArtifactResult> androidRes = res.getArtifacts();
    Set<ResolvedArtifactResult> androidAssets = assets.getArtifacts();
    Set<ResolvedArtifactResult> androidRnames = symbolListWithPackageNames.getArtifacts();
    AtlasDependencyTree androidDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    List<AwbBundle> bundles = new ArrayList<>();
    bundles.add(androidDependencyTree.getMainBundle());
    bundles.addAll(androidDependencyTree.getAwbBundles());
    // this is no used ,if used in future add to transform!
    Set<ResolvedArtifactResult> nativeLibsArtifacts2 = nativeLibs2.getArtifacts();
    nativeLibsArtifacts.addAll(nativeLibsArtifacts2);
    AtlasBuildContext.localLibs = nativeLibs2.getArtifactFiles().getFiles();
    for (ResolvedArtifactResult resolvedArtifactResult : jarArtifacts) {
        ComponentIdentifier componentIdentifier = resolvedArtifactResult.getId().getComponentIdentifier();
        if (componentIdentifier instanceof DefaultModuleComponentIdentifier) {
            allJars.add(new FileIdentity(((DefaultModuleComponentIdentifier) componentIdentifier).getGroup() + ":" + ((DefaultModuleComponentIdentifier) componentIdentifier).getModule(), resolvedArtifactResult.getFile(), resolvedArtifactResult.getId().getDisplayName().startsWith("classes.jar") ? false : true, false));
        } else if (componentIdentifier instanceof DefaultProjectComponentIdentifier) {
            String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
            allJars.add(new FileIdentity(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult.getFile(), resolvedArtifactResult.getId().getDisplayName().startsWith("classes.jar") ? false : true, true));
        } else if (componentIdentifier instanceof OpaqueComponentArtifactIdentifier) {
            if (resolvedArtifactResult.getFile().getAbsolutePath().contains("renderscript"))
                appLocalJars.add(new FileIdentity(componentIdentifier.getDisplayName(), resolvedArtifactResult.getFile(), true, false));
        }
    }
    for (ResolvedArtifactResult resolvedArtifactResult : compileArtifacts) {
        if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
            allManifests.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult.getFile());
        } else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
            String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
            allManifests.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult.getFile());
        }
    }
    for (ResolvedArtifactResult resolvedArtifactResult : nativeLibsArtifacts) {
        if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
            allSolibs.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult.getFile());
        } else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
            String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
            allSolibs.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult.getFile());
        }
    }
    for (ResolvedArtifactResult resolvedArtifactResult : javaResourcesArtifacts) {
        if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
            allJavaRes.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult.getFile());
        } else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
            String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
            allJavaRes.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult.getFile());
        }
    }
    for (ResolvedArtifactResult resolvedArtifactResult : androidRes) {
        if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
            allAndroidRes.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult);
        } else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
            String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
            allAndroidRes.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult);
        }
    }
    for (ResolvedArtifactResult resolvedArtifactResult : androidAssets) {
        if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
            allAndroidAssets.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult);
        } else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
            String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
            allAndroidAssets.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult);
        }
    }
    for (ResolvedArtifactResult resolvedArtifactResult : androidRnames) {
        if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
            allAndroidRnames.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult);
        } else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
            String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
            allAndroidRnames.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult);
        }
    }
    // app localJar is not support , this may course duplicate localjars
    appLocalJars.stream().forEach(fileIdentity -> AtlasBuildContext.atlasMainDexHelperMap.get(getVariantName()).addMainDex(fileIdentity));
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    List<AndroidLibrary> androidLibraries = atlasDependencyTree.getAllAndroidLibrarys();
    androidLibraries.stream().forEach(androidLibrary -> {
        if (androidLibrary instanceof AtlasAndroidLibraryImpl) {
            AndroidDependency fakeAndroidLibrary = ((AtlasAndroidLibraryImpl) androidLibrary).getAndroidLibrary();
            File id = null;
            if ((id = allManifests.get(androidLibrary.getResolvedCoordinates().getGroupId() + ":" + androidLibrary.getResolvedCoordinates().getArtifactId())) == null) {
                id = allManifests.get(androidLibrary.getResolvedCoordinates().toString().split(":")[1]);
            }
            if (id == null) {
                getLogger().warn("id == null---------------------" + androidLibrary.getResolvedCoordinates().getGroupId() + ":" + androidLibrary.getResolvedCoordinates().getArtifactId());
                throw new GradleException("excute failed! ");
            }
            ReflectUtils.updateField(fakeAndroidLibrary, "extractedFolder", id.getParentFile());
            ReflectUtils.updateField(fakeAndroidLibrary, "jarsRootFolder", id.getParentFile());
            ((AtlasAndroidLibraryImpl) androidLibrary).setAndroidLibrary(AndroidDependency.createExplodedAarLibrary(null, androidLibrary.getResolvedCoordinates(), androidLibrary.getName(), ((AtlasAndroidLibraryImpl) androidLibrary).getPath(), id.getParentFile()));
            appVariantContext.manifestMap.put(androidLibrary.getManifest().getAbsolutePath(), appVariantContext.getModifyManifest(androidLibrary));
        }
    });
    List<AndroidLibrary> mainDexAndroidLibraries = atlasDependencyTree.getMainBundle().getAndroidLibraries();
    List<JavaLibrary> mainDexJarLibraries = atlasDependencyTree.getMainBundle().getJavaLibraries();
    List<SoLibrary> mainSoLibraries = atlasDependencyTree.getMainBundle().getSoLibraries();
    for (AndroidLibrary androidLibrary : mainDexAndroidLibraries) {
        String name = androidLibrary.getResolvedCoordinates().getGroupId() + ":" + androidLibrary.getResolvedCoordinates().getArtifactId();
        String moudleName = androidLibrary.getResolvedCoordinates().toString().split(":")[1];
        fillMainManifest(name, moudleName);
        fillMainJar(name, moudleName);
        fillAllJavaRes(name, moudleName);
        fillMainSolibs(name, moudleName);
    }
    for (JavaLibrary jarLibrary : mainDexJarLibraries) {
        String moudleName = jarLibrary.getName().split(":")[1];
        String name = jarLibrary.getResolvedCoordinates().getGroupId() + ":" + jarLibrary.getResolvedCoordinates().getArtifactId();
        fillMainJar(name, moudleName);
        fillAllJavaRes(name, moudleName);
    }
    for (SoLibrary soLibrary : mainSoLibraries) {
        String name = soLibrary.getResolvedCoordinates().getGroupId() + ":" + soLibrary.getResolvedCoordinates().getArtifactId();
        String moudleName = soLibrary.getResolvedCoordinates().toString().split(":")[1];
        fillMainSolibs(name, moudleName);
    }
    for (AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        List<AndroidLibrary> awbAndroidLibraries = awbBundle.getAndroidLibraries();
        List<JavaLibrary> awbJarLibraries = awbBundle.getJavaLibraries();
        List<SoLibrary> awbSoLibraries = awbBundle.getSoLibraries();
        for (AndroidLibrary androidLibrary : awbAndroidLibraries) {
            String name = androidLibrary.getResolvedCoordinates().getGroupId() + ":" + androidLibrary.getResolvedCoordinates().getArtifactId();
            String moudleName = androidLibrary.getResolvedCoordinates().toString().split(":")[1];
            fillAwbManifest(name, moudleName, awbBundle);
            fillAwbJar(name, moudleName, awbBundle);
            fillAwbAllJavaRes(name, moudleName, awbBundle);
            fillAwbSolibs(name, moudleName, awbBundle);
            fillAwbAndroidRes(name, moudleName, awbBundle);
            fillAwbAndroidAssets(name, moudleName, awbBundle);
            fillAwbAndroidRs(name, moudleName, awbBundle);
        }
        for (JavaLibrary jarLibrary : awbJarLibraries) {
            String moudleName = jarLibrary.getName().split(":")[1];
            String name = jarLibrary.getResolvedCoordinates().getGroupId() + ":" + jarLibrary.getResolvedCoordinates().getArtifactId();
            fillAwbJar(name, moudleName, awbBundle);
        }
        for (SoLibrary soLibrary : awbSoLibraries) {
            String name = soLibrary.getResolvedCoordinates().getGroupId() + ":" + soLibrary.getResolvedCoordinates().getArtifactId();
            String moudleName = soLibrary.getResolvedCoordinates().toString().split(":")[1];
            fillAwbSolibs(name, moudleName, awbBundle);
        }
        String name = awbBundle.getResolvedCoordinates().getGroupId() + ":" + awbBundle.getResolvedCoordinates().getArtifactId();
        String moudleName = awbBundle.getResolvedCoordinates().toString().split(":")[1];
        fillAwbManifest(name, moudleName, awbBundle);
        fillAwbJar(name, moudleName, awbBundle);
        fillAwbAllJavaRes(name, moudleName, awbBundle);
        fillAwbSolibs(name, moudleName, awbBundle);
        fillAwbAndroidRes(name, moudleName, awbBundle);
        fillAwbAndroidAssets(name, moudleName, awbBundle);
        fillAwbAndroidRs(name, moudleName, awbBundle);
    }
    MergeResources mergeResources = appVariantContext.getScope().getMergeResourcesTask().get(new TaskContainerAdaptor(getProject().getTasks()));
    try {
        // mergeresources
        Field field = MergeResources.class.getDeclaredField("libraries");
        field.setAccessible(true);
        field.set(mergeResources, new MainArtifactsCollection((ArtifactCollection) field.get(mergeResources), getProject(), mergeResources.getVariantName()));
        appVariantOutputContext.getAwbTransformMap().values().stream().forEach(awbTransform -> {
            if (isMBundle(appVariantContext, awbTransform.getAwbBundle())) {
                try {
                    awbTransform.getAwbBundle().isMBundle = true;
                    awbTransform.getAwbBundle().bundleInfo.setIsMBundle(true);
                    field.set(mergeResources, new AppendMainArtifactsCollection(appVariantContext.getProject(), (ArtifactCollection) field.get(mergeResources), awbTransform.getAwbBundle(), ANDROID_RES));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        mergeResources.doLast(task -> FileUtils.listFiles(((MergeResources) task).getOutputDir(), new String[] { "xml" }, true).parallelStream().forEach(file -> {
            if (!AppendMainArtifactsCollection.bundle2Map.containsKey(file.getName())) {
                return;
            }
            List<String> lines = null;
            List<String> newLines = new ArrayList<>();
            try {
                lines = FileUtils.readLines(file);
                lines.forEach(s -> {
                    String s1 = s;
                    if (s.contains("http://schemas.android.com/apk/res/" + AppendMainArtifactsCollection.bundle2Map.get(file.getName()))) {
                        s1 = s.replace("http://schemas.android.com/apk/res/" + AppendMainArtifactsCollection.bundle2Map.get(file.getName()), "http://schemas.android.com/apk/res-auto");
                    }
                    newLines.add(s1);
                });
                FileUtils.writeLines(file, newLines);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }));
        // mergeSourcesSets
        MergeSourceSetFolders mergeSourceSetFolders = appVariantContext.getScope().getMergeAssetsTask().get(new TaskContainerAdaptor(getProject().getTasks()));
        Field assetsField = MergeSourceSetFolders.class.getDeclaredField("libraries");
        assetsField.setAccessible(true);
        assetsField.set(mergeSourceSetFolders, new MainArtifactsCollection((ArtifactCollection) assetsField.get(mergeSourceSetFolders), getProject(), mergeSourceSetFolders.getVariantName()));
        appVariantOutputContext.getAwbTransformMap().values().stream().forEach(awbTransform -> {
            if (isMBundle(appVariantContext, awbTransform.getAwbBundle())) {
                try {
                    awbTransform.getAwbBundle().isMBundle = true;
                    awbTransform.getAwbBundle().bundleInfo.setIsMBundle(true);
                    assetsField.set(mergeSourceSetFolders, new AppendMainArtifactsCollection(appVariantContext.getProject(), (ArtifactCollection) assetsField.get(mergeSourceSetFolders), awbTransform.getAwbBundle(), ASSETS));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        AtlasBuildContext.atlasMainDexHelperMap.get(getVariantName()).getMainSoFiles().put(appVariantContext.getScope().getMergeNativeLibsOutputDir().getAbsolutePath(), true);
    } catch (Exception e) {
    }
    // process resources
    ProcessAndroidResources processAndroidResources = appVariantContext.getScope().getProcessResourcesTask().get(new TaskContainerAdaptor(appVariantContext.getProject().getTasks()));
    FileCollection fileCollection = processAndroidResources.getSymbolListsWithPackageNames();
    Set<String> filesNames = new HashSet<>();
    for (String fileName : AtlasBuildContext.atlasMainDexHelperMap.get(getVariantName()).getMainManifestFiles().keySet()) {
        filesNames.add(fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1));
    }
    FileCollection updateFileCollection = fileCollection.filter(element -> filesNames.contains(element.getParentFile().getParentFile().getName()));
    ReflectUtils.updateField(processAndroidResources, "symbolListsWithPackageNames", updateFileCollection);
    appVariantOutputContext.getAwbTransformMap().values().stream().forEach(awbTransform -> {
        if (isMBundle(appVariantContext, awbTransform.getAwbBundle())) {
            awbTransform.getAwbBundle().isMBundle = true;
            awbTransform.getAwbBundle().bundleInfo.setIsMBundle(true);
            FileCollection fc = new AppendMainArtifactsCollection(appVariantContext.getProject(), processAndroidResources.getSymbolListsWithPackageNames(), awbTransform.getAwbBundle(), SYMBOL_LIST_WITH_PACKAGE_NAME).getArtifactFiles();
            ReflectUtils.updateField(processAndroidResources, "symbolListsWithPackageNames", fc);
        }
    });
    appVariantContext.processResAwbsTask.mainDexSymbolFileCollection = updateFileCollection;
    // FileCollection fs = appVariantContext.getScope().getArtifactFileCollection(AndroidArtifacts.ConsumedConfigType.COMPILE_CLASSPATH,AndroidArtifacts.ArtifactScope.ALL,AndroidArtifacts.ArtifactType.CLASSES);
    // fs.getFiles().forEach(new Consumer<File>() {
    // @Override
    // public void accept(File file) {
    // if (file.exists()){
    // try {
    // JarFile jarFile = new JarFile(file);
    // Enumeration<JarEntry>enumeration = jarFile.entries();
    // while (enumeration.hasMoreElements()){
    // JarEntry jarEntry = enumeration.nextElement();
    // if (jarEntry.getName().endsWith(".class")){
    // ClassReader classReader = new ClassReader(jarFile.getInputStream(jarEntry));
    // String[]ss = classReader.getInterfaces();
    // if (ss!= null){
    // for (String s:ss){
    // if (s.contains("IExternalComponentGetter")||s.contains("IExternalComponentGetter.class")){
    // System.out.println("IExternalComponentGetter:"+jarEntry.getName());
    // }else if (s.contains("IExternalModuleGetter")||s.contains("IExternalModuleGetter.class")){
    // System.out.println("IExternalModuleGetter:"+jarEntry.getName());
    // }
    // }
    // }
    // 
    // }
    // }
    // } catch (IOException e) {
    // e.printStackTrace();
    // }
    // }
    // }
    // });
    allManifests.clear();
    allJavaRes.clear();
    allSolibs.clear();
    allJars.clear();
    allAndroidAssets.clear();
    allAndroidRes.clear();
// 
// try {
// duplicateClazzNote();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
Also used : IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) AbstractFileCollection(org.gradle.api.internal.file.AbstractFileCollection) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ResourceType(com.android.resources.ResourceType) JarFile(java.util.jar.JarFile) MergeSourceSetFolders(com.android.build.gradle.tasks.MergeSourceSetFolders) AndroidDependency(com.android.builder.dependency.level2.AndroidDependency) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction) AtlasDependencyGraph(com.android.build.gradle.internal.ide.AtlasDependencyGraph) AtlasAndroidArtifacts(com.android.build.gradle.internal.publishing.AtlasAndroidArtifacts) com.android.ide.common.res2(com.android.ide.common.res2) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) Task(org.gradle.api.Task) RUNTIME_CLASSPATH(com.android.build.gradle.internal.publishing.AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH) JavaLibrary(com.android.builder.model.JavaLibrary) AtlasBuildContext(com.taobao.android.builder.AtlasBuildContext) DefaultModuleComponentIdentifier(org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier) com.google.common.collect(com.google.common.collect) TrueFileFilter(org.apache.commons.io.filefilter.TrueFileFilter) AppVariantContext(com.android.build.gradle.internal.api.AppVariantContext) TransformException(com.android.build.api.transform.TransformException) Project(org.gradle.api.Project) Nullable(com.android.annotations.Nullable) SoLibrary(com.taobao.android.builder.dependency.model.SoLibrary) AndroidArtifacts(com.android.build.gradle.internal.publishing.AndroidArtifacts) ALL(com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactScope.ALL) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) COMPILE_CLASSPATH(com.android.build.gradle.internal.publishing.AndroidArtifacts.ConsumedConfigType.COMPILE_CLASSPATH) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) Spec(org.gradle.api.specs.Spec) ClassReader(org.objectweb.asm.ClassReader) NonNull(com.android.annotations.NonNull) FalseFileFilter(org.apache.commons.io.filefilter.FalseFileFilter) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) NotNull(org.jetbrains.annotations.NotNull) AppendMainArtifactsCollection(com.taobao.android.builder.tasks.app.merge.AppendMainArtifactsCollection) MergeManifests(com.android.build.gradle.tasks.MergeManifests) FilenameFilter(java.io.FilenameFilter) BaseTask(com.android.build.gradle.internal.tasks.BaseTask) java.util(java.util) MainArtifactsCollection(com.taobao.android.builder.tasks.app.merge.MainArtifactsCollection) OpaqueComponentArtifactIdentifier(org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier) BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) AndroidLibrary(com.android.builder.model.AndroidLibrary) FileCollection(org.gradle.api.file.FileCollection) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) JarEntry(java.util.jar.JarEntry) ANNOTATION_PROCESSOR(com.android.build.gradle.internal.publishing.AndroidArtifacts.ConsumedConfigType.ANNOTATION_PROCESSOR) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) ReflectUtils(com.taobao.android.builder.tools.ReflectUtils) Action(org.gradle.api.Action) ProjectDependency(org.gradle.api.artifacts.ProjectDependency) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) ProcessAndroidResources(com.android.build.gradle.tasks.ProcessAndroidResources) Field(java.lang.reflect.Field) AtlasAndroidLibraryImpl(com.android.build.gradle.internal.ide.AtlasAndroidLibraryImpl) File(java.io.File) Consumer(java.util.function.Consumer) MainFilesCollection(com.taobao.android.builder.tasks.app.merge.MainFilesCollection) GradleException(org.gradle.api.GradleException) DefaultProjectComponentIdentifier(org.gradle.internal.component.local.model.DefaultProjectComponentIdentifier) ArtifactType(com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactType) Preconditions(com.google.common.base.Preconditions) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) TaskContainerAdaptor(com.android.build.gradle.internal.TaskContainerAdaptor) MergeResources(com.android.build.gradle.tasks.MergeResources) AppendMainArtifactsCollection(com.taobao.android.builder.tasks.app.merge.AppendMainArtifactsCollection) MainArtifactsCollection(com.taobao.android.builder.tasks.app.merge.MainArtifactsCollection) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) DefaultModuleComponentIdentifier(org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) DefaultProjectComponentIdentifier(org.gradle.internal.component.local.model.DefaultProjectComponentIdentifier) AbstractFileCollection(org.gradle.api.internal.file.AbstractFileCollection) FileCollection(org.gradle.api.file.FileCollection) TaskContainerAdaptor(com.android.build.gradle.internal.TaskContainerAdaptor) Field(java.lang.reflect.Field) MergeResources(com.android.build.gradle.tasks.MergeResources) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) AppendMainArtifactsCollection(com.taobao.android.builder.tasks.app.merge.AppendMainArtifactsCollection) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) DefaultModuleComponentIdentifier(org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier) IOException(java.io.IOException) DefaultProjectComponentIdentifier(org.gradle.internal.component.local.model.DefaultProjectComponentIdentifier) SoLibrary(com.taobao.android.builder.dependency.model.SoLibrary) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) TransformException(com.android.build.api.transform.TransformException) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) MergeSourceSetFolders(com.android.build.gradle.tasks.MergeSourceSetFolders) AtlasAndroidLibraryImpl(com.android.build.gradle.internal.ide.AtlasAndroidLibraryImpl) AndroidDependency(com.android.builder.dependency.level2.AndroidDependency) ProcessAndroidResources(com.android.build.gradle.tasks.ProcessAndroidResources) JavaLibrary(com.android.builder.model.JavaLibrary) AndroidLibrary(com.android.builder.model.AndroidLibrary) GradleException(org.gradle.api.GradleException) OpaqueComponentArtifactIdentifier(org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier) JarFile(java.util.jar.JarFile) File(java.io.File) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 3 with TaskContainerAdaptor

use of com.android.build.gradle.internal.TaskContainerAdaptor in project atlas by alibaba.

the class PackageAwbsTask method createAwbPackages.

/**
 * Directory of so
 */
@TaskAction
void createAwbPackages() throws ExecutionException, InterruptedException, IOException {
    File awbApkOutputDir = appVariantContext.getAwbApkOutputDir();
    FileUtils.cleanOutputDir(awbApkOutputDir);
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    if (null == atlasDependencyTree) {
        return;
    }
    ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper(taskName, getLogger(), 0);
    List<Runnable> runnables = new ArrayList<>();
    final AtomicLong dexTotalTime = new AtomicLong(0);
    final AtomicLong packageTotalTime = new AtomicLong(0);
    final Map<String, Long[]> monitors = new HashMap<String, Long[]>();
    long startTime = System.currentTimeMillis();
    for (final AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        if (awbBundle.isMBundle) {
            continue;
        }
        runnables.add(() -> {
            try {
                long start = System.currentTimeMillis();
                // create dex
                File dexOutputFile = appVariantContext.getAwbDexOutput(awbBundle.getName());
                long endDex = System.currentTimeMillis();
                // PACKAGE APP:
                File resourceFile = null;
                if (appVariantContext.getScope().useResourceShrinker()) {
                    resourceFile = appVariantOutputContext.getAwbCompressResourcePackageOutputFile(awbBundle);
                } else {
                    resourceFile = appVariantOutputContext.getAwbAndroidResourcesMap().get(awbBundle.getName()).getPackageOutputFile();
                }
                if (!resourceFile.exists()) {
                    resourceFile = appVariantOutputContext.getAwbAndroidResourcesMap().get(awbBundle.getName()).getPackageOutputFile();
                }
                Set<File> dexFolders = new HashSet<File>();
                dexFolders.add(dexOutputFile);
                Set<File> jniFolders = Sets.newHashSet();
                if (appVariantOutputContext.getAwbJniFolder(awbBundle) != null && appVariantOutputContext.getAwbJniFolder(awbBundle).exists()) {
                    jniFolders.add(appVariantOutputContext.getAwbJniFolder(awbBundle));
                }
                Set<File> javaResourcesLocations = Sets.newHashSet();
                if (appVariantContext.getAtlasExtension().getTBuildConfig().isIncremental() && awbBundle.getAllLibraryAars().size() > 1) {
                    File baseAwb = appVariantOutputContext.getVariantContext().apContext.getBaseAwb(awbBundle.getAwbSoName());
                    if (baseAwb != null) {
                        ZipFile files = new ZipFile(baseAwb);
                        ZipEntry entry = files.getEntry("classes.dex");
                        if (entry == null) {
                            throw new DexException("Expected classes.dex in " + baseAwb);
                        }
                        File file = new File(dexOutputFile, "classes.dex");
                        com.android.dex.Dex[] dexes = new com.android.dex.Dex[2];
                        dexes[0] = new com.android.dex.Dex(file);
                        dexes[1] = new com.android.dex.Dex(files.getInputStream(entry));
                        com.android.dex.Dex merged = new DexMerger(dexes, CollisionPolicy.KEEP_FIRST, new DxContext()).merge();
                        merged.writeTo(file);
                        javaResourcesLocations.add(baseAwb);
                    }
                }
                if (appVariantContext.getAtlasExtension().getTBuildConfig().getMergeAwbJavaRes()) {
                    javaResourcesLocations.add(new File(appVariantOutputContext.getAwbJavaResFolder(awbBundle), "res.jar"));
                }
                AwbApkPackageTask awbApkPackageTask = new AwbApkPackageTask(getProject().files(resourceFile), appVariantContext, awbBundle, appVariantOutputContext, getProject().files(dexFolders), getProject().files(javaResourcesLocations), getProject().files(appVariantOutputContext.getVariantContext().getMergeAssets(awbBundle)), getProject().files(jniFolders), getProject().files(awbBundle.getMergedManifest()), getBuilder(), config.getMinSdkVersion().getApiLevel(), getName());
                File awbFile = awbApkPackageTask.doFullTaskAction();
                bundles.put(awbBundle, awbFile);
                // //                        //TODO 2.3
                // AtlasBuildContext.androidBuilderMap.get(getProject())
                // .oldPackageApk(resourceFile.getAbsolutePath(),
                // dexFolders,
                // javaResourcesLocations,
                // jniFolders,
                // null,
                // getAbiFilters(),
                // config.getBuildType().isJniDebuggable(),
                // null,
                // getOutputFile(awbBundle),
                // config.getMinSdkVersion().getApiLevel(),
                // new com.google.common.base.Predicate<String>() {
                // @Override
                // public boolean apply(@Nullable String s) {
                // return false;
                // }
                // });
                long endPackage = System.currentTimeMillis();
                dexTotalTime.addAndGet(endDex - start);
                packageTotalTime.addAndGet(endPackage - endDex);
                monitors.put(awbBundle.getName(), new Long[] { endDex - start, endPackage - endDex });
            } catch (Throwable e) {
                // e.printStackTrace();
                throw new GradleException("package " + awbBundle.getName() + " failed", e);
            }
        });
    }
    executorServicesHelper.execute(runnables);
    getLogger().info(">>>>> packageAwbs >>>>>>>>>>>>");
    FileLogger fileLogger = FileLogger.getInstance("packageAwbs");
    fileLogger.log("totalTime is " + (System.currentTimeMillis() - startTime));
    fileLogger.log("dexTotalTime is " + dexTotalTime);
    fileLogger.log("packageTotalTime is " + packageTotalTime);
    String format = "[packageawb]  bundle:%50s  dexTime: %10d  packageTime: %10d ";
    for (String bundle : monitors.keySet()) {
        Long[] value = monitors.get(bundle);
        fileLogger.log(String.format(format, bundle, value[0], value[1]));
    }
    getLogger().info(">>>>> packageAwbs >>>>>>>>>>>>");
    PackageApplication packageApplication = appVariantContext.getScope().getPackageApplicationTask().get(new TaskContainerAdaptor(appVariantContext.getScope().getGlobalScope().getProject().getTasks()));
    packageApplication.doFirst(new FirstApkAction(appVariantOutputContext));
    packageApplication.doLast(new LastApkAction());
}
Also used : DexException(com.android.dex.DexException) ZipEntry(java.util.zip.ZipEntry) DexMerger(com.android.dx.merge.DexMerger) TaskContainerAdaptor(com.android.build.gradle.internal.TaskContainerAdaptor) ExecutorServicesHelper(com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) LastApkAction(com.taobao.android.builder.tasks.app.bundle.actions.LastApkAction) DxContext(com.android.dx.command.dexer.DxContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) ZipFile(java.util.zip.ZipFile) FirstApkAction(com.taobao.android.builder.tasks.app.bundle.actions.FirstApkAction) GradleException(org.gradle.api.GradleException) AtomicLong(java.util.concurrent.atomic.AtomicLong) FileLogger(com.taobao.android.builder.tools.log.FileLogger) PackageApplication(com.android.build.gradle.tasks.PackageApplication) ZipFile(java.util.zip.ZipFile) File(java.io.File) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 4 with TaskContainerAdaptor

use of com.android.build.gradle.internal.TaskContainerAdaptor 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");
                    }
                }
            }
        }
    });
}
Also used : Zip(org.gradle.api.tasks.bundling.Zip) FeatureVariantContext(com.android.build.gradle.internal.api.FeatureVariantContext) JarExtractTask(com.taobao.android.builder.tasks.library.JarExtractTask) LibVariantContext(com.android.build.gradle.internal.api.LibVariantContext) IOException(java.io.IOException) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) TaskContainerAdaptor(com.android.build.gradle.internal.TaskContainerAdaptor) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) AwoPropHandler(com.taobao.android.builder.tools.ideaplugin.AwoPropHandler) Field(java.lang.reflect.Field) BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) AwbGenerator(com.taobao.android.builder.tasks.library.AwbGenerator) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) GradleException(org.gradle.api.GradleException) LibraryVariantOutput(com.android.build.gradle.api.LibraryVariantOutput) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) Collection(java.util.Collection) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) FileCollection(org.gradle.api.file.FileCollection) TransformStream(com.android.build.gradle.internal.pipeline.TransformStream) TBuildType(com.taobao.android.builder.extension.TBuildType)

Aggregations

TaskContainerAdaptor (com.android.build.gradle.internal.TaskContainerAdaptor)4 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)3 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)3 File (java.io.File)3 IOException (java.io.IOException)3 GradleException (org.gradle.api.GradleException)3 FileCollection (org.gradle.api.file.FileCollection)3 AppVariantContext (com.android.build.gradle.internal.api.AppVariantContext)2 TransformTask (com.android.build.gradle.internal.pipeline.TransformTask)2 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)2 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)2 Field (java.lang.reflect.Field)2 Action (org.gradle.api.Action)2 Task (org.gradle.api.Task)2 ArtifactCollection (org.gradle.api.artifacts.ArtifactCollection)2 AbstractFileCollection (org.gradle.api.internal.file.AbstractFileCollection)2 TaskAction (org.gradle.api.tasks.TaskAction)2 JavaCompile (org.gradle.api.tasks.compile.JavaCompile)2 NonNull (com.android.annotations.NonNull)1 Nullable (com.android.annotations.Nullable)1