Search in sources :

Example 11 with AwbTransform

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

the class AtlasTransformUtils method getTransformInputs.

public static Collection<File> getTransformInputs(AppVariantContext appVariantContext, TransformInvocation invocation) {
    ImmutableSet.Builder<File> builder = ImmutableSet.builder();
    Collection<File> transformInputs = TransformInputUtil.getAllFiles(invocation.getInputs());
    builder.addAll(transformInputs);
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(appVariantContext.getVariantConfiguration().getFullName());
    if (atlasDependencyTree.getAwbBundles().size() > 0) {
        BaseVariantOutput vod = (BaseVariantOutput) appVariantContext.getVariantOutputData().iterator().next();
        AppVariantOutputContext appVariantOutputContext = appVariantContext.getAppVariantOutputContext(ApkDataUtils.get(vod));
        Collection<AwbTransform> awbTransforms = appVariantOutputContext.getAwbTransformMap().values();
        awbTransforms.forEach(awbTransform -> {
            builder.addAll(awbTransform.getInputLibraries());
        });
    }
    return builder.build();
}
Also used : BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) ImmutableSet(com.google.common.collect.ImmutableSet) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) File(java.io.File) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree)

Example 12 with AwbTransform

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

the class BundleProguarder method printInOut.

private static File printInOut(AppVariantContext appVariantContext, Input input) throws IOException {
    File proguardDir = input.proguardOutputDir;
    if (null == proguardDir) {
        proguardDir = appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle());
    }
    proguardDir.mkdirs();
    File inoutConfigs = new File(proguardDir, INOUT_CFG);
    List<String> configs = new ArrayList<>();
    for (AwbTransform awbTransform : input.getAwbBundles()) {
        List<File> inputLibraries = Lists.newArrayList();
        for (File inputLibrary : awbTransform.getInputLibraries()) {
            configs.add(INJARS_OPTION + " " + inputLibrary.getAbsolutePath());
            String name = FileNameUtils.getUniqueJarName(inputLibrary);
            File obsJar = new File(proguardDir, name + ".jar");
            input.proguardJarMap.put(obsJar, input.getFileMd5s().get(inputLibrary));
            inputLibraries.add(obsJar);
            configs.add(OUTJARS_OPTION + " " + obsJar.getAbsolutePath());
            if (inputLibrary.isDirectory() && AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).getInputDirs().contains(inputLibrary)) {
                input.maindexFolderTransform.put(inputLibrary, obsJar);
            } else if (AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).inMainDex(inputLibrary)) {
                input.maindexFileTransform.put(inputLibrary, obsJar);
            // AtlasBuildContext.atlasMainDexHelper.updateMainDexFile(inputLibrary, obsJar);
            } else if (awbTransform.getAwbBundle().isMainBundle()) {
                AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).addMainDex(new BuildAtlasEnvTask.FileIdentity(obsJar.getName(), obsJar, false, false));
            }
        }
        // configs.add();
        if (null != awbTransform.getInputDirs() && awbTransform.getInputDirs().size() > 0) {
            for (File dir : awbTransform.getInputDirs()) {
                if (!dir.exists()) {
                    continue;
                }
                configs.add(INJARS_OPTION + " " + dir.getAbsolutePath());
                String name = input.getFileMd5s().get(dir);
                if (null == name) {
                    name = FileNameUtils.getUniqueFileName(dir.getName(), "proguard");
                }
                File obsJar = new File(proguardDir, name + ".jar");
                inputLibraries.add(obsJar);
                configs.add(OUTJARS_OPTION + " " + obsJar.getAbsolutePath());
                if (awbTransform.getAwbBundle().isMainBundle()) {
                    AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).getInputDirs().clear();
                    AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).addMainDex(new BuildAtlasEnvTask.FileIdentity(obsJar.getName(), obsJar, false, false));
                }
            }
        }
        awbTransform.setInputFiles(inputLibraries);
        awbTransform.getInputDirs().clear();
        awbTransform.getInputLibraries().clear();
    }
    for (File library : input.getLibraries()) {
        configs.add("-libraryjars " + library.getAbsolutePath());
    }
    FileUtils.writeLines(inoutConfigs, configs);
    return inoutConfigs;
}
Also used : AwbTransform(com.android.build.gradle.internal.api.AwbTransform) ArrayList(java.util.ArrayList) File(java.io.File) BuildAtlasEnvTask(com.taobao.android.builder.tasks.app.BuildAtlasEnvTask)

