Search in sources :

Example 21 with AtlasDependencyTree

use of com.taobao.android.builder.dependency.AtlasDependencyTree in project atlas by alibaba.

the class DiffBundleInfoTask method getArtifactBundleInfo.

private Set<ArtifactBundleInfo> getArtifactBundleInfo(DependencyDiff dependencyDiff, File mainfestFile, File apDir) throws IOException, DocumentException {
    Set<ArtifactBundleInfo> artifactBundleInfos = new HashSet<ArtifactBundleInfo>();
    if (null == apDir) {
        throw new GradleException("No Ap dependency found!");
    }
    File apManifest = new File(apDir, "AndroidManifest.xml");
    String apVersion = null;
    try {
        apVersion = ManifestFileUtils.getVersionName(apManifest);
    } catch (Exception e) {
        throw new GradleException(e.getMessage(), e);
    }
    Map<String, String> tagMap = new HashMap<>();
    JSONObject jsonObject = (JSONObject) JSON.parse(FileUtils.readFileToString(new File(apDir, "atlasFrameworkProperties.json")));
    JSONArray jsonArray = jsonObject.getJSONArray("bundleInfo");
    for (JSONObject obj : jsonArray.toArray(new JSONObject[0])) {
        tagMap.put(obj.getString("pkgName"), obj.getString("unique_tag"));
    }
    // 2. Add your own bundle
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(appVariantOutputContext.getVariantContext().getVariantConfiguration().getFullName());
    List<AwbBundle> modifyMbundles = new ArrayList<>();
    for (AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        BundleInfo bundleInfo = awbBundle.bundleInfo;
        ArtifactBundleInfo awbBundleInfo = new ArtifactBundleInfo();
        awbBundleInfo.setMainBundle(false);
        awbBundleInfo.setDependency(newDependency(bundleInfo.getDependency(), appVariantOutputContext.getVariantContext()));
        awbBundleInfo.setPkgName(bundleInfo.getPkgName());
        awbBundleInfo.setApplicationName(bundleInfo.getApplicationName());
        awbBundleInfo.setArtifactId(awbBundle.getResolvedCoordinates().getArtifactId());
        awbBundleInfo.setName(bundleInfo.getName());
        // History bundle's tag todo
        awbBundleInfo.setSrcUnitTag(tagMap.get(bundleInfo.getPkgName()));
        awbBundleInfo.setUnitTag(bundleInfo.getUnique_tag());
        String version = bundleInfo.getVersion();
        if (version.indexOf("@") > 0) {
            String[] arr = version.split("@");
            version = arr[arr.length - 1];
        }
        awbBundleInfo.setVersion(version);
        // TODO
        bundleInfo.getUnique_tag();
        String libBundleName = getBundleName(awbBundle);
        if (dependencyDiff.getAwbDiffs().contains(libBundleName)) {
            if (dependencyDiff.getNewAwbs().contains(libBundleName)) {
                awbBundleInfo.setDiffType(DiffType.ADD);
            } else {
                awbBundleInfo.setDiffType(DiffType.MODIFY);
            }
        } else {
            awbBundleInfo.setDiffType(DiffType.NONE);
        }
        if (awbBundle.isMBundle) {
            if (awbBundleInfo.getDiffType() == DiffType.ADD || awbBundleInfo.getDiffType() == DiffType.MODIFY) {
                modifyMbundles.add(awbBundle);
            }
            continue;
        }
        artifactBundleInfos.add(awbBundleInfo);
    }
    // 1. First add the main bundle
    ArtifactBundleInfo mainBundleInfo = getMainArtifactBundInfo(mainfestFile);
    mainBundleInfo.setBaseVersion(apVersion);
    mainBundleInfo.setMainBundle(true);
    mainBundleInfo.setVersion(appVariantOutputContext.getVariantContext().getVariantConfiguration().getVersionName());
    if (dependencyDiff.getMainDexDiffs().size() > 0 || modifyMbundles.size() > 0) {
        mainBundleInfo.setDiffType(DiffType.MODIFY);
    } else {
        mainBundleInfo.setDiffType(DiffType.NONE);
    }
    mainBundleInfo.setSrcUnitTag(jsonObject.getString("unit_tag"));
    mainBundleInfo.setUnitTag(appVariantOutputContext.getVariantContext().unit_tag);
    artifactBundleInfos.add(mainBundleInfo);
    return artifactBundleInfos;
}
Also used : ArtifactBundleInfo(com.taobao.android.object.ArtifactBundleInfo) JSONArray(com.alibaba.fastjson.JSONArray) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) BundleInfo(com.taobao.android.builder.tools.bundleinfo.model.BundleInfo) ArtifactBundleInfo(com.taobao.android.object.ArtifactBundleInfo) JSONObject(com.alibaba.fastjson.JSONObject) GradleException(org.gradle.api.GradleException) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) File(java.io.File)

Example 22 with AtlasDependencyTree

use of com.taobao.android.builder.dependency.AtlasDependencyTree 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 23 with AtlasDependencyTree

use of com.taobao.android.builder.dependency.AtlasDependencyTree in project atlas by alibaba.

the class AtlasProguardHelper method applyBundleKeeps.

