You can package libs and. so files by writing android multi-channel packaging tools.

Source: Internet
Author: User

When uploading an android app, you need to separate each channel. A channel id is usually changed in the configuration file. If there are multiple channels, manual modification and apk generation will be very troublesome and increase the error probability.

Here is a packaging tool class. The Internet is similar. I optimized it based on it, especially the so file packaging. A small command is useless.

The umeng used in our project is used as a statistical analysis tool. umeng needs to modify

<Meta-data android: name = "UMENG_CHANNEL" android: value = "@ string/channel_name"/> value.

This value is stored in strings. xml <string name = "channel_name"> app_channel </string>. Therefore, you only need to modify app_channel during compilation.

The following is a tool class. Run it directly.

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 channel packaging tool class, ensure that the path is correct, channel name filled in channels * @ author: hytfly@gamil.com * @ time: 2012-10-23 5:17:47 pm */public class CompiledApkUpdate {private static final String androidSDK_PATH = "D: \ android-sdk-windows \\"; // android SDK path 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 \ "; // project path to be packaged public static final String APK_PATH =" D :\\ android \ workspac E \ tmp \ apk \ duitang _ "; // the path for storing the apk After packaging. duitang _ is the prefix private static final String apk_PATH_keystore =" D: \ android \ keystore \ bb "; // apk signature file path 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 ();}/*** replace channel name */public static void replaceChannel () {try {String outPath = PROJECT_PATH + "res \ values \ strings. xml "; // output file location String content = read (outPath); for (in T 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 (); // The channel number is changed once. It can be packaged. CreateUnsignedApk (); signedApk (channelid);} write (content, outPath); // restore channel_name System. out. println ("execute over! ");} Catch (Exception e) {e. printStackTrace () ;}/ *** class file is packaged into classes. dex */public static void packageDex () {try {System. out. println ("dx. bat start... "); Process process = runtime.getruntime(cmd.exe c (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 ()). star T (); new MyThread (process. getInputStream ()). start (); process. waitFor (); process. destroy (); System. out. println ("dx. bat over... ");} catch (Exception e) {e. printStackTrace () ;}}/*** res assets file into 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 specify the configuration file"-S "+ PROJECT_PATH +" res "+ //-S specify the resource file library +"-A "+ PROJECT_PATH +" assets" + //-A specify assets "-I" + androidSDK_PATH + "platforms \ android-16 \ android. jar "+ //-I specifies the android class"-F "+ PROJECT_PATH +" bin \ resources. ap _ -- auto-add-overlay "); // package the resource file 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 combination into unsigned apk */public static void createUnsignedApk () {try {System. out. println (currentChannelName + "createUnsignedApk start"); Process process = null; process = runtime.getruntime(cmd.exe c (androidSDK_PATH + "tools \ apkbuilder. bat "+ PROJECT_PATH +" bin \ "+ APK_NAME +"-u-z "+ PROJECT_PATH +" bin \ resources. ap _-f "+ PROJECT_PATH +" bin \ classes. dex "); // generate an unsigned apk.
// + "-Rj" + PROJECT_PATH + "libs" it is important to add the libs jar package
// + "-Nf" + PROJECT_PATH + "libs" I tried many times to read various materials and shared them with you. In this way, you can package the so file.
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 () ;}/ *** signature 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 +" ***** "; // signature apk process = Runtime. getRuntime (). exec (jarsigner); // sign the apk new MyThread (process. getErrorStream ()). start (); new MyThread (process. getInputStream ()). start (); process. waitFor (); process.de Stroy (); System. out. println (currentChannelName + "signed apk over"); // package one channel. The file will be output to the specified directory} catch (Exception e) {e. printStackTrace ();}}
/*** This is the packaging method of google signature. 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 "; // signature apk process = Runtime. getRuntime (). exec (jarsigner); // sign the apk new MyThread (process. getErrorStream ()). start (); new MyThread (process. getInputStream ()). start (); process. waitFor (); Process. destroy (); System. out. println (currentChannelName + ": zipalign apk over"); // package a channel. The file will be output to the specified directory} 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 + "\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 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();            }        }    }}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.