use of com.android.ide.common.process.ProcessException in project atlas by alibaba.
the class ProcessAwoAndroidResources method doFullTaskAction.
@Override
protected void doFullTaskAction() throws IOException {
// we have to clean the source folder output in case the package name changed.
File srcOut = getSourceOutputDir();
if (srcOut != null) {
// FileUtils.emptyFolder(srcOut);
srcOut.delete();
srcOut.mkdirs();
}
@Nullable File resOutBaseNameFile = getPackageOutputFile();
// If are in instant run mode and we have an instant run enabled manifest
File instantRunManifest = getInstantRunManifestFile();
File manifestFileToPackage = instantRunBuildContext.isInInstantRunMode() && instantRunManifest != null && instantRunManifest.exists() ? instantRunManifest : getManifestFile();
// 增加awb模块编译所需要的额外参数
addAaptOptions();
AaptPackageProcessBuilder aaptPackageCommandBuilder = new AaptPackageProcessBuilder(manifestFileToPackage, getAaptOptions()).setAssetsFolder(getAssetsDir()).setResFolder(getResDir()).setLibraries(getLibraries()).setPackageForR(getPackageForR()).setSourceOutputDir(absolutePath(srcOut)).setSymbolOutputDir(absolutePath(getTextSymbolOutputDir())).setResPackageOutput(absolutePath(resOutBaseNameFile)).setProguardOutput(absolutePath(getProguardOutputFile())).setType(getType()).setDebuggable(getDebuggable()).setPseudoLocalesEnabled(getPseudoLocalesEnabled()).setResourceConfigs(getResourceConfigs()).setSplits(getSplits()).setPreferredDensity(getPreferredDensity());
@NonNull AtlasBuilder builder = (AtlasBuilder) getBuilder();
// MergingLog mergingLog = new MergingLog(getMergeBlameLogFolder());
//
// ProcessOutputHandler processOutputHandler = new ParsingProcessOutputHandler(
// new ToolOutputParser(new AaptOutputParser(), getILogger()),
// builder.getErrorReporter());
ProcessOutputHandler processOutputHandler = new LoggedProcessOutputHandler(getILogger());
try {
builder.processAwbResources(aaptPackageCommandBuilder, getEnforceUniquePackageName(), processOutputHandler, getMainSymbolFile());
if (resOutBaseNameFile != null) {
if (instantRunBuildContext.isInInstantRunMode()) {
instantRunBuildContext.addChangedFile(InstantRunBuildContext.FileType.RESOURCES, resOutBaseNameFile);
// get the new manifest file CRC
JarFile jarFile = new JarFile(resOutBaseNameFile);
String currentIterationCRC = null;
try {
ZipEntry entry = jarFile.getEntry(SdkConstants.ANDROID_MANIFEST_XML);
if (entry != null) {
currentIterationCRC = String.valueOf(entry.getCrc());
}
} finally {
jarFile.close();
}
// check the manifest file binary format.
File crcFile = new File(instantRunSupportDir, "manifest.crc");
if (crcFile.exists() && currentIterationCRC != null) {
// compare its content with the new binary file crc.
String previousIterationCRC = Files.readFirstLine(crcFile, Charsets.UTF_8);
if (!currentIterationCRC.equals(previousIterationCRC)) {
instantRunBuildContext.close();
instantRunBuildContext.setVerifierResult(InstantRunVerifierStatus.BINARY_MANIFEST_FILE_CHANGE);
}
}
// write the new manifest file CRC.
Files.createParentDirs(crcFile);
Files.write(currentIterationCRC, crcFile, Charsets.UTF_8);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ProcessException e) {
throw new RuntimeException(e);
}
}
use of com.android.ide.common.process.ProcessException 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;
}
use of com.android.ide.common.process.ProcessException 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();
}
}
use of com.android.ide.common.process.ProcessException in project atlas by alibaba.
the class TaobaoInstantRunDex method transform.
@Override
public void transform(@NonNull TransformInvocation invocation) throws IOException, TransformException, InterruptedException {
File outputFolder = variantScope.getReloadDexOutputFolder();
// boolean changesAreCompatible =
// variantScope.getInstantRunBuildContext().hasPassedVerification();
// boolean restartDexRequested =
// variantScope.getGlobalScope().isActive(OptionalCompilationStep.RESTART_ONLY);
// if (!changesAreCompatible || restartDexRequested) {
FileUtils.cleanOutputDir(outputFolder);
// return;
// }
// create a tmp jar file.
File classesJar = new File(outputFolder, "classes.jar");
if (classesJar.exists()) {
FileUtils.delete(classesJar);
}
Files.createParentDirs(classesJar);
final TaobaoInstantRunDex.JarClassesBuilder jarClassesBuilder = getJarClassBuilder(classesJar);
try {
for (TransformInput input : invocation.getReferencedInputs()) {
for (DirectoryInput directoryInput : input.getDirectoryInputs()) {
if (!directoryInput.getContentTypes().contains(ExtendedContentType.CLASSES_ENHANCED)) {
continue;
}
final File folder = directoryInput.getFile();
if (invocation.isIncremental()) {
for (Map.Entry<File, Status> entry : directoryInput.getChangedFiles().entrySet()) {
if (entry.getValue() != Status.REMOVED) {
File file = entry.getKey();
if (file.isFile()) {
jarClassesBuilder.add(folder, file);
}
}
}
} else {
Iterable<File> files = FileUtils.getAllFiles(folder);
for (File inputFile : files) {
jarClassesBuilder.add(folder, inputFile);
}
}
}
}
} finally {
jarClassesBuilder.close();
}
// if no files were added, clean up and return.
if (jarClassesBuilder.isEmpty()) {
FileUtils.cleanOutputDir(outputFolder);
return;
}
if (preloadJarHooker != null) {
classesJar = preloadJarHooker.process(classesJar);
}
final ImmutableList.Builder<File> inputFiles = ImmutableList.builder();
inputFiles.add(classesJar);
try {
variantScope.getInstantRunBuildContext().startRecording(InstantRunBuildContext.TaskType.INSTANT_RUN_DEX);
convertByteCode(inputFiles.build(), outputFolder);
variantScope.getInstantRunBuildContext().addChangedFile(FileType.RELOAD_DEX, new File(outputFolder, "classes.dex"));
} catch (ProcessException e) {
throw new TransformException(e);
} finally {
variantScope.getInstantRunBuildContext().stopRecording(InstantRunBuildContext.TaskType.INSTANT_RUN_DEX);
}
variantScope.getInstantRunBuildContext().close();
if (variantContext.getScope().getInstantRunBuildContext().isInInstantRunMode()) {
InstantRunBuildContext instantRunBuildContext = variantContext.getScope().getInstantRunBuildContext();
InstantRunBuildContext.Artifact artifact = instantRunBuildContext.getLastBuild().getArtifactForType(FileType.RELOAD_DEX);
File patchFile = artifact.getLocation();
String baseVersion = ApkDataUtils.get(variantOutput).getVersionName();
if (artifact != null && patchFile.exists()) {
File finalFile = variantContext.getAppVariantOutputContext(ApkDataUtils.get(variantOutput)).getIPatchFile(baseVersion);
zipPatch(finalFile, patchFile);
} else {
logger.warning("patchFile is not exist or no classes is modified!");
}
return;
}
}
use of com.android.ide.common.process.ProcessException in project atlas by alibaba.
the class TPatchDiffResAPBuildTask method doAction.
@TaskAction
protected void doAction() throws IOException {
manifestFile = getManifestFile();
aaptOptions = getAaptOptions();
diffResDir = getDiffResDir();
resourceConfigs = getResourceConfigs();
preferredDensity = getPreferredDensity();
SplitList splitList = SplitList.load(splitListInput);
// TODO minifyManifest
File miniManifest = new File(packageOutputFile.getParentFile(), "AndroidManifest.xml");
try {
ManifestFileUtils.createPatchManifest(manifestFile, getBaseManifestFile(), miniManifest);
} catch (DocumentException e) {
throw new GradleException(e.getMessage());
}
String splitFilter = apkData.getFilter(com.android.build.OutputFile.FilterType.DENSITY);
String preferredDensity = splitFilter != null ? splitFilter : // if resConfigs is set, we should not use our preferredDensity.
splitList.getFilters(SplitList.RESOURCE_CONFIGS).isEmpty() ? buildTargetDensity : null;
File assetsFolder = new File(diffResDir, "assets");
File resFolder = new File(diffResDir, "res");
if (!assetsFolder.exists()) {
assetsFolder.mkdirs();
}
if (!resFolder.exists()) {
resFolder.mkdirs();
}
aaptOptions.getAdditionalParameters().add("-A");
aaptOptions.getAdditionalParameters().add(assetsFolder.getAbsolutePath());
AaptGeneration aaptGeneration = AaptGeneration.fromProjectOptions(appVariantContext.getScope().getGlobalScope().getProjectOptions());
Aapt aapt = makeAapt(aaptGeneration);
AaptPackageConfig.Builder config = new AaptPackageConfig.Builder().setManifestFile(miniManifest).setOptions(DslAdaptersKt.convert(aaptOptions)).setResourceDir(resFolder).setLibrarySymbolTableFiles(ImmutableSet.of()).setCustomPackageForR(packageForR).setResourceOutputApk(packageOutputFile).setVariantType(type).setDebuggable(debuggable).setResourceConfigs(splitList.getFilters(SplitList.RESOURCE_CONFIGS)).setSplits(getSplits(splitList)).setPreferredDensity(preferredDensity).setListResourceFiles(aaptGeneration == AaptGeneration.AAPT_V2);
try {
getBuilder().processResources(aapt, config);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ProcessException e) {
e.printStackTrace();
}
}
Aggregations