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;
}
Aggregations