Search in sources :

Example 11 with VariantScope

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

the class AwbProguradHook method hookProguardTask.

/**
     * hook混淆的任务,加入awb的混淆配置
     *
     * @param appVariantContext
     */
public void hookProguardTask(final AppVariantContext appVariantContext) {
    final VariantScope variantScope = appVariantContext.getScope();
    List<TransformTask> proguaradTransformTasks = getTransformTaskByTransformType(appVariantContext, ProGuardTransform.class);
    for (TransformTask proguaradTransformTask : proguaradTransformTasks) {
        final ProGuardTransform proGuardTransform = (ProGuardTransform) proguaradTransformTask.getTransform();
        if (null != proGuardTransform) {
            proguaradTransformTask.doFirst(new Action<Task>() {

                @Override
                public void execute(Task task) {
                    GlobalScope globalScope = variantScope.getGlobalScope();
                    File proguardOut = new File(Joiner.on(File.separatorChar).join(String.valueOf(globalScope.getBuildDir()), FD_OUTPUTS, "mapping", variantScope.getVariantConfiguration().getDirName()));
                    //为了方便排查,先把configuration打印到目录
                    proGuardTransform.printconfiguration(new File(proguardOut, "tmp_config.cfg"));
                    final File outConfigFile = new File(proguardOut, "awb_config.cfg");
                    //增加awb的配置
                    AndroidDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName());
                    if (null == dependencyTree) {
                        throw new StopExecutionException("DependencyTree cannot be null!");
                    }
                    if (dependencyTree.getAwbBundles().size() > 0) {
                        BaseVariantOutputData vod = appVariantContext.getVariantData().getOutputs().get(0);
                        AppVariantOutputContext appVariantOutputContext = getAppVariantOutputContext(appVariantContext, vod);
                        File awbObfuscatedDir = new File(globalScope.getIntermediatesDir(), "/classes-proguard/" + variantScope.getVariantConfiguration().getDirName());
                        AwbProguardConfiguration awbProguardConfiguration = new AwbProguardConfiguration(appVariantOutputContext.getAwbTransformMap().values(), awbObfuscatedDir, appVariantOutputContext);
                        try {
                            awbProguardConfiguration.printConfigFile(outConfigFile);
                        } catch (IOException e) {
                            throw new GradleException("", e);
                        }
                        proGuardTransform.setConfigurationFiles(new Supplier<Collection<File>>() {

                            @Override
                            public Collection<File> get() {
                                Set<File> proguardFiles = new HashSet<File>();
                                ((HashSet<File>) proguardFiles).add(outConfigFile);
                                return proguardFiles;
                            }
                        });
                    }
                    File mappingFile = null;
                    if (null != appVariantContext.apContext.getApExploredFolder() && appVariantContext.apContext.getApExploredFolder().exists()) {
                        mappingFile = new File(appVariantContext.apContext.getApExploredFolder(), "mapping.txt");
                    } else {
                        mappingFile = new File(appVariantContext.getScope().getGlobalScope().getProject().getProjectDir(), "mapping.txt");
                    }
                    if (null != mappingFile && mappingFile.exists()) {
                        proGuardTransform.applyTestedMapping(mappingFile);
                    }
                }
            });
        }
    }
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) VariantScope(com.android.build.gradle.internal.scope.VariantScope) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) Task(org.gradle.api.Task) AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) BaseVariantOutputData(com.android.build.gradle.internal.variant.BaseVariantOutputData) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) IOException(java.io.IOException) ProGuardTransform(com.android.build.gradle.internal.transforms.ProGuardTransform) StopExecutionException(org.gradle.api.tasks.StopExecutionException) AwbProguardConfiguration(com.taobao.android.builder.tasks.app.bundle.AwbProguardConfiguration) GradleException(org.gradle.api.GradleException) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) Supplier(java.util.function.Supplier) File(java.io.File)

Example 12 with VariantScope

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

