Search in sources :

Example 1 with VariantScope

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

the class MergeAwbResourceConfigAction method execute.

@Override
public void execute(MergeResources mergeResourcesTask) {
    VariantScope scope = variantContext.getScope();
    final BaseVariantData<? extends BaseVariantOutputData> variantData = scope.getVariantData();
    final AndroidConfig extension = scope.getGlobalScope().getExtension();
    //final VariantConfiguration variantConfig = variantData.getVariantConfiguration();
    mergeResourcesTask.setMinSdk(variantData.getVariantConfiguration().getMinSdkVersion().getApiLevel());
    mergeResourcesTask.setAndroidBuilder(scope.getGlobalScope().getAndroidBuilder());
    mergeResourcesTask.setVariantName(scope.getVariantConfiguration().getFullName());
    GlobalScope globalScope = scope.getGlobalScope();
    mergeResourcesTask.setIncrementalFolder(scope.getIncrementalDir(getName()));
    try {
        FieldUtils.writeField(mergeResourcesTask, "variantScope", scope, true);
    } catch (IllegalAccessException e) {
        throw new GradleException("exception", e);
    }
    // where blame is useful, and once for packaging where it is not.
    if (includeDependencies) {
        mergeResourcesTask.setBlameLogFolder(scope.getResourceBlameLogDir());
    } else {
    //            if (variantContext instanceof AppVariantContext) {
    //                mergeResourcesTask.setBlameLogFolder(((AppVariantContext) variantContext).getAwbBlameLogFolder(awbBundle));
    //            }
    }
    mergeResourcesTask.setProcess9Patch(process9Patch);
    mergeResourcesTask.setCrunchPng(extension.getAaptOptions().getCruncherEnabled());
    VectorDrawablesOptions vectorDrawablesOptions = variantData.getVariantConfiguration().getMergedFlavor().getVectorDrawables();
    Set<String> generatedDensities = vectorDrawablesOptions.getGeneratedDensities();
    mergeResourcesTask.setGeneratedDensities(Objects.firstNonNull(generatedDensities, Collections.<String>emptySet()));
    mergeResourcesTask.setDisableVectorDrawables(vectorDrawablesOptions.getUseSupportLibrary() || mergeResourcesTask.getGeneratedDensities().isEmpty());
    //mergeResourcesTask.setUseNewCruncher(extension.getAaptOptions().getUseNewCruncher());
    final boolean validateEnabled = AndroidGradleOptions.isResourceValidationEnabled(scope.getGlobalScope().getProject());
    mergeResourcesTask.setValidateEnabled(validateEnabled);
    ConventionMappingHelper.map(mergeResourcesTask, "inputResourceSets", new Callable<List<ResourceSet>>() {

        @Override
        public List<ResourceSet> call() throws Exception {
            List<ResourceSet> resourceSets = Lists.newArrayList();
            List<? extends AndroidLibrary> bundleDeps = awbBundle.getLibraryDependencies();
            // the list of dependency must be reversed to use the right overlay order.
            for (int n = bundleDeps.size() - 1; n >= 0; n--) {
                AndroidLibrary dependency = bundleDeps.get(n);
                File resFolder = dependency.getResFolder();
                if (resFolder.isDirectory()) {
                    ResourceSet resourceSet = new ResourceSet(dependency.getFolder().getName(), true);
                    resourceSet.addSource(resFolder);
                    resourceSets.add(resourceSet);
                }
            }
            File awbResourceFolder = awbBundle.getResFolder();
            if (awbResourceFolder.isDirectory()) {
                ResourceSet resourceSet = new ResourceSet(awbBundle.getFolder().getName(), true);
                resourceSet.addSource(awbResourceFolder);
                resourceSets.add(resourceSet);
            }
            return resourceSets;
        }
    });
    mergeResourcesTask.setOutputDir(variantContext.getMergeResources(awbBundle));
    mergeResourcesTask.setGeneratedPngsOutputDir(variantContext.getPngsOutputDir(awbBundle));
//        if (variantContext instanceof AppVariantContext) {
//            AppVariantContext appVariantContext = (AppVariantContext) variantContext;
//            appVariantContext.getAwbMergeResourceTasks().put(awbBundle.getName(), mergeResourcesTask);
//        }
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) VariantScope(com.android.build.gradle.internal.scope.VariantScope) ResourceSet(com.android.ide.common.res2.ResourceSet) AndroidConfig(com.android.build.gradle.AndroidConfig) GradleException(org.gradle.api.GradleException) VectorDrawablesOptions(com.android.builder.model.VectorDrawablesOptions) AndroidLibrary(com.android.builder.model.AndroidLibrary) GradleException(org.gradle.api.GradleException) List(java.util.List) File(java.io.File)

