安卓apk 脚本判断32位和64位兼容,还是纯64位_apk打包兼容32位和64位的-CSDN博客
转java代码:
package cn.silence;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ApkProcessor {
public static void main(String[] args) {
String apkFilePath = "C:\\Users\\12095\\Desktop\\apk\\20241008082912.apk"; // APK文件路径
String zipFilePath = "C:\\Users\\12095\\Desktop\\apk\\20241008082912.zip"; // 输出的ZIP文件路径
String targetDir = "lib/"; // 特定子目录的路径
try {
// 步骤1:将APK转换为ZIP
convertApkToZip(apkFilePath, zipFilePath);
System.out.println("转换完成");
// 步骤2:在ZIP中查找子目录
Set<String> uniqueSubDirs = findSubDirsInZip(zipFilePath, targetDir);
// 步骤3:根据子目录数量复制并重命名APK文件
copyAndRenameApk(apkFilePath, uniqueSubDirs.size());
} catch (IOException e) {
e.printStackTrace();
}
}
private static void convertApkToZip(String apkFilePath, String zipFilePath) throws IOException {
try (ZipInputStream apkZip = new ZipInputStream(new FileInputStream(apkFilePath));
ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
ZipEntry entry;
byte[] buffer = new byte[1024];
int bytesRead;
while ((entry = apkZip.getNextEntry()) != null) {
zipOutput.putNextEntry(new ZipEntry(entry.getName()));
while ((bytesRead = apkZip.read(buffer)) != -1) {
zipOutput.write(buffer, 0, bytesRead);
}
zipOutput.closeEntry();
}
}
}
private static Set<String> findSubDirsInZip(String zipFilePath, String targetDir) throws IOException {
Set<String> subDirs = new HashSet<>();
try (ZipInputStream zipInput = new ZipInputStream(new FileInputStream(zipFilePath))) {
ZipEntry entry;
while ((entry = zipInput.getNextEntry()) != null) {
String entryName = entry.getName();
if (entryName.startsWith(targetDir) && entryName.substring(targetDir.length()).contains("/")) {
String subDirName = entryName.substring(targetDir.length()).split("/")[0];
subDirs.add(subDirName);
}
}
}
System.out.println("亲儿子目录数量: " + subDirs.size() + ", 亲儿子目录名称: " + String.join(", ", subDirs));
return subDirs;
}
private static void copyAndRenameApk(String sourceApkPath, int subDirCount) throws IOException {
String targetDirectory = "C:\\Users\\12095\\Desktop\\apk\\"; // 目标目录
String targetFileName = (subDirCount == 2) ? "32位_64位.apk" : "64位.apk"; // 根据子目录数量设置文件名
Path targetApkPath = Paths.get(targetDirectory, targetFileName);
Files.copy(Paths.get(sourceApkPath), targetApkPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("文件已复制并重命名为: " + targetApkPath);
}
}