直接使用加压工具打开apk 在META-INF/channel_渠道信息 就可以改变渠道 一个apk
ChannelUtils
import android.content.Context;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.text.TextUtils;import android.util.Log;import java.io.IOException;import java.util.Enumeration;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;public class ChannelUtils {private static final String TAG = "ChannelUtils";public static String getChannel(Context context){String encrypteCN = "";try{encrypteCN = SyhdEncrypt.Encrypt(getSDKChannel(context), SyhdEncrypt.SYHD_ENCRPYT_PWD);Log.i(TAG,"encrypteCN = "+encrypteCN);} catch(Exception e){Log.i(TAG, "login e = "+e.getMessage());}return encrypteCN;}public static String getSDKChannel(Context context){return TextUtils.isEmpty(ChannelUtils.getChannelName(context))? getAppChannel(context):ChannelUtils.getChannelName(context);}public static String getAppChannel(Context context) {String channelName = null;try {String pkgName = context.getPackageName();PackageManager manager = context.getPackageManager();ApplicationInfo appInfo = manager.getApplicationInfo(pkgName, PackageManager.GET_META_DATA);channelName = appInfo.metaData.getString("CHANNEL");} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();} finally {if (null == channelName || channelName.equals("MTest")) {channelName = "test";}}Log.i("-ChannelName-",channelName);return channelName;}public static String getChannelName(Context context) {//从apk包中获取ApplicationInfo appInfo = context.getApplicationInfo();String sourceDir = appInfo.sourceDir;//注意这里:默认放在meta-inf/里, 所以需要再拼接一下String key = "META-INF/channel";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) {e.printStackTrace();Log.i("-ChannelName-",e.getMessage());} finally {if (zipfile != null) {try {zipfile.close();} catch (IOException e) {e.printStackTrace();}}}String[] split = ret.split("_");String channel = "";if (split != null && split.length >= 2) {channel = ret.substring(split[0].length() + 1);}Log.i("-ChannelName-",channel);return channel;}}
SyhdEncrypt
import android.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class SyhdEncrypt {
final public static String SYHD_ENCRPYT_PWD = "2AUxV49EGeJTwvLc";
public static String Encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
return null;
}
if (sKey.length() != 16) {
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
//return new String(Base64.encode(encrypted,Base64.URL_SAFE|Base64.NO_WRAP|Base64.NO_PADDING));
return new String(Base64.encode(encrypted,Base64.DEFAULT)).replaceAll("[\\s*\t\n\r]", "");
}
}
在文件META-INF/中添加渠道信息导致无法安装
原因 使用v2签名对apk文件修改后需要重签名
