Search in sources :

Example 1 with PatchBundleInfo

use of com.taobao.android.object.PatchBundleInfo in project atlas by alibaba.

the class TPatchTool method createBasePatchInfo.

/**
     * 获取基准patch包的patchInfo对象
     *
     * @param fileName
     * @return
     */
public PatchInfo createBasePatchInfo(String fileName) {
    PatchInfo patchInfo = new PatchInfo();
    patchInfo.setPatchVersion(newApkVersion);
    patchInfo.setTargetVersion(baseApkVersion);
    patchInfo.setFileName(fileName);
    for (ArtifactBundleInfo artifactBundleInfo : artifactBundleInfos) {
        if (artifactBundleInfo.getMainBundle()) {
            if (DiffType.MODIFY.equals(artifactBundleInfo.getDiffType()) || hasMainBundle) {
                PatchBundleInfo patchBundleInfo = new PatchBundleInfo();
                patchBundleInfo.setNewBundle(DiffType.ADD.equals(artifactBundleInfo.getDiffType()));
                patchBundleInfo.setMainBundle(true);
                patchBundleInfo.setVersion(artifactBundleInfo.getVersion());
                patchBundleInfo.setName(mainBundleName);
                patchBundleInfo.setApplicationName(artifactBundleInfo.getApplicationName());
                patchBundleInfo.setArtifactId(artifactBundleInfo.getArtifactId());
                patchBundleInfo.setPkgName(artifactBundleInfo.getPkgName());
                patchBundleInfo.setDependency(artifactBundleInfo.getDependency());
                //                    patchBundleInfo.setBaseVersion(artifactBundleInfo.getBaseVersion());
                patchInfo.getBundles().add(patchBundleInfo);
                continue;
            }
        } else if (DiffType.MODIFY.equals(artifactBundleInfo.getDiffType()) || DiffType.ADD.equals(artifactBundleInfo.getDiffType())) {
            PatchBundleInfo patchBundleInfo = new PatchBundleInfo();
            patchBundleInfo.setNewBundle(DiffType.ADD.equals(artifactBundleInfo.getDiffType()));
            patchBundleInfo.setMainBundle(false);
            patchBundleInfo.setVersion(artifactBundleInfo.getVersion());
            patchBundleInfo.setName(artifactBundleInfo.getName());
            patchBundleInfo.setApplicationName(artifactBundleInfo.getApplicationName());
            patchBundleInfo.setArtifactId(artifactBundleInfo.getArtifactId());
            patchBundleInfo.setPkgName(artifactBundleInfo.getPkgName());
            patchBundleInfo.setDependency(artifactBundleInfo.getDependency());
            //                patchBundleInfo.setBaseVersion(artifactBundleInfo.getBaseVersion());
            patchInfo.getBundles().add(patchBundleInfo);
        }
    }
    return patchInfo;
}
Also used : PatchBundleInfo(com.taobao.android.object.PatchBundleInfo) ArtifactBundleInfo(com.taobao.android.object.ArtifactBundleInfo) PatchInfo(com.taobao.android.object.PatchInfo)

Example 2 with PatchBundleInfo

use of com.taobao.android.object.PatchBundleInfo in project atlas by alibaba.

the class UpdateTest method test.

@Test
public void test() throws IOException {
    //        System.out.println(123);
    File file = new File(getClass().getClassLoader().getResource("patchs.json").getFile());
    String json = FileUtils.readFileToString(file);
    BuildPatchInfos patchInfos = JSON.parseObject(json, BuildPatchInfos.class);
    UpdateInfo updateInfo = new UpdateInfo();
    updateInfo.baseVersion = patchInfos.getBaseVersion();
    updateInfo.updateVersion = patchInfos.getPatches().get(0).getPatchVersion();
    PatchInfo patchInfo = patchInfos.getPatches().get(0);
    List<UpdateInfo.Item> items = new ArrayList<UpdateInfo.Item>();
    for (PatchBundleInfo patchBundleInfo : patchInfo.getBundles()) {
        UpdateInfo.Item item = new UpdateInfo.Item();
        items.add(item);
        item.dependency = patchBundleInfo.getDependency();
        item.isMainDex = patchBundleInfo.getMainBundle();
        item.name = patchBundleInfo.getPkgName();
        //            item.srcVersion = patchBundleInfo.getVersion();
        item.version = updateInfo.baseVersion + "@" + patchBundleInfo.getVersion();
    }
    updateInfo.updateBundles = items;
    System.out.println(updateInfo);
}
Also used : PatchBundleInfo(com.taobao.android.object.PatchBundleInfo) PatchInfo(com.taobao.android.object.PatchInfo) ArrayList(java.util.ArrayList) File(java.io.File) BuildPatchInfos(com.taobao.android.object.BuildPatchInfos) Test(org.junit.Test)

