fix(packages): 修复 XAPK 解析兼容性并补充本地化名称
- 忽略 manifest.json 中未声明字段,避免反序列化失败 - 直接解压 base APK 字节,移除对 ContainerEntry 的依赖 - 主 APK 解析失败时降级使用容器 JSON 信息 - 记录解析异常日志,便于排查位置越界问题 - XapkJsonInfo 新增 locales_name 字段
This commit is contained in:
@@ -15,6 +15,7 @@ import net.dongliu.apk.parser.ApkFile;
|
|||||||
import net.dongliu.apk.parser.bean.ApkMeta;
|
import net.dongliu.apk.parser.bean.ApkMeta;
|
||||||
import net.dongliu.apk.parser.bean.Icon;
|
import net.dongliu.apk.parser.bean.Icon;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.glassfish.jaxb.core.v2.TODO;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.zeroturnaround.zip.ZipUtil;
|
import org.zeroturnaround.zip.ZipUtil;
|
||||||
@@ -45,7 +46,10 @@ import java.util.zip.ZipInputStream;
|
|||||||
*/
|
*/
|
||||||
public class ApkMetaParser {
|
public class ApkMetaParser {
|
||||||
private static final String ANDROID_NS = "http://schemas.android.com/apk/res/android";
|
private static final String ANDROID_NS = "http://schemas.android.com/apk/res/android";
|
||||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
|
||||||
|
// manifest.json / info.json 可能包含本类未声明的额外字段(如 locales_name),
|
||||||
|
// 忽略未知字段避免反序列化失败
|
||||||
|
.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ApkMetaParser.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ApkMetaParser.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -470,9 +474,7 @@ public class ApkMetaParser {
|
|||||||
throw new IOException("容器内未找到 manifest.json 文件: " + xapkFile.getName());
|
throw new IOException("容器内未找到 manifest.json 文件: " + xapkFile.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
XapkContent content = readXapkContent(xapkFile);
|
byte[] manifestJson = ZipUtil.unpackEntry(xapkFile, "manifest.json");
|
||||||
List<ContainerEntry> apks = content.apks;
|
|
||||||
byte[] manifestJson = content.manifestJson;
|
|
||||||
|
|
||||||
XapkJsonInfo xapkJsonInfo = OBJECT_MAPPER.readValue(manifestJson, XapkJsonInfo.class);
|
XapkJsonInfo xapkJsonInfo = OBJECT_MAPPER.readValue(manifestJson, XapkJsonInfo.class);
|
||||||
|
|
||||||
@@ -498,25 +500,23 @@ public class ApkMetaParser {
|
|||||||
apkMetaResult.setSplitApkList(fileList);
|
apkMetaResult.setSplitApkList(fileList);
|
||||||
|
|
||||||
SplitApks baseApk = splitApksList.stream().filter(splitApks -> splitApks.getId().equals("base")).findFirst().orElse(null);
|
SplitApks baseApk = splitApksList.stream().filter(splitApks -> splitApks.getId().equals("base")).findFirst().orElse(null);
|
||||||
|
|
||||||
if (baseApk == null) {
|
if (baseApk == null) {
|
||||||
throw new IOException("容器内未找到 base apk 文件: " + xapkFile.getName());
|
throw new IOException("容器内未找到 base apk 文件: " + xapkFile.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
ContainerEntry baseApkEntry = apks.stream().filter(containerEntry -> containerEntry.name.equals(baseApk.getFile())).findFirst().orElse(null);
|
LOGGER.info("baseApk info = " + baseApk);
|
||||||
if (baseApkEntry == null) {
|
|
||||||
throw new IOException("容器内未找到 base apk 文件: " + xapkFile.getName());
|
byte[] baseApkBytes = ZipUtil.unpackEntry(xapkFile, baseApk.getFile());
|
||||||
}
|
|
||||||
|
|
||||||
// 2) 若经验法则未命中,兜底:逐个尝试解析,第一个拿到 packageName 的即为主包
|
// 2) 若经验法则未命中,兜底:逐个尝试解析,第一个拿到 packageName 的即为主包
|
||||||
ApkMetaResult result = tryParseApkBytes(baseApkEntry.data);
|
ApkMetaResult result = tryParseApkBytes(baseApkBytes);
|
||||||
if (result == null) {
|
if (result != null) {
|
||||||
throw new IOException("容器内未找到 base apk 文件: " + xapkFile.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 主 APK 解析出的字段优先(覆盖容器 JSON 中的值)
|
// 主 APK 解析出的字段优先(覆盖容器 JSON 中的值)
|
||||||
overrideIfDifferent(apkMetaResult, result);
|
overrideIfDifferent(apkMetaResult, result);
|
||||||
apkMetaResult.setCompileSdk(result.getCompileSdk());
|
apkMetaResult.setCompileSdk(result.getCompileSdk());
|
||||||
apkMetaResult.setAbiList(result.getAbiList());
|
apkMetaResult.setAbiList(result.getAbiList());
|
||||||
|
}
|
||||||
|
|
||||||
return apkMetaResult;
|
return apkMetaResult;
|
||||||
}
|
}
|
||||||
@@ -766,6 +766,8 @@ public class ApkMetaParser {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
// TODO 2026.08.15 有些会报错 newPosition > limit: (524301 > 228)
|
||||||
|
LOGGER.error("tryParseApkBytes error", e);
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
if (tmp != null && tmp.exists()) {
|
if (tmp != null && tmp.exists()) {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
|
||||||
@@ -16,6 +17,10 @@ public class XapkJsonInfo implements Serializable {
|
|||||||
private int xapk_version;
|
private int xapk_version;
|
||||||
private String package_name;
|
private String package_name;
|
||||||
private String name;
|
private String name;
|
||||||
|
/**
|
||||||
|
* 各语言代码到本地化应用名的映射(如 "zh-CN": "GitHub")。
|
||||||
|
*/
|
||||||
|
private Map<String, String> locales_name;
|
||||||
private long version_code;
|
private long version_code;
|
||||||
private String version_name;
|
private String version_name;
|
||||||
private int min_sdk_version;
|
private int min_sdk_version;
|
||||||
|
|||||||
Reference in New Issue
Block a user