Search in sources :

Example 1 with BundleItem

use of com.taobao.android.builder.tools.bundleinfo.BundleItem in project atlas by alibaba.

the class AtlasProguardHelper method addParentKeeps.

private static void addParentKeeps(BundleItem bundleItem, Input input, AwbTransform awbTransform, Map<BundleInfo, AwbTransform> bundleInfoAwbTransformMap, AppVariantContext appVariantContext) throws IOException {
    Set<AwbBundle> parentBundles = new HashSet<>();
    for (BundleItem parent : bundleItem.parents) {
        parentBundles.add(bundleInfoAwbTransformMap.get(parent.bundleInfo).getAwbBundle());
        for (BundleInfo sub : parent.circles) {
            parentBundles.add(bundleInfoAwbTransformMap.get(sub).getAwbBundle());
        }
    }
    if (!parentBundles.isEmpty()) {
        List<AwbBundle> bundles = new ArrayList<>(parentBundles);
        Collections.sort(bundles, new Comparator<AwbBundle>() {

            @Override
            public int compare(AwbBundle o1, AwbBundle o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        File keepFile = generateKeepFile(bundles, appVariantContext.getAwbProguardDir(awbTransform.getAwbBundle()));
        input.getParentKeeps().add(keepFile);
    }
}
Also used : BundleInfo(com.taobao.android.builder.tools.bundleinfo.model.BundleInfo) BundleItem(com.taobao.android.builder.tools.bundleinfo.BundleItem) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with BundleItem

use of com.taobao.android.builder.tools.bundleinfo.BundleItem 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 3 with BundleItem

use of com.taobao.android.builder.tools.bundleinfo.BundleItem in project atlas by alibaba.

the class AtlasProguardHelper method addChildDependency.

private static void addChildDependency(BundleItem bundleItem, Input input, Map<BundleInfo, AwbTransform> bundleInfoAwbTransformMap) throws IOException {
    List<String> pkgNames = new ArrayList<>();
    input.getAwbBundles().forEach(awbTransform -> pkgNames.add(awbTransform.getAwbBundle().getPackageName()));
    sLogger.info("combine to proguard bundles:" + pkgNames.toString());
    List<AwbTransform> childTransforms = new ArrayList<>();
    sLogger.info("combine to proguard bundles:" + pkgNames.toString());
    for (BundleItem child : bundleItem.children) {
        if (!pkgNames.contains(child.bundleInfo.getPkgName())) {
            childTransforms.add(bundleInfoAwbTransformMap.get(child.bundleInfo));
        } else {
            sLogger.error(child.bundleInfo.getPkgName() + " in proguard bundles so discard from " + bundleItem.bundleInfo.getPkgName() + "libraries...");
        }
        for (BundleInfo sub : child.circles) {
            if (!pkgNames.contains(sub.getPkgName())) {
                childTransforms.add(bundleInfoAwbTransformMap.get(sub));
            } else {
                sLogger.error(sub.getPkgName() + " in proguard bundles so discard from " + bundleItem.bundleInfo.getPkgName() + " libraries...");
            }
        }
    }
    List<AwbBundle> childBundles = new ArrayList<>();
    for (AwbTransform child : childTransforms) {
        input.getLibraries().addAll(child.getInputLibraries());
        // join
        childBundles.add(child.getAwbBundle());
    }
}
Also used : 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) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle)

Aggregations

BundleItem (com.taobao.android.builder.tools.bundleinfo.BundleItem)3 BundleInfo (com.taobao.android.builder.tools.bundleinfo.model.BundleInfo)3 AwbTransform (com.android.build.gradle.internal.api.AwbTransform)2 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)2 File (java.io.File)2 JarFile (java.util.jar.JarFile)2 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)1 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)1 VariantScope (com.android.build.gradle.internal.scope.VariantScope)1 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)1 BundleItemRunner (com.taobao.android.builder.tools.bundleinfo.BundleItemRunner)1 Input (com.taobao.android.builder.tools.proguard.domain.Input)1 IOException (java.io.IOException)1 GradleException (org.gradle.api.GradleException)1