Search in sources :

Example 1 with TpatchFile

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

the class TPatchTool method doPatch.

@Override
public PatchFile doPatch() throws Exception {
    TpatchInput tpatchInput = (TpatchInput) input;
    TpatchFile tpatchFile = new TpatchFile();
    File hisPatchJsonFile = new File(tpatchInput.outPutJson.getParentFile(), "patchs-" + input.newApkBo.getVersionName() + ".json");
    hisTpatchFolder = new File(tpatchInput.outPatchDir.getParentFile().getParentFile().getParentFile().getParentFile(), "hisTpatch");
    tpatchFile.diffJson = new File(((TpatchInput) input).outPatchDir, "diff.json");
    tpatchFile.patchInfo = new File(((TpatchInput) input).outPatchDir, "patchInfo.json");
    final File patchTmpDir = new File(((TpatchInput) input).outPatchDir, "tpatch-tmp");
    final File mainDiffFolder = new File(patchTmpDir, ((TpatchInput) input).mainBundleName);
    patchTmpDir.mkdirs();
    FileUtils.cleanDirectory(patchTmpDir);
    mainDiffFolder.mkdirs();
    File lastPatchFile = null;
    readWhiteList(((TpatchInput) input).bundleWhiteList);
    lastPatchFile = getLastPatchFile(input.baseApkBo.getVersionName(), ((TpatchInput) input).productName, ((TpatchInput) input).outPatchDir);
    PatchUtils.getTpatchClassDef(lastPatchFile, bundleClassMap);
    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) input).splitDiffBundle != null)) {
        for (final Pair<BundleBO, BundleBO> bundle : ((TpatchInput) input).splitDiffBundle) {
            if (bundle.getFirst() == null || bundle.getSecond() == null) {
                logger.warning("remote bundle is not set to splitDiffBundles");
                continue;
            }
            executorServicesHelper.submitTask(taskName, new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    TPatchTool.this.processBundleFiles(bundle.getSecond().getBundleFile(), bundle.getFirst().getBundleFile(), patchTmpDir);
                    return true;
                }
            });
        }
    }
    Profiler.enter("awbspatch");
    Collection<File> retainFiles = FileUtils.listFiles(newApkUnzipFolder, new IOFileFilter() {

        @Override
        public boolean accept(File file) {
            String relativePath = PathUtils.toRelative(newApkUnzipFolder, file.getAbsolutePath());
            if (pathMatcher.match(DEFAULT_NOT_INCLUDE_RESOURCES, relativePath)) {
                return false;
            }
            if (null != ((TpatchInput) (input)).notIncludeFiles && pathMatcher.match(((TpatchInput) (input)).notIncludeFiles, relativePath)) {
                return false;
            }
            return true;
        }

        @Override
        public boolean accept(File file, String s) {
            return accept(new File(file, s));
        }
    }, TrueFileFilter.INSTANCE);
    executorServicesHelper.submitTask(taskName, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            // 得到主bundle的dex diff文件
            // File mianDiffDestDex = new File(mainDiffFolder, DEX_NAME);
            // File tmpDexFolder = new File(patchTmpDir, ((TpatchInput)input).mainBundleName + "-dex");
            createBundleDexPatch(newApkUnzipFolder, baseApkUnzipFolder, mainDiffFolder, // tmpDexFolder,
            true);
            // 是否保留主bundle的资源文件
            if (isRetainMainBundleRes()) {
                copyMainBundleResources(newApkUnzipFolder, baseApkUnzipFolder, new File(patchTmpDir, ((TpatchInput) input).mainBundleName), retainFiles);
            }
            return true;
        }
    });
    for (final File soFile : soFiles) {
        System.out.println("do patch:" + soFile.getAbsolutePath());
        final String relativePath = PathUtils.toRelative(newApkUnzipFolder, soFile.getAbsolutePath());
        if (null != ((TpatchInput) input).notIncludeFiles && pathMatcher.match(((TpatchInput) input).notIncludeFiles, relativePath)) {
            continue;
        }
        executorServicesHelper.submitTask(taskName, new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                File destFile = new File(patchTmpDir, ((TpatchInput) input).mainBundleName + "/" + relativePath);
                File baseSoFile = new File(baseApkUnzipFolder, relativePath);
                if (isBundleFile(soFile)) {
                    processBundleFiles(soFile, baseSoFile, patchTmpDir);
                } else if (isFileModify(soFile, baseSoFile)) {
                    if (destFile.exists()) {
                        FileUtils.deleteQuietly(destFile);
                    }
                    if (!baseSoFile.exists() || !((TpatchInput) input).diffNativeSo) {
                        // 新增
                        FileUtils.copyFile(soFile, destFile);
                    } else {
                        destFile = new File(destFile.getParentFile(), destFile.getName() + ".patch");
                        SoDiffUtils.diffSo(patchTmpDir, baseSoFile, soFile, destFile);
                        soFileDefs.add(new SoFileDef(baseSoFile, soFile, destFile, relativePath));
                    }
                }
                return true;
            }
        });
    }
    executorServicesHelper.waitTaskCompleted(taskName);
    executorServicesHelper.stop();
    Profiler.release();
    Profiler.enter("ziptpatchfile");
    // zip file
    File patchFile = createTPatchFile(((TpatchInput) input).outPatchDir, patchTmpDir);
    tpatchFile.patchFile = patchFile;
    PatchInfo curPatchInfo = createBasePatchInfo(patchFile);
    Profiler.release();
    Profiler.enter("createhistpatch");
    BuildPatchInfos buildPatchInfos = createIncrementPatchFiles(((TpatchInput) input).productName, patchFile, ((TpatchInput) input).outPatchDir, newApkUnzipFolder, curPatchInfo, ((TpatchInput) input).hisPatchUrl);
    Profiler.release();
    Profiler.enter("writejson");
    buildPatchInfos.getPatches().add(curPatchInfo);
    buildPatchInfos.setBaseVersion(input.baseApkBo.getVersionName());
    buildPatchInfos.setDiffBundleDex(input.diffBundleDex);
    FileUtils.writeStringToFile(((TpatchInput) input).outPutJson, JSON.toJSONString(buildPatchInfos));
    BuildPatchInfos testForBuildPatchInfos = new BuildPatchInfos();
    testForBuildPatchInfos.setBaseVersion(buildPatchInfos.getBaseVersion());
    List<PatchInfo> patchInfos = new ArrayList<>();
    testForBuildPatchInfos.setPatches(patchInfos);
    testForBuildPatchInfos.setDiffBundleDex(buildPatchInfos.isDiffBundleDex());
    for (PatchInfo patchInfo : buildPatchInfos.getPatches()) {
        if (patchInfo.getTargetVersion().equals(buildPatchInfos.getBaseVersion())) {
            patchInfos.add(patchInfo);
        }
    }
    FileUtils.writeStringToFile(hisPatchJsonFile, JSON.toJSONString(testForBuildPatchInfos));
    tpatchFile.updateJsons = new ArrayList<File>();
    Map<String, List<String>> map = new HashMap<>();
    for (PatchInfo patchInfo : buildPatchInfos.getPatches()) {
        UpdateInfo updateInfo = new UpdateInfo(patchInfo, buildPatchInfos.getBaseVersion());
        // System.out.println("start to check:"+patchInfo.getTargetVersion()+"......");
        // List<PatchChecker.ReasonMsg> msgs = new PatchChecker(updateInfo,bundleInfos.get(patchInfo.getTargetVersion()),new File(((TpatchInput) input).outPatchDir,patchInfo.getFileName())).check();
        // map.put(patchInfo.getFileName(),msgToString(msgs));
        File updateJson = new File(((TpatchInput) input).outPatchDir, "update-" + patchInfo.getTargetVersion() + ".json");
        FileUtils.writeStringToFile(updateJson, JSON.toJSONString(updateInfo, true));
        tpatchFile.updateJsons.add(updateJson);
    }
    // tpatchFile.patchChecker = new File(((TpatchInput) input).outPatchDir,"patch-check.json");
    // FileUtils.writeStringToFile(tpatchFile.patchChecker, JSON.toJSONString(map, true));
    // 删除临时的目录
    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) input).outPatchDir.getParentFile(), true);
    if (newApkFileExist) {
        FileUtils.copyFileToDirectory(input.newApkBo.getApkFile(), ((TpatchInput) input).outPatchDir.getParentFile(), true);
    }
    Profiler.release();
    logger.warning(Profiler.dump());
    return tpatchFile;
}
Also used : BundleBO(com.taobao.android.tpatch.model.BundleBO) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TpatchFile(com.taobao.android.outputs.TpatchFile) TpatchInput(com.taobao.android.inputs.TpatchInput) RecognitionException(org.antlr.runtime.RecognitionException) PatchException(com.taobao.android.differ.dex.PatchException) ExecutorServicesHelper(com.taobao.android.task.ExecutorServicesHelper) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) TpatchFile(com.taobao.android.outputs.TpatchFile) ZipFile(java.util.zip.ZipFile) PatchFile(com.taobao.android.outputs.PatchFile) UpdateInfo(com.taobao.update.UpdateInfo)

Aggregations

PatchException (com.taobao.android.differ.dex.PatchException)1 TpatchInput (com.taobao.android.inputs.TpatchInput)1 PatchFile (com.taobao.android.outputs.PatchFile)1 TpatchFile (com.taobao.android.outputs.TpatchFile)1 ExecutorServicesHelper (com.taobao.android.task.ExecutorServicesHelper)1 BundleBO (com.taobao.android.tpatch.model.BundleBO)1 UpdateInfo (com.taobao.update.UpdateInfo)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ZipFile (java.util.zip.ZipFile)1 RecognitionException (org.antlr.runtime.RecognitionException)1 IOFileFilter (org.apache.commons.io.filefilter.IOFileFilter)1