Example 3 with PatchBundleInfo

use of com.taobao.android.object.PatchBundleInfo in project atlas by alibaba.

the class PatchFileBuilder method diffPatch.

/**
     * 比较2个patch版本之间的差异
     *
     * @param hisPatchInfo
     * @param currentPatchInfo
     * @return
     */
private List<BundlePatch> diffPatch(PatchInfo hisPatchInfo, PatchInfo currentPatchInfo) {
    List<BundlePatch> list = new LinkedList<BundlePatch>();
    Map<String, PatchBundleInfo> hisBundles = toMap(hisPatchInfo.getBundles());
    Map<String, PatchBundleInfo> curBundles = toMap(currentPatchInfo.getBundles());
    for (Map.Entry<String, PatchBundleInfo> entry : curBundles.entrySet()) {
        String bundleName = entry.getKey();
        PatchBundleInfo curBundleInfo = entry.getValue();
        BundlePatch bundlePatch = new BundlePatch();
        bundlePatch.name = curBundleInfo.getName();
        bundlePatch.dependency = curBundleInfo.getDependency();
        bundlePatch.pkgName = curBundleInfo.getPkgName();
        bundlePatch.artifactId = curBundleInfo.getArtifactId();
        bundlePatch.version = curBundleInfo.getVersion();
        bundlePatch.newBundle = curBundleInfo.getNewBundle();
        bundlePatch.hisPatchUrl = hisPatchInfo.getDownloadUrl();
        bundlePatch.mainBundle = curBundleInfo.getMainBundle();
        bundlePatch.baseVersion = curBundleInfo.getBaseVersion();
        if (hisBundles.containsKey(bundleName)) {
            // 如果之前的patch版本也包含这个bundle的patch
            PatchBundleInfo hisBundleInfo = hisBundles.get(bundleName);
            bundlePatch.baseVersion = hisBundleInfo.getVersion();
            if (curBundleInfo.getVersion().equalsIgnoreCase(hisBundleInfo.getVersion())) {
                // 如果2个patch版本没变化
                // 说明:为了防止虽然版本号没变化但是文件内容也有变化的情况,直接也做merge操作
                bundlePatch.bundlePolicy = BundlePolicy.MERGE;
            } else {
                // 如果版本有变化,进行merge操作
                bundlePatch.bundlePolicy = BundlePolicy.MERGE;
            }
            hisBundles.remove(bundleName);
        } else {
            // 如果历史的patch中没有包含该bundle
            bundlePatch.bundlePolicy = BundlePolicy.ADD;
        }
        list.add(bundlePatch);
    }
    // 继续添加在历史版本里有变动的,在新的版本进行回滚的操作
    for (Map.Entry<String, PatchBundleInfo> entry : hisBundles.entrySet()) {
        PatchBundleInfo hisBundleInfo = entry.getValue();
        BundlePatch bundlePatch = new BundlePatch();
        bundlePatch.name = hisBundleInfo.getName();
        bundlePatch.dependency = hisBundleInfo.getDependency();
        bundlePatch.pkgName = hisBundleInfo.getPkgName();
        bundlePatch.artifactId = hisBundleInfo.getArtifactId();
        bundlePatch.bundlePolicy = BundlePolicy.ROLLBACK;
        bundlePatch.version = ROLLBACK_VERSION;
        bundlePatch.baseVersion = hisBundleInfo.getVersion();
        list.add(bundlePatch);
    }
    return list;
}
Also used : PatchBundleInfo(com.taobao.android.object.PatchBundleInfo)

Example 4 with PatchBundleInfo

use of com.taobao.android.object.PatchBundleInfo in project atlas by alibaba.

the class PatchFileBuilder method processBundlePatch.

/**
     * 处理各自bundle的patch文件
     *
     * @param hisPatchInfo
     * @param bundlePatchs
     */
