use of com.android.build.gradle.tasks.PackageApplication in project atlas by alibaba.
the class PackageAwbsTask method createAwbPackages.
/**
* Directory of so
*/
@TaskAction
void createAwbPackages() throws ExecutionException, InterruptedException, IOException {
File awbApkOutputDir = appVariantContext.getAwbApkOutputDir();
FileUtils.cleanOutputDir(awbApkOutputDir);
AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
if (null == atlasDependencyTree) {
return;
}
ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper(taskName, getLogger(), 0);
List<Runnable> runnables = new ArrayList<>();
final AtomicLong dexTotalTime = new AtomicLong(0);
final AtomicLong packageTotalTime = new AtomicLong(0);
final Map<String, Long[]> monitors = new HashMap<String, Long[]>();
long startTime = System.currentTimeMillis();
for (final AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
if (awbBundle.isMBundle) {
continue;
}
runnables.add(() -> {
try {
long start = System.currentTimeMillis();
// create dex
File dexOutputFile = appVariantContext.getAwbDexOutput(awbBundle.getName());
long endDex = System.currentTimeMillis();
// PACKAGE APP:
File resourceFile = null;
if (appVariantContext.getScope().useResourceShrinker()) {
resourceFile = appVariantOutputContext.getAwbCompressResourcePackageOutputFile(awbBundle);
} else {
resourceFile = appVariantOutputContext.getAwbAndroidResourcesMap().get(awbBundle.getName()).getPackageOutputFile();
}
if (!resourceFile.exists()) {
resourceFile = appVariantOutputContext.getAwbAndroidResourcesMap().get(awbBundle.getName()).getPackageOutputFile();
}
Set<File> dexFolders = new HashSet<File>();
dexFolders.add(dexOutputFile);
Set<File> jniFolders = Sets.newHashSet();
if (appVariantOutputContext.getAwbJniFolder(awbBundle) != null && appVariantOutputContext.getAwbJniFolder(awbBundle).exists()) {
jniFolders.add(appVariantOutputContext.getAwbJniFolder(awbBundle));
}
Set<File> javaResourcesLocations = Sets.newHashSet();
if (appVariantContext.getAtlasExtension().getTBuildConfig().isIncremental() && awbBundle.getAllLibraryAars().size() > 1) {
File baseAwb = appVariantOutputContext.getVariantContext().apContext.getBaseAwb(awbBundle.getAwbSoName());
if (baseAwb != null) {
ZipFile files = new ZipFile(baseAwb);
ZipEntry entry = files.getEntry("classes.dex");
if (entry == null) {
throw new DexException("Expected classes.dex in " + baseAwb);
}
File file = new File(dexOutputFile, "classes.dex");
com.android.dex.Dex[] dexes = new com.android.dex.Dex[2];
dexes[0] = new com.android.dex.Dex(file);
dexes[1] = new com.android.dex.Dex(files.getInputStream(entry));
com.android.dex.Dex merged = new DexMerger(dexes, CollisionPolicy.KEEP_FIRST, new DxContext()).merge();
merged.writeTo(file);
javaResourcesLocations.add(baseAwb);
}
}
if (appVariantContext.getAtlasExtension().getTBuildConfig().getMergeAwbJavaRes()) {
javaResourcesLocations.add(new File(appVariantOutputContext.getAwbJavaResFolder(awbBundle), "res.jar"));
}
AwbApkPackageTask awbApkPackageTask = new AwbApkPackageTask(getProject().files(resourceFile), appVariantContext, awbBundle, appVariantOutputContext, getProject().files(dexFolders), getProject().files(javaResourcesLocations), getProject().files(appVariantOutputContext.getVariantContext().getMergeAssets(awbBundle)), getProject().files(jniFolders), getProject().files(awbBundle.getMergedManifest()), getBuilder(), config.getMinSdkVersion().getApiLevel(), getName());
File awbFile = awbApkPackageTask.doFullTaskAction();
bundles.put(awbBundle, awbFile);
// // //TODO 2.3
// AtlasBuildContext.androidBuilderMap.get(getProject())
// .oldPackageApk(resourceFile.getAbsolutePath(),
// dexFolders,
// javaResourcesLocations,
// jniFolders,
// null,
// getAbiFilters(),
// config.getBuildType().isJniDebuggable(),
// null,
// getOutputFile(awbBundle),
// config.getMinSdkVersion().getApiLevel(),
// new com.google.common.base.Predicate<String>() {
// @Override
// public boolean apply(@Nullable String s) {
// return false;
// }
// });
long endPackage = System.currentTimeMillis();
dexTotalTime.addAndGet(endDex - start);
packageTotalTime.addAndGet(endPackage - endDex);
monitors.put(awbBundle.getName(), new Long[] { endDex - start, endPackage - endDex });
} catch (Throwable e) {
// e.printStackTrace();
throw new GradleException("package " + awbBundle.getName() + " failed", e);
}
});
}
executorServicesHelper.execute(runnables);
getLogger().info(">>>>> packageAwbs >>>>>>>>>>>>");
FileLogger fileLogger = FileLogger.getInstance("packageAwbs");
fileLogger.log("totalTime is " + (System.currentTimeMillis() - startTime));
fileLogger.log("dexTotalTime is " + dexTotalTime);
fileLogger.log("packageTotalTime is " + packageTotalTime);
String format = "[packageawb] bundle:%50s dexTime: %10d packageTime: %10d ";
for (String bundle : monitors.keySet()) {
Long[] value = monitors.get(bundle);
fileLogger.log(String.format(format, bundle, value[0], value[1]));
}
getLogger().info(">>>>> packageAwbs >>>>>>>>>>>>");
PackageApplication packageApplication = appVariantContext.getScope().getPackageApplicationTask().get(new TaskContainerAdaptor(appVariantContext.getScope().getGlobalScope().getProject().getTasks()));
packageApplication.doFirst(new FirstApkAction(appVariantOutputContext));
packageApplication.doLast(new LastApkAction());
}
use of com.android.build.gradle.tasks.PackageApplication in project atlas by alibaba.
the class FirstApkAction method execute.
@Override
public void execute(Task task) {
assert task instanceof PackageApplication;
PackageApplication packageApplication = (PackageApplication) task;
if (packageApplication.getName().startsWith("packageInstantRunResources")) {
return;
}
File bundleInfoFile = new File(appVariantOutputContext.getScope().getGlobalScope().getOutputsDir(), "bundleInfo-" + appVariantOutputContext.getVariantContext().getVariantConfiguration().getVersionName() + ".json");
File nativeInfoFile = new File(appVariantOutputContext.getScope().getGlobalScope().getOutputsDir(), "nativeInfo-" + appVariantOutputContext.getVariantContext().getVariantConfiguration().getVersionName() + ".json");
File resOutBaseNameFile = new File(packageApplication.getResourceFiles().getSingleFile(), FN_RES_BASE + RES_QUALIFIER_SEP + appVariantOutputContext.getVariantContext().getVariantName() + SdkConstants.DOT_RES);
File[] dexs = null;
if (packageApplication.getDexFolders().getFiles().size() == 0) {
dexs = appVariantOutputContext.getDexMergeFolder().listFiles(pathname -> pathname.getName().equals("classes.dex"));
ReflectUtils.updateField(packageApplication, "dexFolders", appVariantOutputContext.getVariantContext().getProject().files(appVariantOutputContext.getDexMergeFolder()));
} else {
dexs = packageApplication.getDexFolders().getSingleFile().listFiles(pathname -> pathname.getName().equals("classes.dex"));
}
if (dexs != null && dexs.length == 1) {
File androidManifest = null;
if (appVariantOutputContext.getApkData().getFilterName() == null) {
androidManifest = com.android.utils.FileUtils.join(appVariantOutputContext.getScope().getOutput(TaskOutputHolder.TaskOutputType.MERGED_MANIFESTS).getSingleFile(), "AndroidManifest.xml");
} else {
androidManifest = com.android.utils.FileUtils.join(appVariantOutputContext.getScope().getOutput(TaskOutputHolder.TaskOutputType.MERGED_MANIFESTS).getSingleFile(), appVariantOutputContext.getApkData().getDirName(), "AndroidManifest.xml");
}
File file = AtlasBuildContext.atlasApkProcessor.securitySignApk(dexs[0], androidManifest, appVariantOutputContext.getVariantContext().getBuildType(), false);
if (file != null && file.exists()) {
try {
BetterZip.addFile(resOutBaseNameFile, "res/drawable/".concat(file.getName()), file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (bundleInfoFile.exists()) {
try {
BetterZip.addFile(resOutBaseNameFile, "assets/".concat(bundleInfoFile.getName()), bundleInfoFile);
} catch (IOException e) {
e.printStackTrace();
}
}
if (nativeInfoFile.exists()) {
try {
BetterZip.addFile(resOutBaseNameFile, "assets/".concat(nativeInfoFile.getName()), nativeInfoFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Aggregations