Search in sources :

Example 1 with ApkFileList

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

the class TPatchTask method doTPatch.

@TaskAction
public void doTPatch() throws Exception {
    patchContext = getPatchContext();
    signingConfig = getSigningConfig();
    outPatchFolder = getOutPatchFolder();
    // 获取容器版本
    String baseApkVersion = patchContext.getBaseVersionName();
    String newApkVersion = patchContext.versionName;
    File baseApk = patchContext.getBaseApk();
    File newApk = patchContext.diffApkFile;
    boolean retainMainBundleRes = true;
    if (null == newApk || !newApk.exists()) {
        newApk = patchContext.newApk;
        retainMainBundleRes = false;
    }
    getLogger().info("BaseApk:" + baseApk + ",baseVersion:" + baseApkVersion + ",newApk:" + newApk + ",newApkVersion:" + newApkVersion);
    TPatchTool tPatchTool = new TPatchTool(baseApk, newApk, baseApkVersion, newApkVersion, patchContext.diffBundleDex);
    tPatchTool.setMainBundleName(patchContext.mainBundleName);
    if (StringUtils.isNotBlank(patchContext.excludeFiles)) {
        tPatchTool.setNotIncludeFiles(patchContext.excludeFiles.split(","));
    }
    tPatchTool.setRetainMainBundleRes(retainMainBundleRes);
    if (null != patchContext.artifactBundleInfos) {
        tPatchTool.setArtifactBundleInfos(patchContext.artifactBundleInfos);
    }
    tPatchTool.setBaseApkFileList(patchContext.getBaseApkFiles());
    tPatchTool.setNewApkFileList(patchContext.getNewApkFiles());
    tPatchTool.setLogger(getILogger());
    tPatchTool.setOnlyIncludeModifyBundle(patchContext.onlyBuildModifyAwb);
    tPatchTool.setNoPatchBundles(patchContext.notPatchBundles);
    ApkFileList apkFileList = AtlasBuildContext.finalApkFileList;
    try {
        tPatchTool.setDexcode(apkFileList.getMainBundle().get("classes.dex"));
        FileUtils.writeStringToFile(new File(getOutPatchFolder(), "tpatch-bundles.json"), JSON.toJSONString(patchContext.artifactBundleInfos));
        getLogger().info("start to do patch");
        tPatchTool.doPatch(outPatchFolder, true, new File(getOutPatchFolder(), "patchs.json"), StringUtils.isNotEmpty(patchContext.tpatchHistoryUrl), patchContext.tpatchHistoryUrl, patchContext.appSignName);
        getLogger().info("finish  do patch");
        File patchJson = new File(getOutPatchFolder(), "patchs.json");
        File updateJson = new File(getOutPatchFolder(), "update.json");
        String json = FileUtils.readFileToString(patchJson);
        BuildPatchInfos patchInfos = JSON.parseObject(json, BuildPatchInfos.class);
        UpdateInfo updateInfo = new UpdateInfo(patchInfos);
        FileUtils.writeStringToFile(updateJson, JSON.toJSONString(updateInfo, true));
    } catch (Exception e) {
        e.printStackTrace();
        throw new StopExecutionException(e.getMessage());
    }
    File baseVesrionApk = new File(patchContext.newApk.getParentFile(), patchContext.newApk.getName().replace(".apk", "-" + baseApkVersion + ".apk"));
    FileUtils.copyFile(patchContext.getBaseApk(), baseVesrionApk);
    if (patchContext.writeBuildInfo && StringUtils.isNotEmpty(patchContext.buildId)) {
        File buildFile = new File(getOutPatchFolder(), "build.txt");
        FileUtils.writeStringToFile(buildFile, patchContext.buildId + "," + patchContext.versionName + "," + apkFileList.getMainBundle().get("classes.dex"));
        if (buildFile != null && buildFile.exists()) {
            getLogger().debug("write build to apk!");
            BuildHelper.writeFileToApk(buildFile, baseVesrionApk, "assets/build.txt");
        }
        BuildHelper.reSign(baseVesrionApk, signingConfig);
    }
    FileUtils.forceDelete(patchContext.newApk);
}
Also used : StopExecutionException(org.gradle.api.tasks.StopExecutionException) ApkFileList(com.taobao.android.object.ApkFileList) File(java.io.File) TPatchTool(com.taobao.android.TPatchTool) BuildPatchInfos(com.taobao.android.object.BuildPatchInfos) UpdateInfo(com.taobao.android.builder.tools.update.UpdateInfo) DocumentException(org.dom4j.DocumentException) StopExecutionException(org.gradle.api.tasks.StopExecutionException) IOException(java.io.IOException) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

TPatchTool (com.taobao.android.TPatchTool)1 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)1 UpdateInfo (com.taobao.android.builder.tools.update.UpdateInfo)1 ApkFileList (com.taobao.android.object.ApkFileList)1 BuildPatchInfos (com.taobao.android.object.BuildPatchInfos)1 File (java.io.File)1 IOException (java.io.IOException)1 DocumentException (org.dom4j.DocumentException)1 StopExecutionException (org.gradle.api.tasks.StopExecutionException)1 TaskAction (org.gradle.api.tasks.TaskAction)1