Example 2 with VariantScope

use of com.android.build.gradle.internal.scope.VariantScope 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 3 with VariantScope

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

the class DelegateProguardTransform method applyBundleProguardConfigration.

public void applyBundleProguardConfigration(final AppVariantContext appVariantContext) {
    Set<String> blackList = appVariantContext.getAtlasExtension().getTBuildConfig().getBundleProguardConfigBlackList();
    List<File> proguardFiles = new ArrayList<>();
    VariantScope variantScope = appVariantContext.getScope();
    for (AwbBundle awbBundle : AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName()).getAwbBundles()) {
        for (AndroidLibrary androidDependency : awbBundle.getAllLibraryAars()) {
            File proguardRules = androidDependency.getProguardRules();
            String groupName = androidDependency.getResolvedCoordinates().getGroupId() + ":" + androidDependency.getResolvedCoordinates().getArtifactId();
            if (blackList.contains(groupName)) {
                sLogger.info("[proguard] skip proguard from " + androidDependency.getResolvedCoordinates());
                continue;
            }
            if (proguardRules.isFile()) {
                proguardFiles.add(proguardRules);
                sLogger.warn("[proguard] load proguard from " + androidDependency.getResolvedCoordinates());
            } else {
                sLogger.info("[proguard] missing proguard from " + androidDependency.getResolvedCoordinates());
            }
        }
    }
    defaultProguardFiles.addAll(proguardFiles);
}
Also used : VariantScope(com.android.build.gradle.internal.scope.VariantScope) AndroidLibrary(com.android.builder.model.AndroidLibrary) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) File(java.io.File)

Example 4 with VariantScope

use of com.android.build.gradle.internal.scope.VariantScope 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;
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) VariantScope(com.android.build.gradle.internal.scope.VariantScope) AwbProguardConfiguration(com.taobao.android.builder.tools.proguard.AwbProguardConfiguration) GradleException(org.gradle.api.GradleException) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) IOException(java.io.IOException) File(java.io.File) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree)

Example 5 with VariantScope

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

the class AtlasDependencyGraph method computeArtifactCollection.

