解析apk方法
一、apktool cmd解析
二、解壓apk,讀取AndroidManifest.xml獲取版本信息
由於第一種太麻煩了,要部署到docker麻煩,選用了第二種android
本示例是上傳時,把APK解壓,遍歷文件,若是是AndroidManifest.xml文件就取裏面的值docker
參考這這裏,源碼和jar能夠從這裏下載
https://chensl.iteye.com/blog/2174290spa
public String savePackage(CommonsMultipartFile file, Long pkVersion) throws Exception { if (file == null) { throw new RuntimeException("上傳的文件爲空,請從新上傳!"); } byte[] uploadBytes = file.getBytes(); // 獲取包信息apk信息的返回結果 String[] apkResult = new String[3]; ZipInputStream zs = new ZipInputStream(file.getInputStream()); BufferedInputStream bs = new BufferedInputStream(zs); ZipEntry zipEntry; while ((zipEntry = zs.getNextEntry()) != null) { if (zipEntry.isDirectory()) { continue; } byte[] bytes = null; if ("androidmanifest.xml".equals(zipEntry.getName().toLowerCase())) { bytes = new byte[(int) zipEntry.getSize()]; bs.read(bytes, 0, (int) zipEntry.getSize()); InputStream byteArrayInputStream = new ByteArrayInputStream(bytes); apkResult = AXMLPrinter.getApkInfo(byteArrayInputStream); } } bs.close(); zs.close(); if (StringUtils.isBlank(apkResult[0])) { throw new RuntimeException("未獲取包名,請檢測包"); } //end包 //get包名 String pkPackage=apkResult[0]; String fileName = file.getOriginalFilename(); String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); PackageVer fileInfoModel = new PackageVer(); fileInfoModel.setPkName(fileName); fileInfoModel.setPkSize(file.getSize()); fileInfoModel.setPkVersion(pkVersion); fileInfoModel.setPkPackage(pkPackage); MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] digest = md5.digest(uploadBytes); String hashString = new BigInteger(1, digest).toString(16); fileInfoModel.setPkMd5(hashString); String fileId = fastDfsService.uploadPicFile(uploadBytes, fileSuffix); logger.info("uploadPicFile result={}", fileId); fileInfoModel.setPkUrl(fileUrlPrefix + "/" + fileId); fileInfoModel.setFileid(fileId); packageVerBiz.add(fileInfoModel); return "SUCCESS"; }
public static String[] getApkInfo(InputStream fileInputStream) throws Exception { final String[] apkResult = new String[3]; AXmlResourceParser parser = new AXmlResourceParser(); parser.open(fileInputStream); while (true) { int type = parser.next(); if (type == XmlPullParser.END_DOCUMENT) { break; } switch (type) { case XmlPullParser.START_TAG: { for (int i = 0; i != parser.getAttributeCount(); ++i) { if ("package".equals(parser.getAttributeName(i))) { apkResult[0] = getAttributeValue(parser, i); } else if ("versionCode".equals(parser.getAttributeName(i))) { apkResult[1] = getAttributeValue(parser, i); } else if ("versionName".equals(parser.getAttributeName(i))) { apkResult[2] = getAttributeValue(parser, i); } } break; } default: } } return apkResult; }