Example 13 with AwbTransform

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

the class BundleProguarder method loadProguardFromCache.

public static Result loadProguardFromCache(AppVariantContext appVariantContext, Input input) throws Exception {
    Result result = new Result();
    String md5 = input.getMd5();
    result.key = input.getAwbBundles().get(0).getAwbBundle().getName() + "_" + md5;
    logger.info("bundle proguard for " + result.key + " with md5 key " + md5);
    File cacheDir = FileCacheCenter.queryFile(CACHE_TYPE, result.key, true, isRemoteCacheEnabled(appVariantContext));
    result.cacheDir = cacheDir;
    if (null == cacheDir || !cacheDir.exists()) {
        logger.warn("bundle proguard for " + result.key + " miss  cache " + cacheDir.getAbsolutePath());
        return result;
    }
    logger.warn("bundle proguard for " + result.key + " hit  cache ");
    File keepJson = new File(cacheDir, "keep.json");
    File proguardCfg = new File(cacheDir, "proguard.cfg");
    File usageCfg = new File(cacheDir, "usage.cfg");
    Map<String, File> md5Map = input.getMd5Files();
    if (input.getAwbBundles().get(0).getAwbBundle().isMainBundle()) {
        AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).getMainDexFiles().clear();
        AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).getInputDirs().clear();
        for (File file : cacheDir.listFiles()) {
            if (file.getName().endsWith("jar") && ZipUtils.isZipFile(file)) {
                String jarMd5 = file.getName().replace(".jar", "");
                File srcFile = md5Map.get(jarMd5);
                if (null != srcFile && srcFile.exists()) {
                    String fileName = FileNameUtils.getUniqueJarName(srcFile);
                    FileUtils.copyFile(file, new File(input.proguardOutputDir, fileName + ".jar"));
                    AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).addMainDex(new BuildAtlasEnvTask.FileIdentity(fileName, new File(input.proguardOutputDir, fileName + ".jar"), false, false));
                } else {
                    FileUtils.copyFileToDirectory(file, input.proguardOutputDir);
                    AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).addMainDex(new BuildAtlasEnvTask.FileIdentity(file.getName(), new File(input.proguardOutputDir, file.getName()), false, false));
                }
            }
        }
        File awbProguardDir = input.printConfiguration.getParentFile();
        // FileUtils.copyFileToDirectory(keepJson, awbProguardDir);
        if (proguardCfg.exists()) {
            FileUtils.copyFileToDirectory(proguardCfg, awbProguardDir);
        }
        if (usageCfg.exists()) {
            FileUtils.copyFileToDirectory(usageCfg, awbProguardDir);
        }
        result.success = true;
        return result;
    } else {
        if (!keepJson.exists()) {
            logger.error("bundle proguard for " + result.key + " missing keep.json  ");
            FileUtils.deleteDirectory(cacheDir);
            return result;
        }
        Map<AwbTransform, List<File>> transformListMap = new HashMap<>();
        for (AwbTransform awbTransform : input.getAwbBundles()) {
            File awbProguardDir = appVariantContext.getAwbProguardDir(awbTransform.getAwbBundle());
            FileUtils.copyFileToDirectory(keepJson, awbProguardDir);
            if (proguardCfg.exists()) {
                FileUtils.copyFileToDirectory(proguardCfg, awbProguardDir);
            }
            if (usageCfg.exists()) {
                FileUtils.copyFileToDirectory(usageCfg, awbProguardDir);
            }
            List<File> inputFiles = new ArrayList<>();
            transformListMap.put(awbTransform, inputFiles);
            List<File> files = new ArrayList<>();
            files.addAll(awbTransform.getInputLibraries());
            // configs.add();
            if (null != awbTransform.getInputDirs() && awbTransform.getInputDirs().size() > 0) {
                files.addAll(awbTransform.getInputDirs());
            }
            for (File oldFile : files) {
                File file = new File(cacheDir, input.getFileMd5s().get(oldFile) + ".jar");
                if (file.exists()) {
                    if (ZipUtils.isZipFile(file)) {
                        inputFiles.add(file);
                    }
                } else {
                    logger.error("miss proguard jar for :" + file.getAbsolutePath());
                    FileUtils.deleteDirectory(cacheDir);
                    logger.error("delete cache Dir " + cacheDir.getAbsolutePath());
                    return result;
                }
            }
        }
        for (AwbTransform awbTransform : input.getAwbBundles()) {
            awbTransform.setInputFiles(transformListMap.get(awbTransform));
            awbTransform.getInputDirs().clear();
            awbTransform.getInputLibraries().clear();
            awbTransform.getAwbBundle().setKeepProguardFile(keepJson);
        }
        result.success = true;
        return result;
    }
}
Also used : HashMap(java.util.HashMap) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Result(com.taobao.android.builder.tools.proguard.domain.Result) BuildAtlasEnvTask(com.taobao.android.builder.tasks.app.BuildAtlasEnvTask)