the class AtlasProguardHelper method doBundleProguard.

public static void doBundleProguard(final AppVariantContext appVariantContext, List<File> mainDexJars) throws Exception {
    Profiler.enter("preparebundleproguard");
    VariantScope variantScope = appVariantContext.getScope();
    AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName());
    if (dependencyTree.getAwbBundles().isEmpty()) {
        return;
    }
    List<File> libs = new ArrayList<>(appVariantContext.getScope().getGlobalScope().getAndroidBuilder().getBootClasspath(true));
    // All dependent classes
    Profiler.enter("getDefaultClasses");
    Set<String> defaultLibClasses = getClassList(libs);
    Profiler.release();
    libs.addAll(mainDexJars);
    // Get the basic proguard configuration
    List<File> defaultProguardFiles = new ArrayList<>(appVariantContext.getVariantConfiguration().getBuildType().getProguardFiles());
    Collections.sort(defaultProguardFiles);
    BaseVariantOutput vod = (BaseVariantOutput) appVariantContext.getVariantOutputData().iterator().next();
    AppVariantOutputContext appVariantOutputContext = appVariantContext.getAppVariantOutputContext(ApkDataUtils.get(vod));
    int parallelCount = appVariantContext.getAtlasExtension().getTBuildConfig().getProguardParallelCount();
    // //FIXME
    // parallelCount = 1;
    Map<BundleInfo, AwbTransform> bundleInfoAwbTransformMap = new HashMap<>();
    for (AwbTransform awbTransform : appVariantOutputContext.getAwbTransformMap().values()) {
        bundleInfoAwbTransformMap.put(awbTransform.getAwbBundle().bundleInfo, awbTransform);
    }
    Profiler.release();
    BundleGraphExecutor.execute(new ArrayList<>(bundleInfoAwbTransformMap.keySet()), parallelCount, new BundleItemRunner() {

        @Override
        public void execute(BundleItem bundleItem) {
            try {
                Input input = new Input();
                AwbTransform awbTransform = bundleInfoAwbTransformMap.get(bundleItem.bundleInfo);
                input.getAwbBundles().add(awbTransform);
                for (BundleInfo sub : bundleItem.circles) {
                    input.getAwbBundles().add(bundleInfoAwbTransformMap.get(sub));
                }
                input.getDefaultProguardFiles().addAll(defaultProguardFiles);
                input.getLibraries().addAll(libs);
                input.getDefaultLibraryClasses().addAll(defaultLibClasses);
                input.printConfiguration = new File(appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle()), "proguard.cfg");
                input.printUsage = new File(appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle()), "usage.cfg");
                addLibraryProguardFiles(appVariantContext, input);
                addChildDependency(bundleItem, input, bundleInfoAwbTransformMap);
                addParentKeeps(bundleItem, input, awbTransform, bundleInfoAwbTransformMap, appVariantContext);
                BundleProguarder.execute(appVariantContext, input);
            } catch (Exception e) {
                e.printStackTrace();
                throw new GradleException("proguard " + bundleItem.bundleInfo.getPkgName(), e);
            }
        }
    });
}
Also used : VariantScope(com.android.build.gradle.internal.scope.VariantScope) BundleItemRunner(com.taobao.android.builder.tools.bundleinfo.BundleItemRunner) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) Input(com.taobao.android.builder.tools.proguard.domain.Input) BundleInfo(com.taobao.android.builder.tools.bundleinfo.model.BundleInfo) BundleItem(com.taobao.android.builder.tools.bundleinfo.BundleItem) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) GradleException(org.gradle.api.GradleException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 13 with VariantScope

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

the class PostProcessManifestAction method execute.

@Override
public void execute(Task task) {
    AtlasExtension atlasExtension = appVariantContext.getAtlasExtension();
    File bundleBaseLineInfo = appVariantContext.getBundleBaseInfoFile();
    VariantScope variantScope = appVariantContext.getScope();
    GradleVariantConfiguration config = variantScope.getVariantConfiguration();
    AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(config.getFullName());
    File androidManifest = null;
    File file = variantScope.getInstantRunManifestOutputDirectory();
    if (null != file && file.exists() && variantScope.getInstantRunBuildContext().isInInstantRunMode()) {
        androidManifest = FileUtils.join(baseVariantOutputData.getProcessManifest().getInstantRunManifestOutputDirectory(), baseVariantOutputData.getDirName(), SdkConstants.ANDROID_MANIFEST_XML);
    } else {
        androidManifest = FileUtils.join(baseVariantOutputData.getProcessManifest().getManifestOutputDirectory(), baseVariantOutputData.getDirName(), SdkConstants.ANDROID_MANIFEST_XML);
    }
    try {
        Result result = ManifestFileUtils.postProcessManifests(androidManifest, getLibManifestMap(), getLibManifestDepenendyMap(), bundleBaseLineInfo, atlasExtension.manifestOptions, isMultiDexEnabled(), variantScope.getInstantRunBuildContext().isInInstantRunMode(), appVariantContext.getBuildType().isDebuggable(), atlasExtension.getTBuildConfig().getOutOfApkBundles(), atlasExtension.getTBuildConfig().getInsideOfApkBundles(), atlasExtension.getTBuildConfig().isPushInstall());
        File proxySrcDir = appVariantContext.getAtlasProxySourceDir();
        if (AtlasProxy.genProxyJavaSource(proxySrcDir, result)) {
        // appVariantContext.getVariantData().javacTask.source(proxySrcDir);
        }
        ManifestHelper.checkManifest(appVariantContext, androidManifest, dependencyTree, atlasExtension);
    } catch (DocumentException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
// ManifestFileUtils.postProcessManifests(
// instantRunAndroidManifest,
// getLibManifestMap(),
// getLibManifestDepenendyMap(),
// bundleBaseLineInfo,
// atlasExtension.manifestOptions,
// isMultiDexEnabled(),
// true,
// appVariantContext.getBuildType().isDebuggable(),
// atlasExtension.getTBuildConfig()
// .getOutOfApkBundles(),atlasExtension.getTBuildConfig().getInsideOfApkBundles(),atlasExtension.getTBuildConfig().isPushInstall());
// }
// manifest list check
// TODO??
// AtlasBuildContext.androidBuilderMap.get(appVariantContext.getProject()).generateKeepList(
// baseVariantOutputData.manifestProcessorTask.getManifestOutputFile(),
// appVariantContext.getScope()
// .getManifestKeepListProguardFile());
}
Also used : AtlasExtension(com.taobao.android.builder.extension.AtlasExtension) VariantScope(com.android.build.gradle.internal.scope.VariantScope) DocumentException(org.dom4j.DocumentException) GradleVariantConfiguration(com.android.build.gradle.internal.core.GradleVariantConfiguration) IOException(java.io.IOException) File(java.io.File) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) Result(com.taobao.android.builder.tools.manifest.Result)

Example 14 with VariantScope

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

the class PreProcessManifestAction method execute.

@Override
public void execute(Task task) {
    AtlasExtension atlasExtension = appVariantContext.getAtlasExtension();
    ManifestProcessorTask manifestProcessorTask = baseVariantOutput.getProcessManifest();
    Set<String> notMergedArtifacts = Sets.newHashSet();
    if (null != atlasExtension.getManifestOptions() && null != atlasExtension.getManifestOptions().getNotMergedBundles()) {
        notMergedArtifacts = atlasExtension.getManifestOptions().getNotMergedBundles();
    }
    if (manifestProcessorTask instanceof MergeManifests) {
        MergeManifests mergeManifests = (MergeManifests) manifestProcessorTask;
        VariantScope variantScope = appVariantContext.getScope();
        GradleVariantConfiguration config = variantScope.getVariantConfiguration();
        AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(config.getFullName());
        List<ManifestProvider> bundleProviders = ManifestHelper.getBundleManifest(appVariantContext, dependencyTree, atlasExtension);
        List<ManifestProvider> mainDexProviders = ManifestHelper.getMainDexManifest(appVariantContext, dependencyTree, atlasExtension);
        List<ManifestProvider> allManifest = new ArrayList<>();
        modifyForIncremental(mergeManifests, allManifest);
        // allManifest.addAll(ManifestHelper.convert(mergeManifests.getProviders(), appVariantContext));
        allManifest.addAll(bundleProviders);
        allManifest.addAll(mainDexProviders);
        AtlasBuildContext.androidBuilderMap.get(appVariantContext.getProject()).manifestProviders = allManifest;
        mergeManifests.setAndroidBuilder(AtlasBuildContext.androidBuilderMap.get(appVariantContext.getProject()));
    // if (sLogger.isInfoEnabled()) {
    // for (ManifestProvider manifestProvider : allManifest) {
    // sLogger.warn("[manifestLibs] " + manifestProvider.getManifest().getAbsolutePath());
    // }
    // }
    // Without this step, each time getLibraries It's going to be recomputed from the mapping
    // mergeManifests.(allManifest);
    }
}
Also used : AtlasExtension(com.taobao.android.builder.extension.AtlasExtension) VariantScope(com.android.build.gradle.internal.scope.VariantScope) MergeManifests(com.android.build.gradle.tasks.MergeManifests) ArrayList(java.util.ArrayList) GradleVariantConfiguration(com.android.build.gradle.internal.core.GradleVariantConfiguration) ManifestProcessorTask(com.android.build.gradle.tasks.ManifestProcessorTask) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) ManifestProvider(com.android.manifmerger.ManifestProvider)

