Search in sources :

Example 1 with ProcessOutput

use of com.android.ide.common.process.ProcessOutput in project atlas by alibaba.

the class AtlasDexArchiveBuilderTransform method convertAwbToDexArchive.

private List<File> convertAwbToDexArchive(@NonNull Context context, @NonNull QualifiedContent input, @NonNull File outputProvider, boolean isIncremental, boolean awb) throws Exception {
    int count = 0;
    if (input.getFile().isFile()) {
        count = computerClassCount(input.getFile());
    } else if (input.getFile().isDirectory()) {
        count = 1;
    }
    logger.verbose("Dexing {}", input.getFile().getAbsolutePath());
    ImmutableList.Builder<File> dexArchives = ImmutableList.builder();
    for (int bucketId = 0; bucketId < count; bucketId++) {
        File preDexOutputFile = getAwbPreDexFile(outputProvider, input, bucketId);
        if (input.getFile().isDirectory()) {
            File cachedVersion = cacheHandler.getCachedVersionIfPresent(input.getFile());
            dexArchives.add(preDexOutputFile);
            if (cachedVersion != null) {
                FileUtils.copyDirectoryContentToDirectory(cachedVersion, preDexOutputFile);
                return dexArchives.build();
            }
        }
        if (preDexOutputFile.isDirectory() && preDexOutputFile.exists()) {
            FileUtils.cleanOutputDir(preDexOutputFile);
        } else {
            FileUtils.deleteIfExists(preDexOutputFile);
        }
        AtlasDexArchiveBuilderTransform.DexConversionParameters parameters = new AtlasDexArchiveBuilderTransform.DexConversionParameters(input, preDexOutputFile, NUMBER_OF_BUCKETS, bucketId, minSdkVersion, dexOptions.getAdditionalParameters(), inBufferSize, outBufferSize, dexer, isDebuggable, false, awb);
        if (useGradleWorkers) {
            context.getWorkerExecutor().submit(DexArchiveBuilderTransform.DexConversionWorkAction.class, configuration -> {
                configuration.setIsolationMode(IsolationMode.NONE);
                configuration.setParams(parameters);
            });
        } else {
            executor.execute(() -> {
                ProcessOutputHandler outputHandler = new ParsingProcessOutputHandler(new ToolOutputParser(new DexParser(), Message.Kind.ERROR, logger), new ToolOutputParser(new DexParser(), logger), errorReporter);
                ProcessOutput output = null;
                try (Closeable ignored = output = outputHandler.createOutput()) {
                    launchProcessing(parameters, output.getStandardOutput(), output.getErrorOutput());
                } finally {
                    if (output != null) {
                        try {
                            outputHandler.handleOutput(output);
                        } catch (ProcessException e) {
                        // ignore this one
                        }
                    }
                }
                return null;
            });
        }
    }
    List<File> files = dexArchives.build();
    return files;
}
Also used : ParsingProcessOutputHandler(com.android.ide.common.blame.ParsingProcessOutputHandler) ProcessOutputHandler(com.android.ide.common.process.ProcessOutputHandler) DexArchiveBuilderTransform(com.android.build.gradle.internal.transforms.DexArchiveBuilderTransform) ProcessException(com.android.ide.common.process.ProcessException) ToolOutputParser(com.android.ide.common.blame.parser.ToolOutputParser) DexParser(com.android.ide.common.blame.parser.DexParser) ProcessOutput(com.android.ide.common.process.ProcessOutput) JarFile(java.util.jar.JarFile) ParsingProcessOutputHandler(com.android.ide.common.blame.ParsingProcessOutputHandler)

Example 2 with ProcessOutput

use of com.android.ide.common.process.ProcessOutput in project atlas by alibaba.

the class AtlasMainDexMerger method merge.

