안드로이드 다중 채널 패키지 도구를 직접 쓰면libs와.so 파일

android 앱을 업로드할 때 모든 채널을 구분해야 합니다.일반적으로 설정 파일에서 하나의 채널 id를 변경합니다. 만약 여러 채널이 있다면 수동으로 수정하고 apk를 생성하면 매우 번거롭고 오류 확률이 증가합니다.
패키지 도구 종류를 공유합니다.인터넷에도 비슷한 것이 있다. 나는 그것을 바탕으로 최적화를 했다. 특히 so 파일을 포장했는데 작은 명령 한 마디가 많이 폐기되었다.
우리 프로젝트에서 사용하는umeng은 통계 분석 도구를 만들고umeng은 채널을 나누어 포장할 때 manifest를 수정해야 한다.xml의
value 값.
이 값은strings에 넣었습니다.xml에서 appchannel, 컴파일할 때 app 수정만 하면 됩니다channel.
다음은 공구류입니다. 직접 운행하면 됩니다.
package com.yang.main;

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;
/**
 * @description: android       ,        ,      channels 
 * @author: [email protected]
 * @time:    2012-10-23  5:17:47
 */
public class CompiledApkUpdate {

    private static final String androidSDK_PATH = "D:\\android\\android-sdk-windows\\";        //android SDK  

    public static final String APK_NAME = "duitang.apk";
    public static final String PROJECT_LIBARY = "";
    public static final String PROJECT_PATH = "D:\\android\\workspace\\tmp\\duitang\\";        //        
    public static final String APK_PATH = "D:\\android\\workspace\\tmp\\apk\\duitang_";        //     apk     duitang_   
    
    
    private static final String apk_PATH_keystore = "D:\\android\\keystore\\bb";        //apk      
    private static final String channelFlag = "app_channel";
    
//    public static String[] channels = {"duitang"}; 
    private static String currentChannelName = "";
    public static String[] channels = {"duitang","91","360","QQ","jifeng","anzhuo","anzhi","youyi","appchina","wangyi","baidu","souhu","3g","nduo","xiaomi","huawei","meizu","lianxiang","aliyun","taobao","google","nearme","mumayi","wandoujia","crosscat","dangle","maopao","feiliu"}; 

    public static void main(String[] args) { 
        replaceChannel();
    }

    /**
     *       
     */
    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]);
                currentChannelName = channels[channelid];
                write(tmpContent,outPath);
                System.out.println("replace channel name over...");
                packageRes(); //           。       。
                createUnsignedApk();
                signedApk(channelid);
            }
            write(content,outPath);        //     channel_name
            System.out.println("execute over!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * class     classes.dex
     */
    public static void packageDex(){
        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...");
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    }
    
    /**
     * res assets     res.zip
     */
    public static void packageRes(){
        try{
            System.out.println(currentChannelName+" create resources.ap");
            String library = "";
            if(PROJECT_LIBARY!=null&&!PROJECT_LIBARY.equals("")){
                library = "-S " + PROJECT_LIBARY + "res ";
            }
            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       
                            library +
                            "-A " + PROJECT_PATH + "assets " +                        //-A   assets
                            "-I " + androidSDK_PATH + "platforms\\android-16\\android.jar " +    //-I   android 
                            "-F " + PROJECT_PATH + "bin\\resources.ap_ --auto-add-overlay"); //        resources.ap_
            new MyThread(process.getErrorStream()).start();
            new MyThread(process.getInputStream()).start();
            process.waitFor();
            process.destroy();
            System.out.println(currentChannelName+" resources.ap over...");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * classes.dex res.zip AndroidManifest.xml       apk
     */
    public static void createUnsignedApk(){
        try{
            System.out.println(currentChannelName+" createUnsignedApk start");
            Process process = null;
            process = Runtime.getRuntime().exec(
                    androidSDK_PATH+ "tools\\apkbuilder.bat "
                    + PROJECT_PATH + "bin\\"+APK_NAME+" -u -z "
                    + PROJECT_PATH + "bin\\resources.ap_ -f "
                    + PROJECT_PATH + "bin\\classes.dex"
					+" -rj "+ PROJECT_PATH + "libs"		//          libs jar          
					+" -nf "+ PROJECT_PATH + "libs"		//                   ,      。        so       。
					); //       apk.
            new MyThread(process.getErrorStream()).start();
            new MyThread(process.getErrorStream()).start();
            process.waitFor();
            process.destroy();
            System.out.println(currentChannelName+" createUnsignedApk over");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     *   apk
     */
    public static void signedApk(int channelid){
        try{
            System.out.println(currentChannelName+" signed apk start");
            Process process = null;
            String jarsigner;
            jarsigner = "jarsigner -keystore "+apk_PATH_keystore+" -storepass ***** -keypass ****** " +
                    "-signedjar "
                    + APK_PATH
                    + channels[channelid]
                    + ".apk "
                    + PROJECT_PATH
                    + "bin\\"+APK_NAME+" *****";            //  apk
            process = Runtime
                    .getRuntime()
                    .exec(jarsigner); //  apk    
            new MyThread(process.getErrorStream()).start();

            new MyThread(process.getInputStream()).start();
            process.waitFor();
            process.destroy();
            System.out.println(currentChannelName+" signed apk over"); //          。          
        }catch(Exception e){
            e.printStackTrace();
        }
    }
/**
     *   google       。              zipalign apk
     */
    public static void zipalignApk(int channelid){
        try{
            System.out.println(currentChannelName+":zipalign apk start");
            String apkPath=APK_PATH.replace("{c}", channels[channelid]);
            File file=new File(apkPath);
            if(!file.getParentFile().isDirectory()) file.getParentFile().mkdirs();
            Process process = null;
            String jarsigner;
            jarsigner = "cmd.exe /C "+androidSDK_PATH+"tools/zipalign.exe -v 4 "
                    + apkPath
                    + ".apk "
                    + apkPath
                    + "-z.apk";            //  apk
            process = Runtime
                    .getRuntime()
                    .exec(jarsigner); //  apk    
            new MyThread(process.getErrorStream()).start();


            new MyThread(process.getInputStream()).start();
            process.waitFor();
            process.destroy();
            System.out.println(currentChannelName+":zipalign apk over"); //          。          
        }catch(Exception e){
            e.printStackTrace();
            System.exit(0);
        }
    }
    
    
    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 + "
"); } 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 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(); } } } }

좋은 웹페이지 즐겨찾기