Search in sources :

Example 6 with AtlasDependencyTree

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

the class PackageAwbsTask method createAwbPackages.

/**
 * Directory of so
 */
@TaskAction
void createAwbPackages() throws ExecutionException, InterruptedException, IOException {
    File awbApkOutputDir = appVariantContext.getAwbApkOutputDir();
    FileUtils.cleanOutputDir(awbApkOutputDir);
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    if (null == atlasDependencyTree) {
        return;
    }
    ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper(taskName, getLogger(), 0);
    List<Runnable> runnables = new ArrayList<>();
    final AtomicLong dexTotalTime = new AtomicLong(0);
    final AtomicLong packageTotalTime = new AtomicLong(0);
    final Map<String, Long[]> monitors = new HashMap<String, Long[]>();
    long startTime = System.currentTimeMillis();
    for (final AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        if (awbBundle.isMBundle) {
            continue;
        }
        runnables.add(() -> {
            try {
                long start = System.currentTimeMillis();
                // create dex
                File dexOutputFile = appVariantContext.getAwbDexOutput(awbBundle.getName());
                long endDex = System.currentTimeMillis();
                // PACKAGE APP:
                File resourceFile = null;
                if (appVariantContext.getScope().useResourceShrinker()) {
                    resourceFile = appVariantOutputContext.getAwbCompressResourcePackageOutputFile(awbBundle);
                } else {
                    resourceFile = appVariantOutputContext.getAwbAndroidResourcesMap().get(awbBundle.getName()).getPackageOutputFile();
                }
                if (!resourceFile.exists()) {
                    resourceFile = appVariantOutputContext.getAwbAndroidResourcesMap().get(awbBundle.getName()).getPackageOutputFile();
                }
                Set<File> dexFolders = new HashSet<File>();
                dexFolders.add(dexOutputFile);
                Set<File> jniFolders = Sets.newHashSet();
                if (appVariantOutputContext.getAwbJniFolder(awbBundle) != null && appVariantOutputContext.getAwbJniFolder(awbBundle).exists()) {
                    jniFolders.add(appVariantOutputContext.getAwbJniFolder(awbBundle));
                }
                Set<File> javaResourcesLocations = Sets.newHashSet();
                if (appVariantContext.getAtlasExtension().getTBuildConfig().isIncremental() && awbBundle.getAllLibraryAars().size() > 1) {
                    File baseAwb = appVariantOutputContext.getVariantContext().apContext.getBaseAwb(awbBundle.getAwbSoName());
                    if (baseAwb != null) {
                        ZipFile files = new ZipFile(baseAwb);
                        ZipEntry entry = files.getEntry("classes.dex");
                        if (entry == null) {
                            throw new DexException("Expected classes.dex in " + baseAwb);
                        }
                        File file = new File(dexOutputFile, "classes.dex");
                        com.android.dex.Dex[] dexes = new com.android.dex.Dex[2];
                        dexes[0] = new com.android.dex.Dex(file);
                        dexes[1] = new com.android.dex.Dex(files.getInputStream(entry));
                        com.android.dex.Dex merged = new DexMerger(dexes, CollisionPolicy.KEEP_FIRST, new DxContext()).merge();
                        merged.writeTo(file);
                        javaResourcesLocations.add(baseAwb);
                    }
                }
                if (appVariantContext.getAtlasExtension().getTBuildConfig().getMergeAwbJavaRes()) {
                    javaResourcesLocations.add(new File(appVariantOutputContext.getAwbJavaResFolder(awbBundle), "res.jar"));
                }
                AwbApkPackageTask awbApkPackageTask = new AwbApkPackageTask(getProject().files(resourceFile), appVariantContext, awbBundle, appVariantOutputContext, getProject().files(dexFolders), getProject().files(javaResourcesLocations), getProject().files(appVariantOutputContext.getVariantContext().getMergeAssets(awbBundle)), getProject().files(jniFolders), getProject().files(awbBundle.getMergedManifest()), getBuilder(), config.getMinSdkVersion().getApiLevel(), getName());
                File awbFile = awbApkPackageTask.doFullTaskAction();
                bundles.put(awbBundle, awbFile);
                // //                        //TODO 2.3
                // AtlasBuildContext.androidBuilderMap.get(getProject())
                // .oldPackageApk(resourceFile.getAbsolutePath(),
                // dexFolders,
                // javaResourcesLocations,
                // jniFolders,
                // null,
                // getAbiFilters(),
                // config.getBuildType().isJniDebuggable(),
                // null,
                // getOutputFile(awbBundle),
                // config.getMinSdkVersion().getApiLevel(),
                // new com.google.common.base.Predicate<String>() {
                // @Override
                // public boolean apply(@Nullable String s) {
                // return false;
                // }
                // });
                long endPackage = System.currentTimeMillis();
                dexTotalTime.addAndGet(endDex - start);
                packageTotalTime.addAndGet(endPackage - endDex);
                monitors.put(awbBundle.getName(), new Long[] { endDex - start, endPackage - endDex });
            } catch (Throwable e) {
                // e.printStackTrace();
                throw new GradleException("package " + awbBundle.getName() + " failed", e);
            }
        });
    }
    executorServicesHelper.execute(runnables);
    getLogger().info(">>>>> packageAwbs >>>>>>>>>>>>");
    FileLogger fileLogger = FileLogger.getInstance("packageAwbs");
    fileLogger.log("totalTime is " + (System.currentTimeMillis() - startTime));
    fileLogger.log("dexTotalTime is " + dexTotalTime);
    fileLogger.log("packageTotalTime is " + packageTotalTime);
    String format = "[packageawb]  bundle:%50s  dexTime: %10d  packageTime: %10d ";
    for (String bundle : monitors.keySet()) {
        Long[] value = monitors.get(bundle);
        fileLogger.log(String.format(format, bundle, value[0], value[1]));
    }
    getLogger().info(">>>>> packageAwbs >>>>>>>>>>>>");
    PackageApplication packageApplication = appVariantContext.getScope().getPackageApplicationTask().get(new TaskContainerAdaptor(appVariantContext.getScope().getGlobalScope().getProject().getTasks()));
    packageApplication.doFirst(new FirstApkAction(appVariantOutputContext));
    packageApplication.doLast(new LastApkAction());
}
Also used : DexException(com.android.dex.DexException) ZipEntry(java.util.zip.ZipEntry) DexMerger(com.android.dx.merge.DexMerger) TaskContainerAdaptor(com.android.build.gradle.internal.TaskContainerAdaptor) ExecutorServicesHelper(com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) LastApkAction(com.taobao.android.builder.tasks.app.bundle.actions.LastApkAction) DxContext(com.android.dx.command.dexer.DxContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) ZipFile(java.util.zip.ZipFile) FirstApkAction(com.taobao.android.builder.tasks.app.bundle.actions.FirstApkAction) GradleException(org.gradle.api.GradleException) AtomicLong(java.util.concurrent.atomic.AtomicLong) FileLogger(com.taobao.android.builder.tools.log.FileLogger) PackageApplication(com.android.build.gradle.tasks.PackageApplication) ZipFile(java.util.zip.ZipFile) File(java.io.File) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 7 with AtlasDependencyTree

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

