ChannelUtil.java
2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.cnlive.goldenline.util;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.support.annotation.NonNull;
import com.cnlive.goldenline.BuildConfig;
import com.orhanobut.logger.Logger;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Created by zuoxian on 15/9/7.
*/
public class ChannelUtil {
private static final String CHANGE_TAG = "cztchannel_";
/**
* 从apk中获取版本信息
* @param context
* @return
*/
@NonNull
public static String getChannelFromApk(@NonNull Context context) {
//从apk包中获取
ApplicationInfo appinfo = context.getApplicationInfo();
String sourceDir = appinfo.sourceDir;
//注意这里:默认放在meta-inf/里, 所以需要再拼接一下
String key = "META-INF/" + CHANGE_TAG;
String ret = "";
ZipFile zipfile = null;
try {
zipfile = new ZipFile(sourceDir);
Enumeration<?> entries = zipfile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
String entryName = entry.getName();
if (entryName.startsWith(key)) {
ret = entryName;
break;
}
}
} catch (IOException e) {
Logger.e(e, e.getMessage());
} finally {
if (zipfile != null) {
try {
zipfile.close();
System.gc();
} catch (IOException e) {
e.printStackTrace();
}
}
if(BuildConfig.DEBUG){
ret = "cztchannel_20151031";
}
}
String[] split = ret.split("_");
String channel = "debug";
if (split != null && split.length >= 2) {
channel = ret.substring(split[0].length() + 1);
}
Logger.e("frmId:" + channel);
return channel;
}
}