Search in sources :

Example 1 with DexPatchFile

use of com.taobao.android.outputs.DexPatchFile in project atlas by alibaba.

the class DexPatchTool method doPatch.

@Override
public PatchFile doPatch() throws Exception {
    TpatchInput tpatchInput = (TpatchInput) input;
    DexPatchFile tpatchFile = new DexPatchFile();
    tpatchFile.diffJson = new File(tpatchInput.outPatchDir, "diff.json");
    tpatchFile.patchInfo = new File(tpatchInput.outPatchDir, "patchInfo.json");
    final File patchTmpDir = new File(tpatchInput.outPatchDir, "dexpatch-tmp");
    final File mainDiffFolder = new File(patchTmpDir, tpatchInput.mainBundleName);
    patchTmpDir.mkdirs();
    FileUtils.cleanDirectory(patchTmpDir);
    mainDiffFolder.mkdirs();
    Profiler.release();
    Profiler.enter("unzip apks");
    // unzip apk
    File unzipFolder = unzipApk(((TpatchInput) input).outPatchDir);
    final File newApkUnzipFolder = new File(unzipFolder, NEW_APK_UNZIP_NAME);
    final File baseApkUnzipFolder = new File(unzipFolder, BASE_APK_UNZIP_NAME);
    Profiler.release();
    ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper();
    String taskName = "diffBundleTask";
    // 
    Collection<File> soFiles = FileUtils.listFiles(newApkUnzipFolder, new String[] { "so" }, true);
    // process remote bumdle
    if (tpatchInput.splitDiffBundle != null) {
        for (final Pair<BundleBO, BundleBO> bundle : ((TpatchInput) input).splitDiffBundle) {
            executorServicesHelper.submitTask(taskName, new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    processBundleFiles(bundle.getSecond().getBundleFile(), bundle.getFirst().getBundleFile(), patchTmpDir);
                    return true;
                }
            });
        }
    }
    Profiler.enter("awbspatch");
    executorServicesHelper.submitTask(taskName, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            // 得到主bundle的dex diff文件
            createBundleDexPatch(newApkUnzipFolder, baseApkUnzipFolder, mainDiffFolder, true);
            return true;
        }
    });
    for (final File soFile : soFiles) {
        System.out.println("do dexpatch:" + soFile.getAbsolutePath());
        final String relativePath = PathUtils.toRelative(newApkUnzipFolder, soFile.getAbsolutePath());
        if (null != tpatchInput.notIncludeFiles && pathMatcher.match(tpatchInput.notIncludeFiles, relativePath)) {
            continue;
        }
        executorServicesHelper.submitTask(taskName, new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                File destFile = new File(patchTmpDir, tpatchInput.mainBundleName + "/" + relativePath);
                File baseSoFile = new File(baseApkUnzipFolder, relativePath);
                if (isBundleFile(soFile)) {
                    processBundleFiles(soFile, baseSoFile, patchTmpDir);
                }
                return true;
            }
        });
    }
    executorServicesHelper.waitTaskCompleted(taskName);
    executorServicesHelper.stop();
    Profiler.release();
    Profiler.enter("ziptpatchfile");
    // zip file
    File patchFile = createDexPatchFile(tpatchInput.outPatchDir, patchTmpDir);
    tpatchFile.patchFile = patchFile;
    if (!patchFile.exists()) {
        return null;
    }
    PatchInfo curPatchInfo = createBasePatchInfo(patchFile);
    Profiler.release();
    Profiler.enter("writejson");
    BuildPatchInfos buildPatchInfos = new BuildPatchInfos();
    buildPatchInfos.getPatches().add(curPatchInfo);
    buildPatchInfos.setBaseVersion(input.baseApkBo.getVersionName());
    buildPatchInfos.setDiffBundleDex(true);
    FileUtils.writeStringToFile(tpatchInput.outPutJson, JSON.toJSONString(buildPatchInfos));
    // 删除临时的目录
    FileUtils.deleteDirectory(patchTmpDir);
    apkDiff.setBaseApkVersion(input.baseApkBo.getVersionName());
    apkDiff.setNewApkVersion(input.newApkBo.getVersionName());
    apkDiff.setBundleDiffResults(bundleDiffResults);
    boolean newApkFileExist = input.newApkBo.getApkFile().exists() && input.newApkBo.getApkFile().isFile();
    if (newApkFileExist) {
        apkDiff.setNewApkMd5(MD5Util.getFileMD5String(input.newApkBo.getApkFile()));
    }
    apkDiff.setFileName(input.newApkBo.getApkName());
    apkPatchInfos.setBaseApkVersion(input.baseApkBo.getVersionName());
    apkPatchInfos.setNewApkVersion(input.newApkBo.getVersionName());
    apkPatchInfos.setBundleDiffResults(diffPatchInfos);
    apkPatchInfos.setFileName(patchFile.getName());
    apkPatchInfos.setNewApkMd5(MD5Util.getFileMD5String(patchFile));
    FileUtils.writeStringToFile(tpatchFile.diffJson, JSON.toJSONString(apkDiff));
    FileUtils.writeStringToFile(tpatchFile.patchInfo, JSON.toJSONString(apkPatchInfos));
    FileUtils.copyFileToDirectory(tpatchFile.diffJson, tpatchInput.outPatchDir.getParentFile(), true);
    if (newApkFileExist) {
        FileUtils.copyFileToDirectory(input.newApkBo.getApkFile(), tpatchInput.outPatchDir.getParentFile(), true);
    }
    Profiler.release();
    logger.warning(Profiler.dump());
    return tpatchFile;
}
Also used : BundleBO(com.taobao.android.tpatch.model.BundleBO) DexPatchFile(com.taobao.android.outputs.DexPatchFile) PatchInfo(com.taobao.android.object.PatchInfo) TpatchInput(com.taobao.android.inputs.TpatchInput) IOException(java.io.IOException) ExecutorServicesHelper(com.taobao.android.task.ExecutorServicesHelper) File(java.io.File) DexPatchFile(com.taobao.android.outputs.DexPatchFile) PatchFile(com.taobao.android.outputs.PatchFile) BuildPatchInfos(com.taobao.android.object.BuildPatchInfos)

Aggregations

TpatchInput (com.taobao.android.inputs.TpatchInput)1 BuildPatchInfos (com.taobao.android.object.BuildPatchInfos)1 PatchInfo (com.taobao.android.object.PatchInfo)1 DexPatchFile (com.taobao.android.outputs.DexPatchFile)1 PatchFile (com.taobao.android.outputs.PatchFile)1 ExecutorServicesHelper (com.taobao.android.task.ExecutorServicesHelper)1 BundleBO (com.taobao.android.tpatch.model.BundleBO)1 File (java.io.File)1 IOException (java.io.IOException)1