use of com.android.build.gradle.internal.api.AwbTransform in project atlas by alibaba.
the class AtlasTransformUtils method getTransformInputs.
public static Collection<File> getTransformInputs(AppVariantContext appVariantContext, TransformInvocation invocation) {
ImmutableSet.Builder<File> builder = ImmutableSet.builder();
Collection<File> transformInputs = TransformInputUtil.getAllFiles(invocation.getInputs());
builder.addAll(transformInputs);
AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(appVariantContext.getVariantConfiguration().getFullName());
if (atlasDependencyTree.getAwbBundles().size() > 0) {
BaseVariantOutput vod = (BaseVariantOutput) appVariantContext.getVariantOutputData().iterator().next();
AppVariantOutputContext appVariantOutputContext = appVariantContext.getAppVariantOutputContext(ApkDataUtils.get(vod));
Collection<AwbTransform> awbTransforms = appVariantOutputContext.getAwbTransformMap().values();
awbTransforms.forEach(awbTransform -> {
builder.addAll(awbTransform.getInputLibraries());
});
}
return builder.build();
}
use of com.android.build.gradle.internal.api.AwbTransform in project atlas by alibaba.
the class BundleProguarder method printInOut.
private static File printInOut(AppVariantContext appVariantContext, Input input) throws IOException {
File proguardDir = input.proguardOutputDir;
if (null == proguardDir) {
proguardDir = appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle());
}
proguardDir.mkdirs();
File inoutConfigs = new File(proguardDir, INOUT_CFG);
List<String> configs = new ArrayList<>();
for (AwbTransform awbTransform : input.getAwbBundles()) {
List<File> inputLibraries = Lists.newArrayList();
for (File inputLibrary : awbTransform.getInputLibraries()) {
configs.add(INJARS_OPTION + " " + inputLibrary.getAbsolutePath());
String name = FileNameUtils.getUniqueJarName(inputLibrary);
File obsJar = new File(proguardDir, name + ".jar");
input.proguardJarMap.put(obsJar, input.getFileMd5s().get(inputLibrary));
inputLibraries.add(obsJar);
configs.add(OUTJARS_OPTION + " " + obsJar.getAbsolutePath());
if (inputLibrary.isDirectory() && AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).getInputDirs().contains(inputLibrary)) {
input.maindexFolderTransform.put(inputLibrary, obsJar);
} else if (AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).inMainDex(inputLibrary)) {
input.maindexFileTransform.put(inputLibrary, obsJar);
// AtlasBuildContext.atlasMainDexHelper.updateMainDexFile(inputLibrary, obsJar);
} else if (awbTransform.getAwbBundle().isMainBundle()) {
AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).addMainDex(new BuildAtlasEnvTask.FileIdentity(obsJar.getName(), obsJar, false, false));
}
}
// configs.add();
if (null != awbTransform.getInputDirs() && awbTransform.getInputDirs().size() > 0) {
for (File dir : awbTransform.getInputDirs()) {
if (!dir.exists()) {
continue;
}
configs.add(INJARS_OPTION + " " + dir.getAbsolutePath());
String name = input.getFileMd5s().get(dir);
if (null == name) {
name = FileNameUtils.getUniqueFileName(dir.getName(), "proguard");
}
File obsJar = new File(proguardDir, name + ".jar");
inputLibraries.add(obsJar);
configs.add(OUTJARS_OPTION + " " + obsJar.getAbsolutePath());
if (awbTransform.getAwbBundle().isMainBundle()) {
AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).getInputDirs().clear();
AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).addMainDex(new BuildAtlasEnvTask.FileIdentity(obsJar.getName(), obsJar, false, false));
}
}
}
awbTransform.setInputFiles(inputLibraries);
awbTransform.getInputDirs().clear();
awbTransform.getInputLibraries().clear();
}
for (File library : input.getLibraries()) {
configs.add("-libraryjars " + library.getAbsolutePath());
}
FileUtils.writeLines(inoutConfigs, configs);
return inoutConfigs;
}
use of com.android.build.gradle.internal.api.AwbTransform in project atlas by alibaba.
the class BundleProguarder method loadProguardFromCache.
public static Result loadProguardFromCache(AppVariantContext appVariantContext, Input input) throws Exception {
Result result = new Result();
String md5 = input.getMd5();
result.key = input.getAwbBundles().get(0).getAwbBundle().getName() + "_" + md5;
logger.info("bundle proguard for " + result.key + " with md5 key " + md5);
File cacheDir = FileCacheCenter.queryFile(CACHE_TYPE, result.key, true, isRemoteCacheEnabled(appVariantContext));
result.cacheDir = cacheDir;
if (null == cacheDir || !cacheDir.exists()) {
logger.warn("bundle proguard for " + result.key + " miss cache " + cacheDir.getAbsolutePath());
return result;
}
logger.warn("bundle proguard for " + result.key + " hit cache ");
File keepJson = new File(cacheDir, "keep.json");
File proguardCfg = new File(cacheDir, "proguard.cfg");
File usageCfg = new File(cacheDir, "usage.cfg");
Map<String, File> md5Map = input.getMd5Files();
if (input.getAwbBundles().get(0).getAwbBundle().isMainBundle()) {
AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).getMainDexFiles().clear();
AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).getInputDirs().clear();
for (File file : cacheDir.listFiles()) {
if (file.getName().endsWith("jar") && ZipUtils.isZipFile(file)) {
String jarMd5 = file.getName().replace(".jar", "");
File srcFile = md5Map.get(jarMd5);
if (null != srcFile && srcFile.exists()) {
String fileName = FileNameUtils.getUniqueJarName(srcFile);
FileUtils.copyFile(file, new File(input.proguardOutputDir, fileName + ".jar"));
AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).addMainDex(new BuildAtlasEnvTask.FileIdentity(fileName, new File(input.proguardOutputDir, fileName + ".jar"), false, false));
} else {
FileUtils.copyFileToDirectory(file, input.proguardOutputDir);
AtlasBuildContext.atlasMainDexHelperMap.get(appVariantContext.getVariantName()).addMainDex(new BuildAtlasEnvTask.FileIdentity(file.getName(), new File(input.proguardOutputDir, file.getName()), false, false));
}
}
}
File awbProguardDir = input.printConfiguration.getParentFile();
// FileUtils.copyFileToDirectory(keepJson, awbProguardDir);
if (proguardCfg.exists()) {
FileUtils.copyFileToDirectory(proguardCfg, awbProguardDir);
}
if (usageCfg.exists()) {
FileUtils.copyFileToDirectory(usageCfg, awbProguardDir);
}
result.success = true;
return result;
} else {
if (!keepJson.exists()) {
logger.error("bundle proguard for " + result.key + " missing keep.json ");
FileUtils.deleteDirectory(cacheDir);
return result;
}
Map<AwbTransform, List<File>> transformListMap = new HashMap<>();
for (AwbTransform awbTransform : input.getAwbBundles()) {
File awbProguardDir = appVariantContext.getAwbProguardDir(awbTransform.getAwbBundle());
FileUtils.copyFileToDirectory(keepJson, awbProguardDir);
if (proguardCfg.exists()) {
FileUtils.copyFileToDirectory(proguardCfg, awbProguardDir);
}
if (usageCfg.exists()) {
FileUtils.copyFileToDirectory(usageCfg, awbProguardDir);
}
List<File> inputFiles = new ArrayList<>();
transformListMap.put(awbTransform, inputFiles);
List<File> files = new ArrayList<>();
files.addAll(awbTransform.getInputLibraries());
// configs.add();
if (null != awbTransform.getInputDirs() && awbTransform.getInputDirs().size() > 0) {
files.addAll(awbTransform.getInputDirs());
}
for (File oldFile : files) {
File file = new File(cacheDir, input.getFileMd5s().get(oldFile) + ".jar");
if (file.exists()) {
if (ZipUtils.isZipFile(file)) {
inputFiles.add(file);
}
} else {
logger.error("miss proguard jar for :" + file.getAbsolutePath());
FileUtils.deleteDirectory(cacheDir);
logger.error("delete cache Dir " + cacheDir.getAbsolutePath());
return result;
}
}
}
for (AwbTransform awbTransform : input.getAwbBundles()) {
awbTransform.setInputFiles(transformListMap.get(awbTransform));
awbTransform.getInputDirs().clear();
awbTransform.getInputLibraries().clear();
awbTransform.getAwbBundle().setKeepProguardFile(keepJson);
}
result.success = true;
return result;
}
}
use of com.android.build.gradle.internal.api.AwbTransform in project atlas by alibaba.
the class Input method getMd5.
public String getMd5() throws Exception {
if (StringUtils.isNotEmpty(md5)) {
return md5;
}
List<File> files = new ArrayList<>();
files.addAll(getParentKeeps());
files.addAll(getDefaultProguardFiles());
files.addAll(getLibraryProguardFiles());
for (AwbTransform awbTransform : this.getAwbBundles()) {
for (File file : awbTransform.getInputLibraries()) {
files.add(file);
}
// configs.add();
if (null != awbTransform.getInputDirs() && awbTransform.getInputDirs().size() > 0) {
files.addAll(awbTransform.getInputDirs());
}
}
for (File file : files) {
if (file.isFile()) {
String md5 = MD5Util.getFileMD5(file);
fileMd5s.put(file, md5);
} else {
String md5 = MD5Util.getFileMd5(FileUtils.listFiles(file, new String[] { "class" }, true));
fileMd5s.put(file, md5);
}
}
List<String> mds = new ArrayList<>(fileMd5s.values());
Collections.sort(mds);
md5 = MD5Util.getMD5(StringUtils.join(mds.toArray(new String[0])));
return md5;
}
use of com.android.build.gradle.internal.api.AwbTransform in project atlas by alibaba.
the class AtlasProguardHelper method doBundleProguard.
public static void doBundleProguard(final AppVariantContext appVariantContext, List<File> mainDexJars) throws Exception {
Profiler.enter("preparebundleproguard");
VariantScope variantScope = appVariantContext.getScope();
AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName());
if (dependencyTree.getAwbBundles().isEmpty()) {
return;
}
List<File> libs = new ArrayList<>(appVariantContext.getScope().getGlobalScope().getAndroidBuilder().getBootClasspath(true));
// All dependent classes
Profiler.enter("getDefaultClasses");
Set<String> defaultLibClasses = getClassList(libs);
Profiler.release();
libs.addAll(mainDexJars);
// Get the basic proguard configuration
List<File> defaultProguardFiles = new ArrayList<>(appVariantContext.getVariantConfiguration().getBuildType().getProguardFiles());
Collections.sort(defaultProguardFiles);
BaseVariantOutput vod = (BaseVariantOutput) appVariantContext.getVariantOutputData().iterator().next();
AppVariantOutputContext appVariantOutputContext = appVariantContext.getAppVariantOutputContext(ApkDataUtils.get(vod));
int parallelCount = appVariantContext.getAtlasExtension().getTBuildConfig().getProguardParallelCount();
// //FIXME
// parallelCount = 1;
Map<BundleInfo, AwbTransform> bundleInfoAwbTransformMap = new HashMap<>();
for (AwbTransform awbTransform : appVariantOutputContext.getAwbTransformMap().values()) {
bundleInfoAwbTransformMap.put(awbTransform.getAwbBundle().bundleInfo, awbTransform);
}
Profiler.release();
BundleGraphExecutor.execute(new ArrayList<>(bundleInfoAwbTransformMap.keySet()), parallelCount, new BundleItemRunner() {
@Override
public void execute(BundleItem bundleItem) {
try {
Input input = new Input();
AwbTransform awbTransform = bundleInfoAwbTransformMap.get(bundleItem.bundleInfo);
input.getAwbBundles().add(awbTransform);
for (BundleInfo sub : bundleItem.circles) {
input.getAwbBundles().add(bundleInfoAwbTransformMap.get(sub));
}
input.getDefaultProguardFiles().addAll(defaultProguardFiles);
input.getLibraries().addAll(libs);
input.getDefaultLibraryClasses().addAll(defaultLibClasses);
input.printConfiguration = new File(appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle()), "proguard.cfg");
input.printUsage = new File(appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle()), "usage.cfg");
addLibraryProguardFiles(appVariantContext, input);
addChildDependency(bundleItem, input, bundleInfoAwbTransformMap);
addParentKeeps(bundleItem, input, awbTransform, bundleInfoAwbTransformMap, appVariantContext);
BundleProguarder.execute(appVariantContext, input);
} catch (Exception e) {
e.printStackTrace();
throw new GradleException("proguard " + bundleItem.bundleInfo.getPkgName(), e);
}
}
});
}
Aggregations