簡(jiǎn)介
之前的文章寫過關(guān)于Android多渠道的問題--->在這里啊
因?yàn)橹笆切薷腗ETA-INF文件夾寫入渠道信息,但是這種方式會(huì)有問題,因?yàn)锳ndroid7.0的系統(tǒng)使用Signature Scheme v2,所以修改完apk包以后再次安裝的時(shí)候會(huì)提示安裝包未包含任何證書涤妒,從而安裝失敗格二。查了相關(guān)資料看到一個(gè)開源庫(kù)VasDolly。
VasDolly是一種快速多渠道打包工具臣淤,同時(shí)支持基于V1簽名和V2簽名進(jìn)行多渠道打包。插件本身會(huì)自動(dòng)檢測(cè)Apk使用的簽名類別窃爷,并選擇合適的多渠道打包方式邑蒋,對(duì)使用者來說完全透明姓蜂。
項(xiàng)目實(shí)踐
VasDolly提供了兩種打包方式
第一種是Android Studio 的gradle打包
第二種是命令行打包
因?yàn)槲覀兊陌菑姆?wù)器打的渠道包,所以我寫一下命令行打包方式医吊,測(cè)試完成后把命令行的工作遷移到服務(wù)器就可以了钱慢。
- 首先從github下載VasDolly的項(xiàng)目,因?yàn)榭蛻舳酥恍枰x取渠道所以我們只需要common和reader庫(kù)的代碼卿堂,如下圖
image
新建Android項(xiàng)目束莫,將common和reader庫(kù)的代碼拷貝到項(xiàng)目中
新建ChannelReaderUtil類
public class ChannelReaderUtil {
private static final String TAG = "ChannelReaderUtil";
private static String mChannelCache;
public ChannelReaderUtil() {
}
public static String getChannel(Context context) {
if(mChannelCache == null) {
String channel = getChannelByV2(context);
if(channel == null) {
channel = getChannelByV1(context);
}
mChannelCache = channel;
}
return mChannelCache;
}
public static String getChannelByV2(Context context) {
String apkPath = getApkPath(context);
String channel = ChannelReader.getChannel(new File(apkPath));
Log.i("ChannelReaderUtil", "getChannelByV2 , channel = " + channel);
return channel;
}
public static String getChannelByV1(Context context) {
String apkPath = getApkPath(context);
String channel = ChannelReader.getChannelByZipComment(new File(apkPath));
Log.i("ChannelReaderUtil", "getChannelByV1 , channel = " + channel);
return channel;
}
public static String getStringValueById(Context context, int id) {
String apkPath = getApkPath(context);
String value = IdValueReader.getStringValueById(new File(apkPath), id);
Log.i("ChannelReaderUtil", "id = " + id + " , value = " + value);
return value;
}
public static byte[] getByteValueById(Context context, int id) {
String apkPath = getApkPath(context);
return IdValueReader.getByteValueById(new File(apkPath), id);
}
public static Map<Integer, ByteBuffer> getAllIdValueMap(Context context) {
String apkPath = getApkPath(context);
return IdValueReader.getAllIdValueMap(new File(apkPath));
}
private static String getApkPath(Context context) {
String apkPath = null;
try {
ApplicationInfo applicationInfo = context.getApplicationInfo();
if(applicationInfo == null) {
return null;
}
apkPath = applicationInfo.sourceDir;
} catch (Throwable var3) {
var3.printStackTrace();
}
return apkPath;
}
}
- 獲取渠道
String channel = ChannelReaderUtil.getChannel(getApplicationContext());
- 打包測(cè)試
打開VasDolly項(xiàng)目找到 command/jar/VasDolly.jar 執(zhí)行下面的命令
./ 指當(dāng)前目錄
java -jar VasDolly.jar put -c "channel1,channel2" app-release.apk ./