public static ArtifactCollection computeArtifactCollection(VariantScope variantScope, @NonNull AtlasAndroidArtifacts.ConsumedConfigType configType, @NonNull AndroidArtifacts.ArtifactScope scope, @NonNull AtlasAndroidArtifacts.AtlasArtifactType artifactType) {
    Configuration configuration;
    switch(configType) {
        case COMPILE_CLASSPATH:
            configuration = variantScope.getVariantData().getVariantDependency().getCompileClasspath();
            break;
        case RUNTIME_CLASSPATH:
            configuration = variantScope.getVariantData().getVariantDependency().getRuntimeClasspath();
            break;
        case BUNDLECOMPILE_CLASSPATH:
            configuration = variantScope.getGlobalScope().getProject().getConfigurations().maybeCreate(AtlasPlugin.BUNDLE_COMPILE);
            break;
        case ANNOTATION_PROCESSOR:
            configuration = variantScope.getVariantData().getVariantDependency().getAnnotationProcessorConfiguration();
            break;
        case METADATA_VALUES:
            configuration = variantScope.getVariantData().getVariantDependency().getMetadataValuesConfiguration();
            break;
        default:
            throw new RuntimeException("unknown ConfigType value");
    }
    Action<AttributeContainer> attributes = container -> container.attribute(ARTIFACT_TYPE, artifactType.getType());
    Spec<ComponentIdentifier> filter = getComponentFilter(scope);
    boolean lenientMode = Boolean.TRUE.equals(variantScope.getGlobalScope().getProjectOptions().get(BooleanOption.IDE_BUILD_MODEL_ONLY));
    ArtifactCollection artifacts = configuration.getIncoming().artifactView(config -> {
        config.attributes(attributes);
        if (filter != null) {
            config.componentFilter(filter);
        }
        // TODO somehow read the unresolved dependencies?
        config.lenient(lenientMode);
    }).getArtifacts();
    if (configType == AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH && variantScope.getVariantConfiguration().getType() == VariantType.FEATURE && artifactType != AtlasAndroidArtifacts.AtlasArtifactType.FEATURE_TRANSITIVE_DEPS) {
        artifacts = new FilteredArtifactCollection(variantScope.getGlobalScope().getProject(), artifacts, computeArtifactCollection(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, scope, AtlasAndroidArtifacts.AtlasArtifactType.FEATURE_TRANSITIVE_DEPS).getArtifactFiles());
    }
    return artifacts;
}
Also used : ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) AtlasPlugin(com.taobao.android.builder.AtlasPlugin) AtlasAndroidArtifacts(com.android.build.gradle.internal.publishing.AtlasAndroidArtifacts) CreatingCache(com.android.ide.common.caching.CreatingCache) Matcher(java.util.regex.Matcher) FileUtils(com.android.utils.FileUtils) ResolvedVariantResult(org.gradle.api.artifacts.result.ResolvedVariantResult) com.google.common.collect(com.google.common.collect) MavenCoordinatesImpl(com.android.builder.dependency.MavenCoordinatesImpl) Artifact(org.gradle.api.component.Artifact) GraphItem(com.android.builder.model.level2.GraphItem) Project(org.gradle.api.Project) Nullable(com.android.annotations.Nullable) AndroidArtifacts(com.android.build.gradle.internal.publishing.AndroidArtifacts) BooleanOption(com.android.build.gradle.options.BooleanOption) ComponentArtifactIdentifier(org.gradle.api.artifacts.component.ComponentArtifactIdentifier) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) Spec(org.gradle.api.specs.Spec) NonNull(com.android.annotations.NonNull) VariantType(com.android.builder.core.VariantType) ARTIFACT_TYPE(com.android.build.gradle.internal.publishing.AndroidArtifacts.ARTIFACT_TYPE) com.android.build.gradle.internal.ide.level2(com.android.build.gradle.internal.ide.level2) Pattern(java.util.regex.Pattern) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) EMPTY_DEPENDENCY_GRAPH(com.android.build.gradle.internal.ide.ModelBuilder.EMPTY_DEPENDENCY_GRAPH) JvmLibrary(org.gradle.jvm.JvmLibrary) java.util(java.util) SdkConstants(com.android.SdkConstants) OpaqueComponentArtifactIdentifier(org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier) VariantAttr(com.android.build.gradle.internal.dependency.VariantAttr) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ConfigurationDependencyGraphs(com.android.build.gradle.internal.dependency.ConfigurationDependencyGraphs) VariantScope(com.android.build.gradle.internal.scope.VariantScope) EMPTY_DEPENDENCIES_IMPL(com.android.build.gradle.internal.ide.ModelBuilder.EMPTY_DEPENDENCIES_IMPL) Configuration(org.gradle.api.artifacts.Configuration) ImmutableCollectors(com.android.utils.ImmutableCollectors) DependencyHandler(org.gradle.api.artifacts.dsl.DependencyHandler) AttributeContainer(org.gradle.api.attributes.AttributeContainer) com.android.builder.model(com.android.builder.model) ArtifactResolutionQuery(org.gradle.api.artifacts.query.ArtifactResolutionQuery) SourcesArtifact(org.gradle.language.base.artifact.SourcesArtifact) Action(org.gradle.api.Action) Library(com.android.builder.model.level2.Library) File(java.io.File) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) Consumer(java.util.function.Consumer) ArtifactCollectionWithExtraArtifact(com.android.build.gradle.internal.dependency.ArtifactCollectionWithExtraArtifact) JavaLibraryImpl(com.android.build.gradle.internal.ide.level2.JavaLibraryImpl) Preconditions(com.google.common.base.Preconditions) DependencyGraphs(com.android.builder.model.level2.DependencyGraphs) Configuration(org.gradle.api.artifacts.Configuration) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) AttributeContainer(org.gradle.api.attributes.AttributeContainer) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection)

Aggregations

VariantScope (com.android.build.gradle.internal.scope.VariantScope)16 File (java.io.File)12 IOException (java.io.IOException)8 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)7 GradleException (org.gradle.api.GradleException)7 GlobalScope (com.android.build.gradle.internal.scope.GlobalScope)6 AndroidLibrary (com.android.builder.model.AndroidLibrary)5 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)4 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)4 GradleVariantConfiguration (com.android.build.gradle.internal.core.GradleVariantConfiguration)3 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)3 AtlasExtension (com.taobao.android.builder.extension.AtlasExtension)3 JarFile (java.util.jar.JarFile)3 StopExecutionException (org.gradle.api.tasks.StopExecutionException)3 TransformTask (com.android.build.gradle.internal.pipeline.TransformTask)2 BaseVariantOutputData (com.android.build.gradle.internal.variant.BaseVariantOutputData)2 ManifestProcessorTask (com.android.build.gradle.tasks.ManifestProcessorTask)2 MergeManifests (com.android.build.gradle.tasks.MergeManifests)2 AndroidDependencyTree (com.taobao.android.builder.dependency.AndroidDependencyTree)2 ArrayList (java.util.ArrayList)2