博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 多渠道打包
阅读量:6315 次
发布时间:2019-06-22

本文共 4687 字,大约阅读时间需要 15 分钟。

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
    public static final String PROJECT_PATH = "E:\\java\\PkgTest\\";    //工程路径
    private static final String androidSDK_PATH = "D:\\java\\android-sdk\\";        //android SDK路径
    private static final String apk_PATH = "E:\\java\\apks\\PkgTest_";// apk生成路径, 'duitangMain_'是apk的名称,后面会自动增加渠道名称,修改apk名称,只要修改duitangMain
    private static final String channelFlag = "app_channel";
    
    public static String[] channels = {"91","360"
//        ,"QQ","jifeng","anzhuo","anzhi","youyi","appchina",
//        "wangyi","baidu","souhu","3g","nduo","xiaomi","huawei","meizu","htc","lianxiang","aliyun","taobao"
        };
    public static void main(String[] args) {
        try {
            System.out.println("dx.bat start...");
            Process process = Runtime.getRuntime().exec(androidSDK_PATH
                    +"platform-tools\\dx.bat --dex --output="
                    +PROJECT_PATH+"bin\\classes.dex "
                    +PROJECT_PATH+"bin\\classes "
                    +PROJECT_PATH+"libs\\*.jar");
            
            new MyThread(process.getErrorStream()).start();
            new MyThread(process.getInputStream()).start();
            
            process.waitFor();  
            process.destroy();  
            System.out.println("dx.bat over...");
            replaceChannel(); // 替换渠道号
            System.out.println("execute finished!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void replaceChannel() {
        try {
            String outPath = PROJECT_PATH + "res\\values\\strings.xml"; // 输出文件位置
//            String content = read(outPath);
            for(int channelid=0;channelid<channels.length;channelid++){
//                String tmpContent = content;
//                tmpContent = tmpContent.replaceFirst(channelFlag, channels[channelid]);
//                write(tmpContent,outPath);
//                System.out.println("replace channel name over...");
                generatePackage(channelid); // 一次渠道号的更改完成。可以进行打包了。
            }
//            write(content,outPath);        //完成后还原channel_name
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String read(String path) {
        StringBuffer res = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path),"UTF-8"));
            while ((line = reader.readLine()) != null) {
                res.append(line + "\n");
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res.toString();
    }
    public static boolean write(String cont, String dist) {
        try {
            OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File(dist)),"utf-8");
            writer.write(cont);
            writer.flush();
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public static void generatePackage(int channelid) {
        try {
            System.out.println(channels[channelid]+" aapt start...");
            Process process = null;
            process = Runtime
                    .getRuntime()
                    .exec(  androidSDK_PATH
                            + "platform-tools\\aapt.exe package -f " +
                            "-M " + PROJECT_PATH + "AndroidManifest.xml " +            //-M 指定配置文件
                            "-S " + PROJECT_PATH + "res " +                            //-S 指定资源文件
                            "-A " + PROJECT_PATH + "assets " +                        //-A 指定assets
                            "-I " + androidSDK_PATH + "platforms\\android-16\\android.jar " +    //-I 指定android类
                            "-F " + PROJECT_PATH + "bin\\resources.ap_"); // 将资源文件打包resources.ap_
            new MyThread(process.getErrorStream()).start();
            new MyThread(process.getInputStream()).start();
            process.waitFor();
            process.destroy();
            System.out.println(channels[channelid]+" aapt over...");
            process = Runtime.getRuntime().exec(
                            androidSDK_PATH+ "tools\\apkbuilder.bat "
                            + PROJECT_PATH + "bin\\duitangMain.apk -u -z "
                            + PROJECT_PATH + "bin\\resources.ap_ -f "
                            + PROJECT_PATH + "bin\\classes.dex -rf "
                            + PROJECT_PATH + "src -rj "
                            + PROJECT_PATH + "\\libs"); // 生成未签名的apk
            process.waitFor();
            process.destroy();
            process = Runtime
                    .getRuntime()
                    .exec("jarsigner -keystore D:\\android\\keystore\\android.keystore -storepass 123456 -keypass 123456 " +
                            "-signedjar "
                            + apk_PATH
                            + channels[channelid]
                            + ".apk "
                            + PROJECT_PATH
                            + "bin\\PkgTest.apk android.keystore"); // 对apk进行签名
            new MyThread(process.getErrorStream()).start();
            new MyThread(process.getInputStream()).start();
            process.waitFor();
            process.destroy();
            System.out.println(channels[channelid] + " finished..."); // 一条渠道的打包完成。文件会输出到指定目录
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static class MyThread extends Thread{
        BufferedReader bf;
        
        public MyThread(InputStream input) {
            bf = new BufferedReader(new InputStreamReader(input));
        }
        public void run() {
            String line;
            try {
                line = bf.readLine();
                while (line != null) {
                    System.out.println(line);
                    line = bf.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

转载于:https://www.cnblogs.com/jiezzy/archive/2012/12/13/2817069.html

你可能感兴趣的文章
下一代 Hadoop YARN :相比于MRv1,YARN的优势
查看>>
阿里巴巴离职DBA 35岁总结的职业生涯
查看>>
LOT NUMBER / PO / RECEIPT NO Relation.
查看>>
Fedora9常用网络调试
查看>>
Matlab绘图方法汇总
查看>>
关于操作系统的进程调度问题
查看>>
POJ 1243 One Person
查看>>
Bash: about .bashrc, .bash_profile, .profile, /etc/profile, etc/bash.bashrc and others
查看>>
hibernate 映射实例 学生 课程 成绩
查看>>
【CAS单点登录视频教程】 第04集 -- tomcat下配置https环境
查看>>
自适应网页布局经验
查看>>
Ubuntu apache 禁止目录浏览
查看>>
常用脚本--归档ERRORLOG
查看>>
js网页倒计时精确到秒级
查看>>
常用CSS缩写语法总结
查看>>
TDD:什么是桩(stub)和模拟(mock)?
查看>>
C# 模拟POST提交文件
查看>>
PAT 解题报告 1004. Counting Leaves (30)
查看>>
Android开发之蓝牙 --修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
查看>>
[Head First设计模式]生活中学设计模式——外观模式
查看>>