use of com.android.builder.core.AndroidBuilder in project atlas by alibaba.
the class ProcessAwbAndroidResources method doFullTaskAction.
@Override
protected void doFullTaskAction() throws IOException {
File srcOut = getSourceOutputDir();
aaptGeneration = AaptGeneration.fromProjectOptions(appVariantContext.getScope().getGlobalScope().getProjectOptions());
if (srcOut != null) {
// FileUtils.emptyFolder(srcOut);
srcOut.delete();
srcOut.mkdirs();
}
getTextSymbolOutputDir().mkdirs();
getPackageOutputFile().getParentFile().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();
// Add additional parameters required for the awb module to compile
Set<File> libraries = new HashSet<>();
for (AndroidLibrary androidLibrary : getLibraries()) {
if (androidLibrary.getSymbolFile().exists()) {
if (androidLibrary.getManifest().exists() && androidLibrary.getSymbolFile().exists()) {
File libraryRtxt = androidLibrary.getSymbolFile();
File awbPackageR = new File(libraryRtxt.getParentFile(), "package-aware-r.txt");
SymbolIo.writeSymbolTableWithPackage(libraryRtxt.toPath(), androidLibrary.getManifest().toPath(), awbPackageR.toPath());
libraries.add(awbPackageR);
}
}
}
// libraries.addAll(mainDexSymbolFileCollection.getFiles());
addAaptOptions(aaptGeneration);
AaptPackageConfig.Builder aaptPackageCommandBuilder = new AaptPackageConfig.Builder().setManifestFile(manifestFileToPackage).setOptions(DslAdaptersKt.convert(getAaptOptions())).setResourceDir(getResDir()).setLibrarySymbolTableFiles(libraries).setCustomPackageForR(getPackageForR()).setSourceOutputDir(srcOut).setSymbolOutputDir(getTextSymbolOutputDir()).setResourceOutputApk(resOutBaseNameFile).setProguardOutputFile(getProguardOutputFile()).setVariantType(getType()).setDebuggable(debuggable).setResourceConfigs(getResourceConfigs()).setSplits(getSplits()).setPreferredDensity(getPreferredDensity()).setPseudoLocalize(getPseudoLocalesEnabled()).setListResourceFiles(aaptGeneration == AaptGeneration.AAPT_V2);
// .setPackageId(Integer.valueOf(getCustomPackageId()));
@NonNull AndroidBuilder builder = getBuilder();
Aapt aapt = makeAapt();
try {
if (builder instanceof AtlasBuilder) {
((AtlasBuilder) builder).processAwbResources(aapt, aaptPackageCommandBuilder, getMainSymbolFile(), awbBundle.getAndroidLibrary().getSymbolFile(), appVariantContext.getVariantData().getApplicationId());
} else {
builder.processResources(aapt, aaptPackageCommandBuilder);
}
if (resOutBaseNameFile != null) {
if (instantRunBuildContext.isInInstantRunMode()) {
instantRunBuildContext.addChangedFile(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 (Exception e) {
e.printStackTrace();
throw new GradleException("process res exception", e);
}
}
Aggregations