public void merge(List<File> mainDexFiles, File outputDir, File[][] mergeDexs) {
    sort(mainDexFiles);
    FileCache.Inputs buildCacheInputs = getBuildCacheInputs(mainDexFiles, dexingType, dexMerger, mainDexListFile == null ? null : mainDexListFile.getSingleFile(), minSdkVersion, isDebuggable, "maindex", ID);
    ProcessOutput output = outputHandler.createOutput();
    FileCache fileCache = BuildCacheUtils.createBuildCacheIfEnabled(variantOutputContext.getVariantContext().getProject(), variantOutputContext.getScope().getGlobalScope().getProjectOptions());
    if (fileCache == null) {
        try {
            fileCache = FileCache.getInstanceWithMultiProcessLocking(new File(AndroidLocation.getFolder(), "atlas-buildCache"));
        } catch (AndroidLocation.AndroidLocationException e) {
            e.printStackTrace();
        }
    }
    if (variantOutputContext.getVariantContext().getAtlasExtension().getTBuildConfig().getMergeBundlesDex()) {
        allDexsArchives.addAll(Arrays.asList(mergeDexs[0]));
        return;
    }
    try {
        FileCache.QueryResult result = fileCache.createFileInCacheIfAbsent(buildCacheInputs, in -> {
            List<ForkJoinTask<Void>> mergeTasks = new ArrayList();
            if (dexingType == DexingType.NATIVE_MULTIDEX) {
                mergeTasks.addAll(handleNativeMultiDex(Arrays.asList(mergeDexs[0]), output, outputDir, mainDexListFile == null ? null : mainDexListFile.getSingleFile()));
            } else {
                mergeTasks.addAll(handleLegacyAndMonoDex(Arrays.asList(mergeDexs[0]), output, outputDir, mainDexListFile == null ? null : mainDexListFile.getSingleFile()));
            }
            mergeTasks.forEach(voidForkJoinTask -> voidForkJoinTask.join());
            cacheHandler.handleMissActionResult(outputDir, in);
            if (output != null) {
                try {
                    outputHandler.handleOutput(output);
                } catch (ProcessException e) {
                // ignore this one
                }
            }
        });
        cacheHandler.handleQueryResult(result, outputDir, "maindex");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ProcessException(com.android.ide.common.process.ProcessException) ProcessOutput(com.android.ide.common.process.ProcessOutput) ForkJoinTask(java.util.concurrent.ForkJoinTask) ProcessException(com.android.ide.common.process.ProcessException) FileCache(com.android.builder.utils.FileCache) AndroidLocation(com.android.prefs.AndroidLocation)

Example 3 with ProcessOutput

use of com.android.ide.common.process.ProcessOutput in project atlas by alibaba.

the class AtlasDexArchiveBuilderTransform method convertToDexArchive.

private List<File> convertToDexArchive(@NonNull Context context, @NonNull QualifiedContent input, @NonNull TransformOutputProvider outputProvider, boolean isIncremental) throws Exception {
    logger.verbose("Dexing {}", input.getFile().getAbsolutePath());
    ImmutableList.Builder<File> dexArchives = ImmutableList.builder();
    for (int bucketId = 0; bucketId < NUMBER_OF_BUCKETS; bucketId++) {
        File preDexOutputFile = getPreDexFile(outputProvider, input, bucketId);
        if (input.getFile().isDirectory()) {
            File cachedVersion = cacheHandler.getCachedVersionIfPresent(input.getFile());
            dexArchives.add(preDexOutputFile);
            if (cachedVersion != null) {
                FileUtils.copyDirectoryContentToDirectory(cachedVersion, preDexOutputFile);
                return dexArchives.build();
            }
        }
        if (preDexOutputFile.isDirectory() && preDexOutputFile.exists()) {
            FileUtils.cleanOutputDir(preDexOutputFile);
        } else {
            FileUtils.deleteIfExists(preDexOutputFile);
        }
        AtlasDexArchiveBuilderTransform.DexConversionParameters parameters = new AtlasDexArchiveBuilderTransform.DexConversionParameters(input, preDexOutputFile, NUMBER_OF_BUCKETS, bucketId, minSdkVersion, dexOptions.getAdditionalParameters(), inBufferSize, outBufferSize, dexer, isDebuggable, isIncremental, false);
        if (useGradleWorkers) {
            context.getWorkerExecutor().submit(DexArchiveBuilderTransform.DexConversionWorkAction.class, configuration -> {
                configuration.setIsolationMode(IsolationMode.NONE);
                configuration.setParams(parameters);
            });
        } else {
            executor.execute(() -> {
                ProcessOutputHandler outputHandler = new ParsingProcessOutputHandler(new ToolOutputParser(new DexParser(), Message.Kind.ERROR, logger), new ToolOutputParser(new DexParser(), logger), errorReporter);
                ProcessOutput output = null;
                try (Closeable ignored = output = outputHandler.createOutput()) {
                    launchProcessing(parameters, output.getStandardOutput(), output.getErrorOutput());
                } finally {
                    if (output != null) {
                        try {
                            outputHandler.handleOutput(output);
                        } catch (ProcessException e) {
                        // ignore this one
                        }
                    }
                }
                return null;
            });
        }
    }
    List<File> files = dexArchives.build();
    return files;
}
Also used : ParsingProcessOutputHandler(com.android.ide.common.blame.ParsingProcessOutputHandler) ProcessOutputHandler(com.android.ide.common.process.ProcessOutputHandler) DexArchiveBuilderTransform(com.android.build.gradle.internal.transforms.DexArchiveBuilderTransform) ProcessException(com.android.ide.common.process.ProcessException) ToolOutputParser(com.android.ide.common.blame.parser.ToolOutputParser) DexParser(com.android.ide.common.blame.parser.DexParser) ProcessOutput(com.android.ide.common.process.ProcessOutput) JarFile(java.util.jar.JarFile) ParsingProcessOutputHandler(com.android.ide.common.blame.ParsingProcessOutputHandler)

Example 4 with ProcessOutput

use of com.android.ide.common.process.ProcessOutput in project atlas by alibaba.

the class AtlasDexMerger method mergeAll.

public void mergeAll(TransformInvocation transformInvocation) throws IOException {
    ProcessOutput output = outputHandler.createOutput();
    for (File file : allDexsArchives) {
        logger.warning("mergeAll File:" + file.getAbsolutePath());
    }
    TransformOutputProvider outputProvider = transformInvocation.getOutputProvider();
    File outputDir = getDexOutputLocation(outputProvider, "main", TransformManager.SCOPE_FULL_PROJECT);
    File finalDexDir = new File(outputDir.getParentFile(), "temp");
    finalDexDir.mkdirs();
    List<ForkJoinTask<Void>> mergeTasks = new ArrayList();
    if (dexingType == DexingType.NATIVE_MULTIDEX) {
        mergeTasks.addAll(handleNativeMultiDex(allDexsArchives, output, finalDexDir, null));
    } else {
        mergeTasks.addAll(handleLegacyAndMonoDex(allDexsArchives, output, finalDexDir, mainDexListFile == null ? null : mainDexListFile.getSingleFile()));
    }
    mergeTasks.forEach(voidForkJoinTask -> voidForkJoinTask.join());
    FileUtils.deleteDirectory(outputDir);
    FileUtils.moveDirectory(finalDexDir, outputDir);
}
Also used : ProcessOutput(com.android.ide.common.process.ProcessOutput) TransformOutputProvider(com.android.build.api.transform.TransformOutputProvider) ForkJoinTask(java.util.concurrent.ForkJoinTask) File(java.io.File)

Example 5 with ProcessOutput

use of com.android.ide.common.process.ProcessOutput in project atlas by alibaba.

the class AwbDexsMerger method merge.

public void merge(AwbBundle awbBundle) {
    File file = variantOutputContext.getVariantContext().getAwbDexAchiveOutput(awbBundle);
    List<File> awbDexFiles = new ArrayList<>();
    if (!file.exists() || !file.isDirectory()) {
        return;
    }
    awbDexFiles.addAll(org.apache.commons.io.FileUtils.listFiles(file, new String[] { "jar", "dex" }, true));
    File[] mergeDexs = file.listFiles(pathname -> pathname.getName().endsWith(".jar") || pathname.isDirectory());
    sort(awbDexFiles);
    File outPutFolder = variantOutputContext.getAwbDexOutput(awbBundle.getName());
    try {
        FileUtils.cleanOutputDir(outPutFolder);
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileCache.Inputs buildCacheInputs = getBuildCacheInputs(awbDexFiles, dexingType, dexMerger, null, minSdkVersion, isDebuggable, awbBundle.getName(), ID);
    ProcessOutput output = outputHandler.createOutput();
    FileCache fileCache = BuildCacheUtils.createBuildCacheIfEnabled(variantOutputContext.getVariantContext().getProject(), variantOutputContext.getScope().getGlobalScope().getProjectOptions());
    if (fileCache == null) {
        try {
            fileCache = FileCache.getInstanceWithMultiProcessLocking(new File(AndroidLocation.getFolder(), "atlas-buildCache"));
        } catch (AndroidLocation.AndroidLocationException e) {
            e.printStackTrace();
        }
    }
    if (variantOutputContext.getVariantContext().getAtlasExtension().getTBuildConfig().getMergeBundlesDex() && !awbBundle.isRemote && awbBundle.isMBundle) {
        allDexsArchives.addAll(Arrays.asList(mergeDexs));
        return;
    }
    try {
        FileCache.QueryResult result = fileCache.createFileInCacheIfAbsent(buildCacheInputs, in -> {
            List<ForkJoinTask<Void>> mergeTasks = new ArrayList<ForkJoinTask<Void>>();
            mergeTasks.addAll(handleLegacyAndMonoDex(Arrays.asList(mergeDexs), output, outPutFolder, null));
            mergeTasks.forEach(voidForkJoinTask -> voidForkJoinTask.join());
            cacheHandler.handleMissActionResult(outPutFolder, in);
            if (output != null) {
                try {
                    outputHandler.handleOutput(output);
                } catch (ProcessException e) {
                // ignore this one
                }
            }
        });
        cacheHandler.handleQueryResult(result, outPutFolder, awbBundle.getName());
        if (awbBundle.isMBundle) {
            org.apache.commons.io.FileUtils.moveFile(new File(outPutFolder, CLASSES_DEX), new File(mainDexOut, "classes" + atomicInteger.incrementAndGet() + ".dex"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) ForkJoinTask(java.util.concurrent.ForkJoinTask) IOException(java.io.IOException) IOException(java.io.IOException) ProcessException(com.android.ide.common.process.ProcessException) FileCache(com.android.builder.utils.FileCache) ProcessException(com.android.ide.common.process.ProcessException) ProcessOutput(com.android.ide.common.process.ProcessOutput) File(java.io.File) AndroidLocation(com.android.prefs.AndroidLocation)

Aggregations

ProcessOutput (com.android.ide.common.process.ProcessOutput)6 ProcessException (com.android.ide.common.process.ProcessException)4 ForkJoinTask (java.util.concurrent.ForkJoinTask)3 DexArchiveBuilderTransform (com.android.build.gradle.internal.transforms.DexArchiveBuilderTransform)2 FileCache (com.android.builder.utils.FileCache)2 ParsingProcessOutputHandler (com.android.ide.common.blame.ParsingProcessOutputHandler)2 DexParser (com.android.ide.common.blame.parser.DexParser)2 ToolOutputParser (com.android.ide.common.blame.parser.ToolOutputParser)2 ProcessOutputHandler (com.android.ide.common.process.ProcessOutputHandler)2 AndroidLocation (com.android.prefs.AndroidLocation)2 File (java.io.File)2 JarFile (java.util.jar.JarFile)2 TransformOutputProvider (com.android.build.api.transform.TransformOutputProvider)1 DxConsole (com.taobao.android.dx.command.DxConsole)1 Main (com.taobao.android.dx.command.dexer.Main)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1