Search in sources :

Example 6 with BundleInfo

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

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

the class ManifestFileUtils method addApplicationMetaData.

/**
 * Add the metadata information to the manifest of the Lord as a solution for atals
 *
 * @param document
 * @param libManifestMap
 * @param baseBunfleInfoFile
 * @param manifestOptions
 */
private static void addApplicationMetaData(Document document, Map<String, File> libManifestMap, File baseBunfleInfoFile, ManifestOptions manifestOptions, Set<String> remoteBundles, Set<String> insideBundles) throws IOException, DocumentException {
    Map<String, BundleInfo> bundleFileMap = Maps.newHashMap();
    // Parsing basic information
    if (null != baseBunfleInfoFile && baseBunfleInfoFile.exists() && baseBunfleInfoFile.canRead()) {
        String bundleBaseInfo = FileUtils.readFileToString(baseBunfleInfoFile, "utf-8");
        bundleFileMap = JSON.parseObject(bundleBaseInfo, new TypeReference<Map<String, BundleInfo>>() {
        });
    }
    Map<String, LibBundleInfo> awbManifestMap = Maps.newHashMap();
    for (Map.Entry<String, File> entry : libManifestMap.entrySet()) {
        String artifactId = entry.getKey();
        String libName = artifactId.substring(artifactId.indexOf("-") + 1);
        File libManifest = entry.getValue();
        if (libManifest.exists()) {
            SAXReader reader = new SAXReader();
            // Read the XML file
            Document libDocument = reader.read(libManifest);
            // Get the root node
            Element libRoot = libDocument.getRootElement();
            String packageName = libRoot.attributeValue("package");
            Element applicationElement = libRoot.element("application");
            String applicationName = null;
            if (null != applicationElement) {
                applicationName = applicationElement.attributeValue("name");
            }
            LibBundleInfo libBundleInfo = new LibBundleInfo(artifactId, packageName, applicationName, bundleFileMap.get(libName), libName);
            awbManifestMap.put(artifactId, libBundleInfo);
        }
    }
    // Write meta-data information
    // Get the root node
    Element root = document.getRootElement();
    List<? extends Node> nodes = root.selectNodes("//application");
    for (Node node : nodes) {
        Element element = (Element) node;
        for (String artifactId : libManifestMap.keySet()) {
            LibBundleInfo libBundleInfo = awbManifestMap.get(artifactId);
            if (StringUtils.isNotBlank(libBundleInfo.applicationName)) {
                String bundlePackageName = libBundleInfo.packageName;
                BundleInfo bundleInfo = libBundleInfo.bundleInfo;
                Element metaData = element.addElement("meta-data");
                String bundleDepValue = "";
                if (null != bundleInfo && bundleInfo.getDependency() != null) {
                    bundleDepValue = StringUtils.join(bundleInfo.getDependency(), "|");
                }
                String value = libBundleInfo.applicationName + "," + !(remoteBundles.contains(libBundleInfo.libName) || insideBundles.contains(libBundleInfo.libName)) + "," + bundleDepValue;
                logger.info("[bundleInfo] add bundle value : " + value + " to manifest");
                metaData.addAttribute("android:name", "bundle_" + bundlePackageName);
                metaData.addAttribute("android:value", value);
            }
        }
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Node(org.dom4j.Node) Document(org.dom4j.Document) BundleInfo(com.taobao.android.builder.tools.bundleinfo.model.BundleInfo) TypeReference(com.alibaba.fastjson.TypeReference) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 8 with BundleInfo

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

the class ManifestHelper method checkManifest.

public static boolean checkManifest(AppVariantContext appVariantContext, File fullManifest, AtlasDependencyTree dependencyTree, AtlasExtension atlasExtension) throws DocumentException {
    Set<String> notMergedArtifacts = getNotMergedBundles(atlasExtension);
    BundleInfo mainBundleInfo = new BundleInfo();
    collectBundleInfo(appVariantContext, mainBundleInfo, fullManifest, null);
    List<String> errors = new ArrayList<>();
    for (AwbBundle awbBundle : dependencyTree.getAwbBundles()) {
        String cord = String.format("%s:%s", awbBundle.getResolvedCoordinates().getGroupId(), awbBundle.getResolvedCoordinates().getArtifactId());
        if (null != notMergedArtifacts && notMergedArtifacts.contains(cord)) {
            continue;
        }
        for (String activity : awbBundle.bundleInfo.getActivities()) {
            if (StringUtils.isNotEmpty(activity) && !mainBundleInfo.getActivities().contains(activity)) {
                errors.add("miss activity:" + activity);
            }
        }
        for (String service : awbBundle.bundleInfo.getServices()) {
            if (StringUtils.isNotEmpty(service) && !mainBundleInfo.getServices().contains(service)) {
                errors.add("miss service:" + service);
            }
        }
        if (!atlasExtension.getManifestOptions().isRemoveProvider()) {
            for (String provider : awbBundle.bundleInfo.getContentProviders()) {
                if (StringUtils.isNotEmpty(provider) && !mainBundleInfo.getContentProviders().contains(provider)) {
                    errors.add("miss provider:" + provider);
                }
            }
        }
        for (String receiver : awbBundle.bundleInfo.getReceivers()) {
            if (StringUtils.isNotEmpty(receiver) && !mainBundleInfo.getReceivers().contains(receiver)) {
                errors.add("miss receiver:" + receiver);
            }
        }
    }
    if (errors.isEmpty()) {
        return true;
    }
    for (String err : errors) {
        sLogger.error(err);
    }
    throw new GradleException("manifest merge error :" + StringUtils.join(errors, ","));
}
Also used : BundleInfo(com.taobao.android.builder.tools.bundleinfo.model.BundleInfo) GradleException(org.gradle.api.GradleException) ArrayList(java.util.ArrayList) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle)

Example 9 with BundleInfo

use of com.taobao.android.builder.tools.bundleinfo.model.BundleInfo 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 10 with BundleInfo

use of com.taobao.android.builder.tools.bundleinfo.model.BundleInfo 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

BundleInfo (com.taobao.android.builder.tools.bundleinfo.model.BundleInfo)15 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)9 File (java.io.File)7 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)5 HashMap (java.util.HashMap)5 GradleException (org.gradle.api.GradleException)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 BundleItem (com.taobao.android.builder.tools.bundleinfo.BundleItem)3 TypeReference (com.alibaba.fastjson.TypeReference)2 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)2 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)2 AwbTransform (com.android.build.gradle.internal.api.AwbTransform)2 Map (java.util.Map)2 JarFile (java.util.jar.JarFile)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 AppVariantContext (com.android.build.gradle.internal.api.AppVariantContext)1 VariantScope (com.android.build.gradle.internal.scope.VariantScope)1 BaseTask (com.android.build.gradle.internal.tasks.BaseTask)1