直接使用加压工具打开apk 在META-INF/channel_渠道信息 就可以改变渠道 一个apk

ChannelUtils

  1. import android.content.Context;
  2. import android.content.pm.ApplicationInfo;
  3. import android.content.pm.PackageManager;
  4. import android.text.TextUtils;
  5. import android.util.Log;
  6. import java.io.IOException;
  7. import java.util.Enumeration;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipFile;
  10. public class ChannelUtils {
  11. private static final String TAG = "ChannelUtils";
  12. public static String getChannel(Context context){
  13. String encrypteCN = "";
  14. try{
  15. encrypteCN = SyhdEncrypt.Encrypt(getSDKChannel(context), SyhdEncrypt.SYHD_ENCRPYT_PWD);
  16. Log.i(TAG,"encrypteCN = "+encrypteCN);
  17. } catch(Exception e){
  18. Log.i(TAG, "login e = "+e.getMessage());
  19. }
  20. return encrypteCN;
  21. }
  22. public static String getSDKChannel(Context context){
  23. return TextUtils.isEmpty(ChannelUtils.getChannelName(context))? getAppChannel(context):ChannelUtils.getChannelName(context);
  24. }
  25. public static String getAppChannel(Context context) {
  26. String channelName = null;
  27. try {
  28. String pkgName = context.getPackageName();
  29. PackageManager manager = context.getPackageManager();
  30. ApplicationInfo appInfo = manager.getApplicationInfo(
  31. pkgName, PackageManager.GET_META_DATA);
  32. channelName = appInfo.metaData.getString("CHANNEL");
  33. } catch (PackageManager.NameNotFoundException e) {
  34. e.printStackTrace();
  35. } finally {
  36. if (null == channelName || channelName.equals("MTest")) {
  37. channelName = "test";
  38. }
  39. }
  40. Log.i("-ChannelName-",channelName);
  41. return channelName;
  42. }
  43. public static String getChannelName(Context context) {
  44. //从apk包中获取
  45. ApplicationInfo appInfo = context.getApplicationInfo();
  46. String sourceDir = appInfo.sourceDir;
  47. //注意这里:默认放在meta-inf/里, 所以需要再拼接一下
  48. String key = "META-INF/channel";
  49. String ret = "";
  50. ZipFile zipfile = null;
  51. try {
  52. zipfile = new ZipFile(sourceDir);
  53. Enumeration<?> entries = zipfile.entries();
  54. while (entries.hasMoreElements()) {
  55. ZipEntry entry = ((ZipEntry) entries.nextElement());
  56. String entryName = entry.getName();
  57. if (entryName.startsWith(key)) {
  58. ret = entryName;
  59. break;
  60. }
  61. }
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. Log.i("-ChannelName-",e.getMessage());
  65. } finally {
  66. if (zipfile != null) {
  67. try {
  68. zipfile.close();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74. String[] split = ret.split("_");
  75. String channel = "";
  76. if (split != null && split.length >= 2) {
  77. channel = ret.substring(split[0].length() + 1);
  78. }
  79. Log.i("-ChannelName-",channel);
  80. return channel;
  81. }
  82. }

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文件修改后需要重签名