public static void applyBundleKeeps(final AppVariantContext appVariantContext, ProGuardTransform proGuardTransform) {
    Set<File> proguardFiles = new HashSet<File>();
    GlobalScope globalScope = appVariantContext.getScope().getGlobalScope();
    // Add awb configuration
    AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(appVariantContext.getVariantConfiguration().getFullName());
    for (AwbBundle awbBundle : dependencyTree.getAwbBundles()) {
        if (null != awbBundle.getKeepProguardFile() && awbBundle.getKeepProguardFile().exists()) {
            proguardFiles.add(awbBundle.getKeepProguardFile());
        } else {
            appVariantContext.getProject().getLogger().error("missing " + awbBundle.getKeepProguardFile().getAbsolutePath());
        }
    }
    proGuardTransform.setConfigurationFiles(appVariantContext.getScope().getGlobalScope().getProject().files(proguardFiles));
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) JarFile(java.util.jar.JarFile) File(java.io.File) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree)

Example 24 with AtlasDependencyTree

use of com.taobao.android.builder.dependency.AtlasDependencyTree in project atlas by alibaba.

the class ApkFileListUtils method recordApkFileInfos.

/**
 * Records resource information for apk files
 */
public static ApkFiles recordApkFileInfos(AppVariantContext appVariantContext) {
    ApkFiles apkFiles = new ApkFiles();
    List<File> mainBundleResFolders = new ArrayList<File>();
    mainBundleResFolders.add(appVariantContext.getScope().getVariantData().mergeResourcesTask.getOutputDir());
    prepareApkFileList(appVariantContext.getScope().getVariantData().mergeAssetsTask.getOutputDir(), "assets", apkFiles);
    for (File resFolder : mainBundleResFolders) {
        prepareApkFileList(resFolder, "res", apkFiles);
    }
    // Record the resource information for each bundle
    AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(appVariantContext.getScope().getVariantConfiguration().getFullName());
    if (null == dependencyTree) {
        throw new StopExecutionException("DependencyTree cannot be null!");
    }
    List<AwbBundle> libs = dependencyTree.getAwbBundles();
    for (AwbBundle awbLib : libs) {
        File mergeAssertFolder = appVariantContext.getMergeAssets(awbLib);
        File mergeResFolder = appVariantContext.getMergeResources(awbLib);
        String awbName = awbLib.getName();
        prepareApkFileList(mergeAssertFolder, "assets", awbName, apkFiles);
        prepareApkFileList(mergeResFolder, "res", awbName, apkFiles);
    }
    return apkFiles;
}
Also used : ApkFiles(com.android.build.gradle.internal.api.ApkFiles) StopExecutionException(org.gradle.api.tasks.StopExecutionException) ArrayList(java.util.ArrayList) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) CompiledFile(android.aapt.pb.internal.ResourcesInternal.CompiledFile) File(java.io.File) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree)

Example 25 with AtlasDependencyTree

use of com.taobao.android.builder.dependency.AtlasDependencyTree in project atlas by alibaba.

the class BundleInfoUtils method setupAwbBundleInfos.

public static void setupAwbBundleInfos(AppVariantContext appVariantContext) throws IOException, DocumentException {
    String apkVersion = appVariantContext.getVariantConfiguration().getVersionName();
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(appVariantContext.getScope().getVariantConfiguration().getFullName());
    /**
     * name Is artifictId
     */
    Map<String, BundleInfo> bundleInfoMap = getBundleInfoMap(appVariantContext);
    String baseVersion = apkVersion;
    if (null != appVariantContext.apContext && null != appVariantContext.apContext.getBaseManifest() && appVariantContext.apContext.getBaseManifest().exists()) {
        baseVersion = ManifestFileUtils.getVersionName(appVariantContext.apContext.getBaseManifest());
    }
    for (AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        update(awbBundle, bundleInfoMap, appVariantContext, apkVersion, baseVersion);
        if (appVariantContext.getAtlasExtension().getTBuildConfig().getDisabledBundleDependency()) {
            awbBundle.bundleInfo.getDependency().clear();
        }
    }
}
Also used : BundleInfo(com.taobao.android.builder.tools.bundleinfo.model.BundleInfo) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree)

Aggregations

AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)38 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)26 File (java.io.File)21 ArrayList (java.util.ArrayList)16 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)13 GradleException (org.gradle.api.GradleException)13 TaskAction (org.gradle.api.tasks.TaskAction)12 IOException (java.io.IOException)11 ExecutorServicesHelper (com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper)8 AwbTransform (com.android.build.gradle.internal.api.AwbTransform)7 VariantScope (com.android.build.gradle.internal.scope.VariantScope)7 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)6 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)6 JarFile (java.util.jar.JarFile)6 BundleInfo (com.taobao.android.builder.tools.bundleinfo.model.BundleInfo)5 AppVariantContext (com.android.build.gradle.internal.api.AppVariantContext)4 DefaultTask (org.gradle.api.DefaultTask)4 TaskContainerAdaptor (com.android.build.gradle.internal.TaskContainerAdaptor)3 GlobalScope (com.android.build.gradle.internal.scope.GlobalScope)3 Task (org.gradle.api.Task)3