private PatchInfo processBundlePatch(PatchInfo hisPatchInfo, List<BundlePatch> bundlePatchs, File curTPatchUnzipFolder) throws IOException, PatchException {
    String patchName = "patch-" + currentBuildPatchInfo.getPatchVersion() + "@" + hisPatchInfo.getPatchVersion();
    PatchInfo patchInfo = new PatchInfo();
    patchInfo.setFileName(patchName + ".tpatch");
    patchInfo.setTargetVersion(hisPatchInfo.getPatchVersion());
    patchInfo.setPatchVersion(currentBuildPatchInfo.getPatchVersion());
    File hisTPatchFile = new File(tPatchTmpFolder, hisPatchInfo.getPatchVersion() + "-download.tpatch");
    File hisTPatchUnzipFolder = new File(tPatchTmpFolder, hisPatchInfo.getPatchVersion());
    File destTPathTmpFolder = new File(tPatchTmpFolder, patchName);
    if (!destTPathTmpFolder.exists()) {
        destTPathTmpFolder.mkdirs();
    }
    for (BundlePatch bundlePatch : bundlePatchs) {
        boolean addToPatch = true;
        String bundleName = "lib" + bundlePatch.pkgName.replace('.', '_');
        if (bundlePatch.mainBundle) {
            continue;
        } else if (noPatchBundles.contains(bundlePatch.pkgName)) {
            File currentBundle = new File(curTPatchUnzipFolder, "lib" + bundlePatch.pkgName.replace(".", "_") + ".so");
            if (!currentBundle.exists()) {
                continue;
            }
            FileUtils.copyFileToDirectory(currentBundle, destTPathTmpFolder);
            PatchBundleInfo patchBundleInfo = new PatchBundleInfo();
            patchBundleInfo.setApplicationName(bundlePatch.applicationName);
            patchBundleInfo.setArtifactId(bundlePatch.artifactId);
            patchBundleInfo.setMainBundle(false);
            patchBundleInfo.setNewBundle(bundlePatch.newBundle);
            patchBundleInfo.setName(bundleName);
            patchBundleInfo.setPkgName(bundlePatch.pkgName);
            patchBundleInfo.setVersion(bundlePatch.version);
            patchBundleInfo.setBaseVersion(bundlePatch.baseVersion);
            patchBundleInfo.setDependency(bundlePatch.dependency);
            patchInfo.getBundles().add(patchBundleInfo);
            continue;
        }
        File curBundleFolder = new File(curTPatchUnzipFolder, bundleName);
        File bundleDestFolder = new File(destTPathTmpFolder, bundleName);
        PatchBundleInfo patchBundleInfo = new PatchBundleInfo();
        patchBundleInfo.setApplicationName(bundlePatch.applicationName);
        patchBundleInfo.setArtifactId(bundlePatch.artifactId);
        patchBundleInfo.setMainBundle(false);
        patchBundleInfo.setNewBundle(bundlePatch.newBundle);
        patchBundleInfo.setName(bundleName);
        patchBundleInfo.setPkgName(bundlePatch.pkgName);
        patchBundleInfo.setVersion(bundlePatch.version);
        patchBundleInfo.setBaseVersion(bundlePatch.baseVersion);
        patchBundleInfo.setDependency(bundlePatch.dependency);
        switch(bundlePatch.bundlePolicy) {
            case ADD:
                bundleDestFolder.mkdirs();
                FileUtils.copyDirectory(curBundleFolder, bundleDestFolder);
                break;
            case REMOVE:
                addToPatch = false;
                // donothing
                break;
            case // donothing
            ROLLBACK:
                break;
            case MERGE:
                downloadTPathAndUnzip(hisPatchInfo.getDownloadUrl(), hisTPatchFile, hisTPatchUnzipFolder);
                File hisBundleFolder = new File(hisTPatchUnzipFolder, bundleName);
                if (!hisBundleFolder.exists()) {
                    //如果历史的文件不存在,就直接覆盖
                    throw new PatchException("The bundle:" + bundleName + " does not existed in tpatch:" + hisPatchInfo.getDownloadUrl());
                //                        bundleDestFolder.mkdirs();
                //                        FileUtils.copyDirectory(curBundleFolder, bundleDestFolder);
                } else {
                    File fullAwbFile = awbMaps.get(bundlePatch.artifactId);
                    copyDiffFiles(fullAwbFile, curBundleFolder, hisBundleFolder, bundleDestFolder);
                    if (!bundleDestFolder.exists() || bundleDestFolder.listFiles().length == 0) {
                        addToPatch = false;
                    }
                }
                break;
        }
        if (addToPatch) {
            patchInfo.getBundles().add(patchBundleInfo);
        }
    }
    return patchInfo;
}
Also used : PatchBundleInfo(com.taobao.android.object.PatchBundleInfo) PatchInfo(com.taobao.android.object.PatchInfo) PatchException(com.taobao.android.differ.dex.PatchException)