the class MergeResAwbsConfigAction method execute.

@Override
public void execute(MtlParallelTask parallelTask) {
    super.execute(parallelTask);
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(parallelTask.getVariantName());
    if (null == atlasDependencyTree) {
        return;
    }
    List<DefaultTask> tasks = new ArrayList<DefaultTask>();
    for (final AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        if (awbBundle.isMBundle) {
            continue;
        }
        MergeAwbResource.MergeAwbResourceConfigAction mergeAwbResourceConfigAction = new MergeAwbResource.MergeAwbResourceConfigAction(appVariantContext, baseVariantOutput, awbBundle);
        final MergeAwbResource mergeTask = TaskCreater.create(appVariantContext.getProject(), mergeAwbResourceConfigAction.getName(), mergeAwbResourceConfigAction.getType());
        // mergeTask.getOutputs().cacheIf(new Spec<Task>() {
        // @Override
        // public boolean isSatisfiedBy(Task element) {
        // return false;
        // }
        // });
        mergeTask.setBlameLogFolder(appVariantContext.getAwbBlameLogFolder(awbBundle));
        mergeAwbResourceConfigAction.execute(mergeTask);
        tasks.add(mergeTask);
        mergeTask.doLast(new Action<Task>() {

            @Override
            public void execute(Task task) {
                File publicRes = new File(awbBundle.getAndroidLibrary().getResFolder(), "values/public.xml");
                if (publicRes.exists()) {
                    try {
                        FileUtils.copyFile(publicRes, new File(mergeTask.getOutputDir(), "values/public.xml"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    parallelTask.parallelTask = tasks;
    parallelTask.uniqueTaskName = getName();
    parallelTask.concurrent = true;
}
Also used : MtlParallelTask(com.taobao.android.builder.tasks.manager.MtlParallelTask) Task(org.gradle.api.Task) DefaultTask(org.gradle.api.DefaultTask) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) MergeAwbResource(com.taobao.android.builder.tasks.app.merge.bundle.MergeAwbResource) DefaultTask(org.gradle.api.DefaultTask) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) File(java.io.File)

Example 8 with AtlasDependencyTree

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

the class PreparePackageIdsTask method generate.

/**
 * packageid The range is 30 - 127
 */
@TaskAction
void generate() throws IOException {
    TBuildConfig tBuildConfig = appVariantContext.getAtlasExtension().getTBuildConfig();
    File packageIdFile = tBuildConfig.getPackageIdFile();
    File apPackageIdFile = appVariantContext.apContext.getPackageIdFile();
    boolean isAutoPackageId = tBuildConfig.isAutoPackageId();
    int minPackageId = tBuildConfig.getMinPackageId();
    Map<String, String> autoConfigMap = new HashMap<String, String>();
    if (null != apPackageIdFile && apPackageIdFile.exists()) {
        autoConfigMap.putAll(loadPackageIdProperties(apPackageIdFile));
    } else if (null != packageIdFile && packageIdFile.exists()) {
        autoConfigMap.putAll(loadPackageIdProperties(packageIdFile));
    }
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    for (AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        String key = awbBundle.getResolvedCoordinates().getGroupId() + ":" + awbBundle.getResolvedCoordinates().getArtifactId();
        if (autoConfigMap.containsKey(key)) {
            continue;
        }
        File customPackageIDFile = new File(awbBundle.getAndroidLibrary().getFolder(), "customPackageID.txt");
        String packageId = getCustomPackageId(customPackageIDFile);
        if (StringUtils.isNotEmpty(packageId)) {
            autoConfigMap.put(key, packageId);
        } else {
            autoConfigMap.put(key, "");
        }
    }
    if (isAutoPackageId && autoConfigMap.containsValue("")) {
        // Automatic allocation of autoConfig
        List<String> keys = new ArrayList<String>(autoConfigMap.keySet());
        Collections.sort(keys);
        for (String key : keys) {
            if ("".equals(autoConfigMap.get(key))) {
                for (int i = minPackageId; i < /*=*/
                127; i++) {
                    if (!autoConfigMap.values().contains(String.valueOf(i))) {
                        autoConfigMap.put(key, String.valueOf(i));
                        break;
                    }
                }
            }
        }
    }
    AtlasBuildContext.customPackageIdMaps = autoConfigMap;
    // wite package Id file
    File outPkgFile = new File(getProject().getBuildDir(), "outputs/packageIdFile.properties");
    writeProperties(autoConfigMap, outPkgFile);
    appBuildInfo.setPackageIdFile(outPkgFile);
    // check value
    if (autoConfigMap.containsValue("")) {
        getLogger().error(JSON.toJSONString(autoConfigMap, true));
        throw new GradleException("packageId Error configuration, please check");
    }
    if (autoConfigMap.size() != new HashSet(autoConfigMap.values()).size()) {
        // getLogger().error(JSON.toJSONString(autoConfigMap, true));
        Map<String, PackageIdItem> idItemMap = new HashMap<String, PackageIdItem>();
        for (String key : autoConfigMap.keySet()) {
            String customPackageId = autoConfigMap.get(key);
            PackageIdItem packageIdItem = idItemMap.get(customPackageId);
            if (null == packageIdItem) {
                String[] split = customPackageId.split("\\.");
                packageIdItem = new PackageIdItem();
                packageIdItem.packageId = Integer.valueOf(split[0]);
                if (split.length > 1) {
                    packageIdItem.typeIdOffset = Integer.valueOf(split[1]);
                }
                idItemMap.put(customPackageId, packageIdItem);
            }
            packageIdItem.bundles.add(key);
        }
        Collection<PackageIdItem> collection = idItemMap.values();
        List<PackageIdItem> packageList = new ArrayList<PackageIdItem>(collection);
        Collections.sort(packageList, new Comparator<PackageIdItem>() {

            @Override
            public int compare(PackageIdItem o1, PackageIdItem o2) {
                return o1.packageId - o2.packageId;
            }
        });
        getLogger().error(JSON.toJSONString(packageList, true));
        throw new GradleException("packageId Configuration error, duplicate, please check");
    }
    // check if packageID is not used
    Map<String, String> autoConfigMap2 = new HashMap<>(autoConfigMap);
    for (AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        String key = awbBundle.getResolvedCoordinates().getGroupId() + ":" + awbBundle.getResolvedCoordinates().getArtifactId();
        autoConfigMap2.remove(key);
    }
    if (autoConfigMap2.size() > 0) {
        File outPkgFile2 = new File(getProject().getBuildDir(), "outputs/warning-unusedPackageIdFile.properties");
        writeProperties(autoConfigMap2, outPkgFile2);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TBuildConfig(com.taobao.android.builder.extension.TBuildConfig) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) GradleException(org.gradle.api.GradleException) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) File(java.io.File) HashSet(java.util.HashSet) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 9 with AtlasDependencyTree

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

the class AwbGenerator method createAwbBundle.

public AwbBundle createAwbBundle(LibVariantContext libVariantContext) throws IOException {
    String variantName = libVariantContext.getVariantName();
    AtlasDependencyTree libDependencyTree = AtlasBuildContext.libDependencyTrees.get(variantName);
    // TODO 2.3
    if (null == libDependencyTree) {
        libDependencyTree = new AtlasDepTreeParser(libVariantContext.getProject(), new ExtraModelInfo(new ProjectOptions(libVariantContext.getProject()), null), new HashSet<>()).parseDependencyTree(libVariantContext.getVariantDependency());
        AtlasBuildContext.libDependencyTrees.put(variantName, libDependencyTree);
    }
    Project project = libVariantContext.getProject();
    String groupName = (String) project.getGroup();
    String name = "";
    String version = (String) project.getVersion();
    if (project.hasProperty("archivesBaseName")) {
        name = (String) project.getProperties().get("archivesBaseName");
    } else {
        name = project.getName();
    }
    File explodedDir = project.file(project.getBuildDir().getAbsolutePath() + "/" + FD_INTERMEDIATES + "/exploded-awb/" + computeArtifactPath(groupName, name, version));
    FileUtils.deleteDirectory(explodedDir);
    MavenCoordinates mavenCoordinates = new MavenCoordinatesImpl(groupName, name, version, "awb", "");
    ResolvedDependencyInfo resolvedDependencyInfo = new ResolvedDependencyInfo(groupName, name, version, "awb");
    resolvedDependencyInfo.setVariantName(libVariantContext.getVariantName());
    AwbBundle awbBundle = new AwbBundle(resolvedDependencyInfo, DependencyConvertUtils.toAndroidLibrary(mavenCoordinates, libVariantContext.getBundleTask().getArchivePath(), explodedDir));
    awbBundle.getSoLibraries().addAll(libDependencyTree.getMainBundle().getSoLibraries());
    awbBundle.getAndroidLibraries().addAll(libDependencyTree.getMainBundle().getAndroidLibraries());
    awbBundle.getJavaLibraries().addAll(libDependencyTree.getMainBundle().getJavaLibraries());
    return awbBundle;
}
Also used : Project(org.gradle.api.Project) MavenCoordinates(com.android.builder.model.MavenCoordinates) MavenCoordinatesImpl(com.android.builder.dependency.MavenCoordinatesImpl) ResolvedDependencyInfo(com.taobao.android.builder.dependency.parser.ResolvedDependencyInfo) AtlasDepTreeParser(com.taobao.android.builder.dependency.parser.AtlasDepTreeParser) ExtraModelInfo(com.android.build.gradle.internal.ExtraModelInfo) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) ProjectOptions(com.android.build.gradle.options.ProjectOptions) File(java.io.File)

Example 10 with AtlasDependencyTree

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

the class AwbDataBindingProcessLayoutTask method run.

@TaskAction
public void run() throws ExecutionException, InterruptedException {
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    if (null == atlasDependencyTree) {
        return;
    }
    DataBindingBuilder dataBindingBuilder = new DataBindingBuilder();
    dataBindingBuilder.setPrintMachineReadableOutput(false);
    dataBindingBuilder.setDebugLogEnabled(appVariantContext.getProject().getLogger().isDebugEnabled());
    List<Runnable> tasks = new ArrayList<Runnable>();
    for (final AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        if (!appVariantContext.getAtlasExtension().getTBuildConfig().getDataBindingBundles().contains(awbBundle.getPackageName()) || awbBundle.isMBundle) {
            continue;
        }
        tasks.add(() -> {
            AwbDataBindingProcessLayoutsTask.AwbDataBindingProcessLayoutsConfigAction processLayoutsConfigAction = new AwbDataBindingProcessLayoutsTask.AwbDataBindingProcessLayoutsConfigAction(appVariantContext, awbBundle, dataBindingBuilder);
            AwbDataBindingProcessLayoutsTask dataBindingProcessLayoutsTask = TaskCreater.create(appVariantContext.getProject(), processLayoutsConfigAction.getName(), processLayoutsConfigAction.getType());
            processLayoutsConfigAction.execute(dataBindingProcessLayoutsTask);
            dataBindingProcessLayoutsTask.execute();
        });
    }
    ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper("dataBindingProcessLayoutTask", getLogger(), 0);
    executorServicesHelper.execute(tasks);
}
Also used : ExecutorServicesHelper(com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper) ArrayList(java.util.ArrayList) DataBindingBuilder(android.databinding.tool.DataBindingBuilder) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) AwbDataBindingProcessLayoutsTask(com.taobao.android.builder.tasks.app.merge.bundle.AwbDataBindingProcessLayoutsTask) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

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