Search in sources :

Example 1 with APatchFile

use of com.taobao.android.outputs.APatchFile 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)

Aggregations

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