读完这篇文章,你可能会了解到以下几点:
1. 蒲公英为什么只上传 ipa 文件,就可以下载 app
2. Java 解析 ipa 文件 (iOS 应用包)
3. Java 解析 apk 文件 (Android 应用包)
4. 自己上传 app 到服务器,模拟蒲公英的效果
关于蒲公英的思考
蒲公英的作用(在工作中)
- 在我的实际工作中,蒲公英主要用于企业包(In-House证书打的包)的分发,方便 QA 和其他用户测试
- 如果是自己做应用分发(下载),比如把
.ipa
和info.plist
文件 上传到七牛服务器,然后自己制作一个下载页面
为什么蒲公英那么方便?
- 我的想法是:在我们上传
ipa
文件的同时,蒲公英会根据 ipa 文件,读取应用对应的配置文件,获取必要信息 (比如bundleId
),生成对应的info.plist
文件,然后同时上传到服务器,就相当于我们自己手动上传那两个文件一样的效果。
思考:如何获取 ipa 或者 apk 文件的应用的配置文件信息呢?
获取 ipa 文件的配置文件信息
准备工作
- iOS 安装包(.ipa 文件)
- 解析
info.plist
文件所需的 jar 包(点我去下载页)
主要代码
// AppUtil.java 文件
import com.dd.plist.NSDictionary;
import com.dd.plist.NSNumber;
import com.dd.plist.NSObject;
import com.dd.plist.NSString;
import com.dd.plist.PropertyListParser;
/**
* 解析 IPA 文件
* @param is 输入流
* @return Map<String, Object>
*/
public static Map<String, Object> analyzeIpa(InputStream is) {
Map<String, Object> resultMap = new HashMap<>();
try {
ZipInputStream zipIs = new ZipInputStream(is);
ZipEntry ze;
InputStream infoIs = null;
while ((ze = zipIs.getNextEntry()) != null) {
if (!ze.isDirectory()) {
String name = ze.getName();
// 读取 info.plist 文件
// FIXME: 包里可能会有多个 info.plist 文件!!!
if (name.contains(".app/Info.plist")) {
ByteArrayOutputStream abos = new ByteArrayOutputStream();
int chunk = 0;
byte[] data = new byte[256];
while(-1 != (chunk = zipIs.read(data))) {
abos.write(data, 0, chunk);
}
infoIs = new ByteArrayInputStream(abos.toByteArray());
break;
}
}
}
NSDictionary rootDict = (NSDictionary) PropertyListParser.parse(infoIs);
String[] keyArray = rootDict.allKeys();
for (String key : keyArray) {
NSObject value = rootDict.objectForKey(key);
if (key.equals("CFBundleSignature")) {
continue;
}
if (value.getClass().equals(NSString.class) || value.getClass().equals(NSNumber.class)) {
resultMap.put(key, value.toString());
}
}
zipIs.close();
is.close();
} catch (Exception e) {
resultMap.put("error", e.getStackTrace());
}
return resultMap;
}
获取 apk 文件的配置文件信息
准备工作
- Android 安装包(.apk 文件)
- 解析
AndroidManifest.xml
文件所需的 jar 包(点我去下载页)
主要代码
// AppUtil.java 文件
import org.apkinfo.api.util.AXmlResourceParser;
import org.apkinfo.api.util.XmlPullParser;
import org.apkinfo.api.util.XmlPullParserException;
/**
* 解析 APK 文件
* @param is 输入流
* @return Map<String,Map<String,Object>>
*/
public static Map<String,Map<String,Object>> analyzeApk(InputStream is) {
Map<String,Map<String,Object>> resultMap = new HashMap<>();
try {
ZipInputStream zipIs = new ZipInputStream(is);
zipIs.getNextEntry();
AXmlResourceParser parser = new AXmlResourceParser();
parser.open(zipIs);
boolean flag = true;
while(flag) {
int type = parser.next();
if (type == XmlPullParser.START_TAG) {
int count = parser.getAttributeCount();
String action = parser.getName().toUpperCase();
if(action.equals("MANIFEST") || action.equals("APPLICATION")) {
Map<String,Object> tempMap = new HashMap<>();
for (int i = 0; i < count; i++) {
String name = parser.getAttributeName(i);
String value = parser.getAttributeValue(i);
value = (value == null) ? "" : value;
tempMap.put(name, value);
}
resultMap.put(action, tempMap);
} else {
Map<String,Object> manifest = resultMap.get("MANIFEST");
Map<String,Object> application = resultMap.get("APPLICATION");
if(manifest != null && application != null) {
flag = false;
}
continue;
}
}
}
zipIs.close();
is.close();
} catch (ZipException e) {
resultMap.put("error", getError(e));
} catch (IOException e) {
resultMap.put("error", getError(e));
} catch (XmlPullParserException e) {
resultMap.put("error", getError(e));
}
return resultMap;
}
private static Map<String,Object> getError(Exception e) {
Map<String,Object> errorMap = new HashMap<>();
errorMap.put("cause", e.getCause());
errorMap.put("message", e.getMessage());
errorMap.put("stack", e.getStackTrace());
return errorMap;
}
注:以上代码,部分参考自网络。整合之后,亲测,可以正常使用。【20170903】
模拟蒲公英上传 ipa 文件
主要代码
@ResponseBody
@RequestMapping(value = "app/upload", method = RequestMethod.POST)
public Object upload(@RequestParam MultipartFile file, HttpServletRequest request) {
Log.info("上传开始");
int evn = 4;
// 文件上传成功后,返回给前端的 appInfo 对象
AppInfoModel appInfo = new AppInfoModel();
appInfo.setEvn(evn);
String path = request.getSession().getServletContext().getRealPath("upload");
Date now = new Date();
String[] extensions = file.getOriginalFilename().split("\\.");
long time = now.getTime();
String fileNameWithoutExtension = "app_" + evn + "_" + time;
String fileExtension = extensions[extensions.length - 1];
String fileName = fileNameWithoutExtension + "." + fileExtension;
Log.info(path);
try {
File targetFile = new File(path, fileName);
if(!targetFile.exists()) {
targetFile.mkdirs();
}
file.transferTo(targetFile);
InputStream is = new FileInputStream(targetFile);
boolean isIOS = fileExtension.toLowerCase().equals("ipa");
if (isIOS) {
// 获取配置文件信息
Map<String, Object> infoPlist = AppUtil.analyzeIpa(is);
// 获取包名
String packageName = (String) infoPlist.get("CFBundleIdentifier");
appInfo.setBundleId(packageName);
appInfo.setVersionCode((String) infoPlist.get("CFBundleShortVersionString"));
// 这是个私有方法,根据包名获取特定的 app 信息,并设置 appInfo
setupAppInfo(packageName, true, appInfo);
} else if (fileExtension.toLowerCase().equals("apk")) {
// 获取配置文件信息
Map<String,Map<String,Object>> infoConfig = AppUtil.analyzeApk(is);
Map<String, Object> manifestMap = infoConfig.get("MANIFEST");
String packageName = (String) manifestMap.get("package");
appInfo.setBundleId(packageName);
appInfo.setVersionCode((String) manifestMap.get("versionName"));
setupAppInfo(packageName, false, appInfo);
} else {
Map<String, Object> map = new HashMap<>();
map.put("code", NetError.BadRequest.getCode());
map.put("message", "文件格式错误,请重新上传!");
return map;
}
// 上传 FTP
FTPUtil ftp = new FTPUtil(FtpHostName, FtpHostPort, FtpUserName, FtpPassword);
String ftpPath = "/app/" + appInfo.getAppId() + "/" + appInfo.getVersionCode();
FileInputStream in = new FileInputStream(targetFile);
ftp.uploadFile(ftpPath, fileName, in);
targetFile.delete();
String url = ftpPath + "/" + fileName;
if (isIOS) { // iOS 创建 plist 文件
String plistFilName = fileNameWithoutExtension + ".plist";
String plistUrl = path + "/" + plistFilName;
// 创建 info.plist 文件
boolean result = appUploadService.createPlist(plistUrl, nfo.getBundleId(), appInfo.getName(), appInfo.getVersionCode(), url, est.getLocalAddr(), request.getLocalPort());
if (result == false) {
NetError error = NetError.BadRequest;
error.setMessage("创建Plist文件失败");
throw new NetException(error);
}
File targetPlistFile = new File(path, plistFilName);
in = new FileInputStream(targetPlistFile);
ftp.uploadFile(ftpPath, plistFilName, in);
url = ftpPath + "/" + plistFilName;
targetPlistFile.delete();
}
Log.info("上传完成");
final String uploadedUrl = url;
return getResult(new HashMap<String, Object>(){{
put("url", uploadedUrl);
put("appInfo", appInfo);
}});
} catch (Exception e) {
e.printStackTrace();
NetError error = NetError.BadRequest;
error.setMessage(e.toString());
throw new NetException(error);
}
}
Demo

Tips
关于 jar 包下载
- 之前 csdn 上可以上传零积分下载的资源,现在至少是1积分,所以积分不足的同学,可以留下联系方式,私发。
关于 demo
- 由于完整的 demo 涉及到公司项目相关的内容,所以暂不上传,日后整理后再贴出来下载链接。
网友评论