Search in sources :

Example 1 with ProcessResult

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

the class AtlasBuilder method processResources.

/**
     * 对主bundle的资源进行处理
     *
     * @param aaptCommand
     * @param enforceUniquePackageName
     * @param processOutputHandler
     * @throws IOException
     * @throws InterruptedException
     * @throws ProcessException
     */
public void processResources(AaptPackageProcessBuilder aaptCommand, boolean enforceUniquePackageName, ProcessOutputHandler processOutputHandler) throws IOException, InterruptedException, ProcessException {
    checkState(getTargetInfo() != null, "Cannot call processResources() before setTargetInfo() is called.");
    BuildToolInfo buildToolInfo = getTargetInfo().getBuildTools();
    // launch aapt: create the command line
    ProcessInfo processInfo = aaptCommand.build(buildToolInfo, getTargetInfo().getTarget(), getLogger());
    processInfo = new TProcessInfo(processInfo);
    // 打印日志
    //        if (null != getLogger()) {
    //            getLogger().info("[Aapt]" + processInfo.getExecutable() + " "
    //                    + StringUtils.join(processInfo.getArgs(), " "));
    //        }
    ProcessResult result = getProcessExecutor().execute(processInfo, processOutputHandler);
    result.rethrowFailure().assertNormalExitValue();
    // If the project has libraries, R needs to be created for each library.
    if (aaptCommand.getSourceOutputDir() != null && !aaptCommand.getLibraries().isEmpty()) {
        SymbolLoader fullSymbolValues = null;
        // First pass processing the libraries, collecting them by packageName,
        // and ignoring the ones that have the same package name as the application
        // (since that R class was already created).
        String appPackageName = aaptCommand.getPackageForR();
        if (appPackageName == null) {
            appPackageName = ManifestFileUtils.getPackage(aaptCommand.getManifestFile());
        }
        // list of all the symbol loaders per package names.
        Multimap<String, SymbolLoader> libMap = ArrayListMultimap.create();
        for (AndroidLibrary lib : aaptCommand.getLibraries()) {
            if (lib.isOptional()) {
                continue;
            }
            String packageName = ManifestFileUtils.getPackage(lib.getManifest());
            if (appPackageName == null) {
                continue;
            }
            if (appPackageName.equals(packageName)) {
                if (enforceUniquePackageName) {
                    String msg = String.format("Error: A library uses the same package as this project: %s", packageName);
                    throw new RuntimeException(msg);
                }
                // ignore libraries that have the same package name as the app
                continue;
            }
            File rFile = lib.getSymbolFile();
            // if the library has no resource, this file won't exist.
            if (rFile.isFile()) {
                // resources anywhere.
                if (fullSymbolValues == null) {
                    fullSymbolValues = new SymbolLoader(new File(aaptCommand.getSymbolOutputDir(), "R.txt"), getLogger());
                    fullSymbolValues.load();
                }
                SymbolLoader libSymbols = new SymbolLoader(rFile, getLogger());
                libSymbols.load();
                // store these symbols by associating them with the package name.
                libMap.put(packageName, libSymbols);
            }
        }
        // now loop on all the package name, merge all the symbols to write, and write them
        for (String packageName : libMap.keySet()) {
            Collection<SymbolLoader> symbols = libMap.get(packageName);
            if (enforceUniquePackageName && symbols.size() > 1) {
                String msg = String.format("Error: more than one library with package name '%s'", packageName);
                throw new RuntimeException(msg);
            }
            SymbolWriter writer = new SymbolWriter(aaptCommand.getSourceOutputDir(), packageName, fullSymbolValues, false);
            for (SymbolLoader symbolLoader : symbols) {
                writer.addSymbolsToWrite(symbolLoader);
            }
            writer.write();
        }
    }
}
Also used : SymbolWriter(com.android.builder.internal.SymbolWriter) BuildToolInfo(com.android.sdklib.BuildToolInfo) AndroidLibrary(com.android.builder.model.AndroidLibrary) ProcessResult(com.android.ide.common.process.ProcessResult) ProcessInfo(com.android.ide.common.process.ProcessInfo) SymbolLoader(com.android.builder.internal.SymbolLoader) File(java.io.File)

Example 2 with ProcessResult

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

the class AtlasBuilder method processAwbResources.

/**
     * 处理awb的资源
     *
     * @param aaptCommand
     * @param enforceUniquePackageName
     * @param processOutputHandler
     * @param mainSymbolFile
     */
public void processAwbResources(AaptPackageProcessBuilder aaptCommand, boolean enforceUniquePackageName, ProcessOutputHandler processOutputHandler, File mainSymbolFile) throws IOException, InterruptedException, ProcessException {
    if (!useCustomAapt) {
        throw new StopExecutionException("Must set useCustomAapt value to true for awb build!");
    }
    checkState(getTargetInfo() != null, "Cannot call processResources() before setTargetInfo() is called.");
    // launch aapt: create the command line
    ProcessInfo processInfo = aaptCommand.build(getTargetInfo().getBuildTools(), getTargetInfo().getTarget(), getLogger());
    processInfo = new TProcessInfo(processInfo, aaptCommand.getSymbolOutputDir());
    // 打印日志
    //        if (null != getLogger()) {
    //            getLogger().info("[Aapt]" + processInfo.getExecutable() + " "
    //                    + StringUtils.join(processInfo.getArgs(), " "));
    //        }
    ProcessResult result = getProcessExecutor().execute(processInfo, processOutputHandler);
    result.rethrowFailure().assertNormalExitValue();
    processAwbSymbols(aaptCommand, mainSymbolFile, enforceUniquePackageName);
}
Also used : StopExecutionException(org.gradle.api.tasks.StopExecutionException) ProcessResult(com.android.ide.common.process.ProcessResult) ProcessInfo(com.android.ide.common.process.ProcessInfo)

Aggregations

ProcessInfo (com.android.ide.common.process.ProcessInfo)2 ProcessResult (com.android.ide.common.process.ProcessResult)2 SymbolLoader (com.android.builder.internal.SymbolLoader)1 SymbolWriter (com.android.builder.internal.SymbolWriter)1 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 BuildToolInfo (com.android.sdklib.BuildToolInfo)1 File (java.io.File)1 StopExecutionException (org.gradle.api.tasks.StopExecutionException)1