android 多渠道打包

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();
            }
        }
    }
}

posted on 2012-12-13 21:12  jiezzy  阅读(416)  评论(0编辑  收藏  举报