Example 5 with PatchBundleInfo

use of com.taobao.android.object.PatchBundleInfo in project atlas by alibaba.

the class PatchFileBuilder method createHisTPatch.

/**
     * 创建指定版本的patch文件
     *
     * @param targetVersion
     */
private PatchInfo createHisTPatch(String targetVersion, ILogger logger) throws IOException, PatchException {
    PatchInfo hisPatchInfo = hisPatchInfos.get(targetVersion);
    String patchName = "patch-" + currentBuildPatchInfo.getPatchVersion() + "@" + hisPatchInfo.getPatchVersion();
    File destTPathTmpFolder = new File(tPatchTmpFolder, patchName);
    destTPathTmpFolder.mkdirs();
    File curTPatchUnzipFolder = unzipCurTPatchFolder(patchName);
    // 处理awb的更新
    List<BundlePatch> bundlePatches = diffPatch(hisPatchInfo, currentBuildPatchInfo);
    PatchInfo newPatchInfo = processBundlePatch(hisPatchInfo, bundlePatches, curTPatchUnzipFolder);
    // 比对主bundle的信息
    PatchBundleInfo curMainBundleInfo = getMainBundleInfo(currentBuildPatchInfo);
    PatchBundleInfo hisMainBundleInfo = getMainBundleInfo(hisPatchInfo);
    PatchBundleInfo mainBundleInfo = new PatchBundleInfo();
    mainBundleInfo.setMainBundle(true);
    mainBundleInfo.setNewBundle(false);
    if (null != curMainBundleInfo) {
        File mainBundleFile = new File(curTPatchUnzipFolder, curMainBundleInfo.getName() + ".so");
        FileUtils.copyFileToDirectory(mainBundleFile, destTPathTmpFolder);
        mainBundleInfo.setVersion(curMainBundleInfo.getVersion());
        mainBundleInfo.setPkgName(curMainBundleInfo.getPkgName());
        mainBundleInfo.setBaseVersion(curMainBundleInfo.getBaseVersion());
        mainBundleInfo.setName(curMainBundleInfo.getName());
        mainBundleInfo.setPkgName(curMainBundleInfo.getPkgName());
        mainBundleInfo.setApplicationName(curMainBundleInfo.getApplicationName());
        newPatchInfo.getBundles().add(mainBundleInfo);
    }
    // 生成tpatch文件
    for (PatchBundleInfo bundleInfo : newPatchInfo.getBundles()) {
        if (bundleInfo.getMainBundle() || bundleInfo.getNewBundle() || noPatchBundles.contains(bundleInfo.getPkgName())) {
            File bundleFolder = new File(destTPathTmpFolder, bundleInfo.getName());
            File soFile = new File(destTPathTmpFolder, bundleInfo.getName() + ".so");
            if (soFile.exists() || bundleInfo.getVersion().equals(ROLLBACK_VERSION)) {
                continue;
            }
            zipBundleSo(bundleFolder, soFile);
            FileUtils.deleteDirectory(bundleFolder);
        }
    }
    File tPatchFile = new File(patchsFolder, newPatchInfo.getFileName());
    if (tPatchFile.exists())
        FileUtils.deleteQuietly(tPatchFile);
    zipBundleSo(destTPathTmpFolder, tPatchFile);
    if (null != logger) {
        logger.info("[TPatchFile]" + tPatchFile.getAbsolutePath());
    }
    return newPatchInfo;
}
Also used : PatchBundleInfo(com.taobao.android.object.PatchBundleInfo) PatchInfo(com.taobao.android.object.PatchInfo)

Aggregations

PatchBundleInfo (com.taobao.android.object.PatchBundleInfo)5 PatchInfo (com.taobao.android.object.PatchInfo)4 PatchException (com.taobao.android.differ.dex.PatchException)1 ArtifactBundleInfo (com.taobao.android.object.ArtifactBundleInfo)1 BuildPatchInfos (com.taobao.android.object.BuildPatchInfos)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1