Example 14 with AwbTransform

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

the class Input method getMd5.

public String getMd5() throws Exception {
    if (StringUtils.isNotEmpty(md5)) {
        return md5;
    }
    List<File> files = new ArrayList<>();
    files.addAll(getParentKeeps());
    files.addAll(getDefaultProguardFiles());
    files.addAll(getLibraryProguardFiles());
    for (AwbTransform awbTransform : this.getAwbBundles()) {
        for (File file : awbTransform.getInputLibraries()) {
            files.add(file);
        }
        // configs.add();
        if (null != awbTransform.getInputDirs() && awbTransform.getInputDirs().size() > 0) {
            files.addAll(awbTransform.getInputDirs());
        }
    }
    for (File file : files) {
        if (file.isFile()) {
            String md5 = MD5Util.getFileMD5(file);
            fileMd5s.put(file, md5);
        } else {
            String md5 = MD5Util.getFileMd5(FileUtils.listFiles(file, new String[] { "class" }, true));
            fileMd5s.put(file, md5);
        }
    }
    List<String> mds = new ArrayList<>(fileMd5s.values());
    Collections.sort(mds);
    md5 = MD5Util.getMD5(StringUtils.join(mds.toArray(new String[0])));
    return md5;
}
Also used : AwbTransform(com.android.build.gradle.internal.api.AwbTransform) ArrayList(java.util.ArrayList) File(java.io.File)

Example 15 with AwbTransform

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

Aggregations

AwbTransform (com.android.build.gradle.internal.api.AwbTransform)23 File (java.io.File)16 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)12 JarFile (java.util.jar.JarFile)8 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)7 ArrayList (java.util.ArrayList)6 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)5 WaitableExecutor (com.android.ide.common.internal.WaitableExecutor)4 GradleException (org.gradle.api.GradleException)4 Nullable (com.android.annotations.Nullable)3 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)3 AppVariantContext (com.android.build.gradle.internal.api.AppVariantContext)3 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)3 IOException (java.io.IOException)3 TaskAction (org.gradle.api.tasks.TaskAction)3 NonNull (com.android.annotations.NonNull)2 AndroidLibrary (com.android.builder.model.AndroidLibrary)2 AtlasBuildContext (com.taobao.android.builder.AtlasBuildContext)2 BuildAtlasEnvTask (com.taobao.android.builder.tasks.app.BuildAtlasEnvTask)2 BundleItem (com.taobao.android.builder.tools.bundleinfo.BundleItem)2