Search in sources :

Example 1 with ApatchInput

use of com.taobao.android.inputs.ApatchInput in project atlas by alibaba.

the class APatchTool method doPatch.

@Override
public PatchFile doPatch() throws Exception {
    APatchFile patchFile = new APatchFile();
    ApatchInput apatchInput = (ApatchInput) input;
    mappingMap = apatchInput.mappingMap;
    if (apatchInput.mappingMap.get(apatchInput.replaceAnnotation) != null) {
        MethodReplaceAnnotation.ANNOTATION = apatchInput.mappingMap.get(apatchInput.replaceAnnotation);
    }
    File patchTmpDir = new File(input.outPutFile.getParentFile(), "apatch-tmp");
    patchTmpDir.mkdirs();
    patchFile.aDiffText = new File(input.outPutFile.getParentFile(), "apatch-diff.txt");
    patchFile.diffJson = new File(input.outPutFile.getParentFile(), "apatch-diff.json");
    FileUtils.deleteQuietly(patchFile.aDiffText);
    patchFile.aDiffText.createNewFile();
    // unzip apk
    File unzipFolder = unzipApk(patchTmpDir);
    final File newApkUnzipFolder = new File(unzipFolder, NEW_APK_UNZIP_NAME);
    final File baseApkUnzipFolder = new File(unzipFolder, BASE_APK_UNZIP_NAME);
    // first generate main bundle patch file
    List<File> aPatches = createBundleAPatch(newApkUnzipFolder, baseApkUnzipFolder, patchTmpDir, apatchInput.andfixMainBundleName, patchFile.aDiffText, patchFile.diffJson);
    // second generate common bundle patch file
    // 
    Collection<File> soFiles = FileUtils.listFiles(newApkUnzipFolder, new String[] { "so" }, true);
    if (input.splitDiffBundle != null) {
        for (Pair<BundleBO, BundleBO> bundle : input.splitDiffBundle) {
            if (bundle.getFirst() == null || bundle.getSecond() == null) {
                continue;
            }
            List<File> aPatchFiles = processBundleFiles(bundle.getSecond().getBundleFile(), bundle.getFirst().getBundleFile(), patchTmpDir, patchFile.aDiffText, patchFile.diffJson);
            if (null != aPatchFiles) {
                for (File aPatchFile : aPatchFiles) {
                    if (null != aPatchFile && aPatchFile.exists()) {
                        aPatches.add(aPatchFile);
                    }
                }
            }
        }
    }
    for (File soFile : soFiles) {
        String relativePath = PathUtils.toRelative(newApkUnzipFolder, soFile.getAbsolutePath());
        if (null != apatchInput.notIncludeFiles && pathMatcher.match(apatchInput.notIncludeFiles, relativePath)) {
            continue;
        }
        File baseSoFile = new File(baseApkUnzipFolder, relativePath);
        if (PatchUtils.isBundleFile(soFile)) {
            // if bundle file
            List<File> aPatchFiles = processBundleFiles(soFile, baseSoFile, patchTmpDir, patchFile.aDiffText, patchFile.diffJson);
            if (null != aPatchFiles) {
                for (File aPatchFile : aPatchFiles) {
                    if (null != aPatchFile && aPatchFile.exists()) {
                        aPatches.add(aPatchFile);
                    }
                }
            }
        }
    }
    if (aPatches.size() <= 0) {
        throw new Exception("No apatch files! No classes modify!");
    }
    // merge apatch file
    File[] aPatchFiles = new File[aPatches.size()];
    aPatchFiles = aPatches.toArray(aPatchFiles);
    File mergePatchFile = null;
    if (null != aPatchFiles && aPatchFiles.length > 1) {
        MergePatch mergePatch = new MergePatch(aPatchFiles, apatchInput.projectArtifactId, patchTmpDir);
        mergePatchFile = mergePatch.doMerge();
    } else if (null != aPatchFiles && aPatchFiles.length == 1) {
        mergePatchFile = aPatchFiles[0];
    }
    if (null != mergePatchFile && mergePatchFile.exists()) {
        FileUtils.moveFile(mergePatchFile, input.outPutFile);
    }
    FileUtils.deleteDirectory(unzipFolder);
    FileUtils.deleteDirectory(patchTmpDir);
    patchFile.patchFile = input.outPutFile;
    return patchFile;
}
Also used : BundleBO(com.taobao.android.tpatch.model.BundleBO) ApatchInput(com.taobao.android.inputs.ApatchInput) APatchFile(com.taobao.android.outputs.APatchFile) File(java.io.File) APatchFile(com.taobao.android.outputs.APatchFile) PatchFile(com.taobao.android.outputs.PatchFile) MergePatch(com.taobao.android.apatch.MergePatch) IOException(java.io.IOException) RecognitionException(org.antlr.runtime.RecognitionException) PatchException(com.taobao.android.differ.dex.PatchException)

Example 2 with ApatchInput

use of com.taobao.android.inputs.ApatchInput in project atlas by alibaba.

the class APatchTool method processBundleFiles.

/**
 * @param newBundleFile
 * @param baseBundleFile
 * @param patchTmpDir
 * @return
 */
private List<File> processBundleFiles(File newBundleFile, File baseBundleFile, File patchTmpDir, File diffFile, File diffJsonFile) throws PatchException, IOException, RecognitionException {
    String bundleName = FilenameUtils.getBaseName(newBundleFile.getName());
    if (!isModifyBundle(newBundleFile.getName()) || !((ApatchInput) input).onlyIncludeModifyBundle) {
        return null;
    }
    if (null != logger) {
        logger.info("[DiffApatch]" + bundleName);
    }
    final File newBundleUnzipFolder = new File(newBundleFile.getParentFile(), bundleName);
    final File baseBundleUnzipFolder = new File(baseBundleFile.getParentFile(), bundleName);
    if (null != baseBundleFile && baseBundleFile.isFile() && baseBundleFile.exists()) {
        // unzip apk
        ZipUtils.unzip(newBundleFile, newBundleUnzipFolder.getAbsolutePath());
        ZipUtils.unzip(baseBundleFile, baseBundleUnzipFolder.getAbsolutePath());
        // String patchBundleName = getBundleName(newBundleFile.getName());
        String patchBundleName = FilenameUtils.getBaseName(newBundleFile.getName().replace("lib", ""));
        if (null != patchBundleName) {
            patchBundleName = patchBundleName.replace(".", "_");
            List<File> bundlePatchs = createBundleAPatch(newBundleUnzipFolder, baseBundleUnzipFolder, patchTmpDir, patchBundleName, diffFile, diffJsonFile);
            return bundlePatchs;
        } else {
            throw new PatchException("Cannot found bundle:" + newBundleFile.getName() + " in modifyBundleInfos.");
        }
    }
    return null;
}
Also used : ApatchInput(com.taobao.android.inputs.ApatchInput) PatchException(com.taobao.android.differ.dex.PatchException) File(java.io.File) APatchFile(com.taobao.android.outputs.APatchFile) PatchFile(com.taobao.android.outputs.PatchFile)

Aggregations

PatchException (com.taobao.android.differ.dex.PatchException)2 ApatchInput (com.taobao.android.inputs.ApatchInput)2 APatchFile (com.taobao.android.outputs.APatchFile)2 PatchFile (com.taobao.android.outputs.PatchFile)2 File (java.io.File)2 MergePatch (com.taobao.android.apatch.MergePatch)1 BundleBO (com.taobao.android.tpatch.model.BundleBO)1 IOException (java.io.IOException)1 RecognitionException (org.antlr.runtime.RecognitionException)1