需求

为什么打那么多的渠道包,相信这个不需要说明了,中国android发布的渠道多也是没有办法的。

多渠道打包的原理参照美团多渠道打包,python的实现方法参照GavinCT

我的Java实现

我是做Android的,所以就用java实现了一遍。一个巩固一下知识,二是Android 的环境下面一定都有java环境了,不需要再装python的环境了。下面是具体的步骤。

  • 目录排版如下
  • info
    • apks //存放需要添加渠道的apk文件
    • channel.txt //渠道信息文件
    • czt.txt //空文件
  • 第一步 加载 apks目录下载所有的 apk文件
    private void initApks(){        if (apks == null) {            apks = new ArrayList<>();        }else {            apks.clear();        }        File apkDir = new File(channelConfigPath, "info/apks");        if (!apkDir.exists()){            throw new IllegalArgumentException("can't find apk files in :"  + apkDir.getAbsolutePath());        }        File[] files = apkDir.listFiles(new FileFilter() {            @Override            public boolean accept(File pathname) {                return pathname.getName().endsWith(".apk");            }        });        for (File f : files){            apks.add(f);        }        if (apks.isEmpty()){            throw new RuntimeException("not fond apk in :" + apkDir.getAbsolutePath());        }        System.out.println("init apk finish");    }
  • 第二步加载渠道文件,文件内容结构如下:

360
baidu
等等

    private void initChannel(){        if (channels == null){            channels = new ArrayList<>();        }else {            channels.clear();        }        File channelFile = new File(channelConfigPath, "info/channel.txt");        if (!channelFile.exists()){            throw new IllegalArgumentException("can't find channel file at :"  + channelFile.getAbsolutePath());        }        BufferedReader reader = null;        try {            reader = new BufferedReader(new FileReader(channelFile));            String ch = null;            while ((ch = reader.readLine()) != null){                channels.add(ch);            }        } catch (FileNotFoundException e) {            e.printStackTrace();            throw new RuntimeException("read channel file error");        } catch (IOException e) {            e.printStackTrace();            throw new RuntimeException("read channel file error");        }finally {            if (reader != null){                try {                    reader.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        System.out.println("init channel finish");    }
  • 第三步生成一个空文件,用来添加到apk里面
    private void initEmptyFile(){        if (srcEmptyFile == null){            srcEmptyFile = new File(channelConfigPath, "info/czt.txt");        }        if (!srcEmptyFile.exists()) {            try {                srcEmptyFile.createNewFile();            } catch (IOException e) {                e.printStackTrace();                throw new RuntimeException("create czt.txt file eroor");            }        }    }
  • 最后生成渠道的apk文件
private void buildChannelApk(){        for (File f : apks){            String fileName = f.getName();            String filerRealName = fileName.split("\\.")[0];            File storeDir = new File(f.getParentFile(), filerRealName);            if (!storeDir.exists()){                storeDir.mkdirs();            }            for (String channel : channels){                File outChannelFile = new File(storeDir, filerRealName + "_" + channel + ".apk");                try {                    FileUtils.copyFile(f, outChannelFile);                } catch (IOException e) {                    e.printStackTrace();                    throw new RuntimeException("copy apk file error" + f.getAbsolutePath());                }                try {                    Map env = new HashMap<>();                    env.put("create", "true");                    // locate file system by using the syntax                    // defined in java.net.JarURLConnection                    URI uri = URI.create("jar:" + outChannelFile.toURI());                    try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {                        Path externalTxtFile = Paths.get(srcEmptyFile.toURI());                        Path pathInZipfile = zipfs.getPath(String.format(CHANNEL, channel));                        // copy a file into the zip file                        Files.copy(externalTxtFile, pathInZipfile,                                StandardCopyOption.REPLACE_EXISTING);                    }                } catch (IOException e) {                    e.printStackTrace();                    throw new RuntimeException("build file error");                }finally {                }                System.out.println("build " + outChannelFile.getName() +  " finish");            }            System.out.println("build apk finish");        }    }

获取渠道信息的代码参见GavinCT。
当前项目的源代码参见github

更多相关文章

  1. Android(安卓)环信官方Demo3.3.2详细配置方法
  2. Android中Debuggable设置问题
  3. android makefile and build system分析
  4. android jni
  5. Android使用自定义字体的方法
  6. Android的反编译和代码混淆
  7. Android(安卓)Studio 友盟api实现apk多渠道打包
  8. android boot.img 拆包,修改adb 具有root权限,以i9250为例
  9. NPM 和webpack 的基础使用

随机推荐

  1. MySQL基础: 预处理和常用的MySQL函数
  2. CURD常用操作、select常用查询、预处理原
  3. 小写转大写(ASCLL)
  4. 初识指针
  5. 微信小程序实现带参分享并消息卡片获取参
  6. 【Golang语言社区】四川麻将随机初始化牌
  7. 多种详细讲解小程序页面传值
  8. Awesome Go!高玩Gopher都会参考的Go资源集
  9. 经典面试题(22):以下代码将输出的结果是什
  10. 配置微信小程序自动更新