Example 15 with VariantScope

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

the class ProcessResAwbsTask method run.

@TaskAction
void run() throws ExecutionException, InterruptedException {
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    if (null == atlasDependencyTree) {
        return;
    }
    final VariantScope outputScope = appVariantOutputContext.getScope();
    ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper(taskName, getLogger(), 0);
    List<Runnable> runnables = new ArrayList<>();
    for (final AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        if (awbBundle.isMBundle) {
            continue;
        }
        runnables.add(new Runnable() {

            @Override
            public void run() {
                File symbolLocation = new File(outputScope.getGlobalScope().getIntermediatesDir(), "awb-symbols/" + outputScope.getVariantConfiguration().getDirName() + "/" + awbBundle.getName());
                // Write the resources to the ap for debug
                if ("debug".equals(appVariantOutputContext.getVariantContext().getBaseVariantData().getName())) {
                    appVariantOutputContext.appBuildInfo.getOtherFilesMap().put("awo/" + awbBundle.getPackageName() + ".R.txt", new File(symbolLocation, "R.txt"));
                }
                ProcessAwbAndroidResources.ConfigAction configAction = new ProcessAwbAndroidResources.ConfigAction(outputScope, symbolLocation, true, awbBundle, getBuilder(), appVariantOutputContext, baseVariantOutput);
                ProcessAwbAndroidResources processAwbAndroidResources = TaskCreater.create(getProject(), configAction.getName(), configAction.getType());
                processAwbAndroidResources.mainDexSymbolFileCollection = mainDexSymbolFileCollection;
                appVariantContext.awbsProcessResourcesTask.put(awbBundle.getName(), processAwbAndroidResources);
                configAction.execute(processAwbAndroidResources);
                try {
                    processAwbAndroidResources.doFullTaskAction();
                } catch (IOException e) {
                    throw new GradleException("process awb res exception", e);
                }
            }
        });
    }
    executorServicesHelper.execute(runnables);
}
Also used : VariantScope(com.android.build.gradle.internal.scope.VariantScope) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) ExecutorServicesHelper(com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper) GradleException(org.gradle.api.GradleException) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) File(java.io.File) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

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