自动化打包apk总结并整合资料 (继)

上次的总结已过了一段时间,这次细说当中的解析manifest模块 涉及到三块功能

# 一、解析manifest
# 二、maven生成JAR包
# 三、Ant执行调用解析方法

一、解析manifest

使用JAVA实现解析方法,并生成可执行jar包 最终通过shell命令执行解析动做,这里不做详细解释只贴出两部分代码 以做备忘。
1、做为外部使用shell命令的出口代码:
public class AndroidManifestRewriter {
    public static void main(String[] args) throws IOException {
        try{
            Args argList = Args.parseArgs(args);
        
            if(argList == null) {
                System.out.println( "java -jar axml-io.jar -i xxx -o xxx ...");
                System.exit(0);
            }

            new AndroidManifestRewriter().rewrite(new File(argList.mInManifest), new File(argList.mOutManifest), argList.mVersionCode, argList.mVersionName, argList.mChannelValue);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private void rewrite(File source, File target, String versionCode, String versionName, String channelValue) throws IOException {
        final AndroidManifest manifest = new AndroidManifest(source);
        System.out.println("Gefunden: versionName='" + manifest.getVersionName() + "', versionCode=" + manifest.getVersionCode() + ", "+ manifest.getChannelName() + ":" + manifest.getChannelValue() + "," + manifest.getChannelGTName() + ":" + manifest.getChannelGTValue());
        manifest.validate(source);
        if (versionName != null) {
            manifest.setVersionName(versionName);
        }
        if (versionCode != null) {
            manifest.setVersionCode(Integer.parseInt(versionCode));
        }
        if (channelValue != null) {
            manifest.setChannelValue(channelValue);
        }
        if (channelValue != null && manifest.getChannelGTName() != null){
            manifest.setChannelGTValue(channelValue);
        }
        System.out.println("Gefunden: versionName='" + manifest.getVersionName() + "', versionCode=" + manifest.getVersionCode() + ", "+ manifest.getChannelName() + ":" + manifest.getChannelValue() + "," + manifest.getChannelGTName() + ":" + manifest.getChannelGTValue());
        manifest.write(target);
    }

    static class Args {
        public String mInManifest = null;
        public String mOutManifest = null;
        public String mVersionCode = null;
        public String mVersionName = null;
        public String mChannelValue = null;

        public static Args parseArgs(String[] args) {
            Args argList = new Args();

            Options options = new Options();
            options.addOption("h", "help", false, "Print this usage information");
            options.addOption("i", "in", true, "Type a input AndroidManifest.xml file" );
            options.addOption("o", "out", true, "Type a output AndroidManifest.xml file" );
            options.addOption(null, "versionCode", true, "Modify android:versionCode by input value" );
            options.addOption(null, "versionName", true, "Modify android:versionName by input value" );
            options.addOption(null, "channelValue", true, "Modify meta-data:channelValue by input value" );

            CommandLineParser parser = new BasicParser();
            CommandLine commandLine = null;

            try {
                commandLine = parser.parse( options, args );
            } catch (ParseException ex) {
                System.err.println(ex.getMessage());
                System.out.println( "java -jar axml-io.jar -i xxx -o xxx ...");
                System.exit(0);
            }

            boolean cmdFound = false;
            if( commandLine.hasOption("h") ) {
                System.out.println( "java -jar axml-io.jar -i xxx -o xxx ...");
                System.exit(0);
            }
            if( commandLine.hasOption("i") ) {
                argList.mInManifest = commandLine.getOptionValue("i");
                cmdFound = true;
            }
            if( commandLine.hasOption("o") ) {
                argList.mOutManifest = commandLine.getOptionValue("o");
                cmdFound = true;
            }
            if( commandLine.hasOption("versionCode") ) {
                argList.mVersionCode = commandLine.getOptionValue("versionCode");
                cmdFound = true;
            }
            if( commandLine.hasOption("versionName") ) {
                argList.mVersionName = commandLine.getOptionValue("versionName");
                cmdFound = true;
            }
            if( commandLine.hasOption("channelValue") ) {
                argList.mChannelValue = commandLine.getOptionValue("channelValue");
                cmdFound = true;
            }

            if (cmdFound == false) {
                System.out.println( "java -jar axml-io.jar -i xxx -o xxx ...");
                System.exit(0);
            }

            return argList;
        }
    }
}

此处代码 将与maven生成所用到的 pom.xml相关,做为可执行jar包

2、解析后替换清单文件中相关的KEY-VALUE值,因为这次总结 主要源于打包后台需求有修改,不只使用友盟上报统计,又增加了个额外的统计SDK 他自有一套统计SDK,并有独立的meta 定义渠道名称,相应的 在解析时对渠道多了一个维度的修改,主要需要修改的代码如下:

AndroidManifest.java

private ResXmlAttribute(ResSource src) {
            this.namespace = new ResStringPoolRef(src);
            this.name = new ResStringPoolRef(src);
            this.rawValue = new ResStringPoolRef(src);
            this.typedValue = new ResValue(src);
            
            if ("http://schemas.android.com/apk/res/android".equals(this.namespace.lookup())) {
                if (-1 == versionCode && "versionCode".equals(name.lookup())) {
                    versionCode = typedValue.asInt();
                    isVersionCode = true;
                } else if (null == versionName && "versionName".equals(name.lookup())) {
                    versionName = typedValue.asString();
                    isVersionName = true;
                } else if (null == channelName && "UMENG_CHANNEL".equals(rawValue.lookup())) {
                    channelName = typedValue.asString();
                } else if (null == channelGTName && "GT_INSTALL_CHANNEL".equals(rawValue.lookup())) {
                    channelGTName = typedValue.asString();
                } else if (null == channelValue && null != channelName) {
                    channelValue = typedValue.asString();
                    isChannelValue = true;
                }else if (null == channelGTValue && null != channelGTName) {
                    channelGTValue = typedValue.asString();
                    isChannelGTValue = true;
                }
            }
        }

        public void writeTo(ResTarget tgt) {
            this.namespace.writeTo(tgt);
            this.name.writeTo(tgt);
            if (isVersionName && versionNameChanged) {
                long newIndex = stringPool.getIndexOfNewVersionName();
                this.rawValue.writeTo(tgt, newIndex);
                typedValue.writeToWithNewVersionIndex(tgt, newIndex);
            } else if (isVersionCode && versionCodeChanged) {
                this.rawValue.writeTo(tgt);
                typedValue.writeToWithNewVersionCode(tgt);
            } else if (isChannelValue && channelValueChanged) {
                long newIndex = stringPool.getIndexOfNewChannelValue();
                this.rawValue.writeTo(tgt, newIndex);
                System.out.println("getIndexOfNewChannelValue:"+newIndex);
                typedValue.writeToWithNewChannelIndex(tgt, newIndex);
            } else if (isChannelGTValue && channelValueChanged) {
                long newIndex = stringPool.getIndexOfNewChannelGTValue();
                this.rawValue.writeTo(tgt, newIndex);
                System.out.println("getIndexOfNewChannelGTValue:"+newIndex);
                typedValue.writeToWithNewChannelIndex(tgt, newIndex);
            } else {
                this.rawValue.writeTo(tgt);
                typedValue.writeTo(tgt);
            }
        }


AndroidManifestRewriter.java

    private void rewrite(File source, File target, String versionCode, String versionName, String channelValue) throws IOException {
        final AndroidManifest manifest = new AndroidManifest(source);
        System.out.println("Gefunden: versionName='" + manifest.getVersionName() + "', versionCode=" + manifest.getVersionCode() + ", "+ manifest.getChannelName() + ":" + manifest.getChannelValue() + "," + manifest.getChannelGTName() + ":" + manifest.getChannelGTValue());
        manifest.validate(source);
        if (versionName != null) {
            manifest.setVersionName(versionName);
        }
        if (versionCode != null) {
            manifest.setVersionCode(Integer.parseInt(versionCode));
        }
        if (channelValue != null) {
            manifest.setChannelValue(channelValue);
        }
        if (channelValue != null && manifest.getChannelGTName() != null){
            manifest.setChannelGTValue(channelValue);
        }
        System.out.println("Gefunden: versionName='" + manifest.getVersionName() + "', versionCode=" + manifest.getVersionCode() + ", "+ manifest.getChannelName() + ":" + manifest.getChannelValue() + "," + manifest.getChannelGTName() + ":" + manifest.getChannelGTValue());
        manifest.write(target);
    }

GT_INSTALL_CHANNEL 的增加 引起的channelGTName,channelGTValue,isChannelGTValue,getIndexOfNewChannelGTValue()一系列变量、方法的增加。

二、maven生成JAR包,

解析方法完成后,切换到 项目工程目录下,包含pom.xml文件的目录下

自动化打包apk总结并整合资料 (继)_第1张图片
image.png

执行命令:mvn assembly:assembly 会生成可执行JAR包。
关于pom.xml有两处需要注意下 XML文件如下


    4.0.0

    idotools
    axml-io
    1.0.2

    
        
            commons-cli
            commons-cli
            1.2
        
    

    
        
            
                maven-compiler-plugin
                3.1
                
                    1.6
                    1.6
                
            
              

                maven-assembly-plugin  
                  
                      
                          
                            true  
                            libs/  
                            idotools.axml.io.AndroidManifestRewriter
                          
                      
                    
                        jar-with-dependencies
                    
                  
                
                    
                        make-assembly
                        package
                        
                            single
                        
                    
                
              
        
    


其中
1.0.2 代表JAR的版本号;
idotools.axml.io.AndroidManifestRewriter 代表可执行入口类名称。
就此 解析方法修改完成,接下来实现使用ant 在build.xml中配置此可执行jar包,实现批量打包。

三、Ant执行调用解析方法

    
        New Channel umeng: ${CHANNEL}
        
            
            
            
            
            
            
        
        
    

以上是build.xml中对渠道的替换配置,可以见到 执行axmlio shell命令,其中参数 -i -o --channelValue 与可执行JAR中的AndroidManifestRewriter相关。以下只做参考

    static class Args {
        public String mInManifest = null;
        public String mOutManifest = null;
        public String mVersionCode = null;
        public String mVersionName = null;
        public String mChannelValue = null;

        public static Args parseArgs(String[] args) {
            Args argList = new Args();

            Options options = new Options();
            options.addOption("h", "help", false, "Print this usage information");
            options.addOption("i", "in", true, "Type a input AndroidManifest.xml file" );
            options.addOption("o", "out", true, "Type a output AndroidManifest.xml file" );
            options.addOption(null, "versionCode", true, "Modify android:versionCode by input value" );
            options.addOption(null, "versionName", true, "Modify android:versionName by input value" );
            options.addOption(null, "channelValue", true, "Modify meta-data:channelValue by input value" );

            CommandLineParser parser = new BasicParser();
            CommandLine commandLine = null;

            try {
                commandLine = parser.parse( options, args );
            } catch (ParseException ex) {
                System.err.println(ex.getMessage());
                System.out.println( "java -jar axml-io.jar -i xxx -o xxx ...");
                System.exit(0);
            }

            boolean cmdFound = false;
            if( commandLine.hasOption("h") ) {
                System.out.println( "java -jar axml-io.jar -i xxx -o xxx ...");
                System.exit(0);
            }
......
}

也可以切换到utils下执行

./axmlio -i AndroidManifest.xm -o AndroidManifest.xml.tmp --channelValue mumay

shell 文件axmlio 内容如下:

#!/bin/bash
#
# Copyright (C) 2007 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script is a wrapper for smali.jar, so you can simply call "smali",
# instead of java -jar smali.jar. It is heavily based on the "dx" script
# from the Android SDK

# Set up prog to be the path of this script, including following symlinks,
# and set up progdir to be the fully-qualified pathname of its directory.
prog="$0"
while [ -h "${prog}" ]; do
    newProg=`/bin/ls -ld "${prog}"`
    echo ${newProg}


    newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
    if expr "x${newProg}" : 'x/' >/dev/null; then
        prog="${newProg}"
    else
        progdir=`dirname "${prog}"`
        prog="${progdir}/${newProg}"
    fi
done
oldwd=`pwd`
progdir=`dirname "${prog}"`
cd "${progdir}"
progdir=`pwd`
prog="${progdir}"/`basename "${prog}"`
cd "${oldwd}"


jarfile=axmlio.jar
libdir="$progdir"
if [ ! -r "$libdir/$jarfile" ]
then
    echo `basename "$prog"`": can't find $jarfile"
    exit 1
fi

javaOpts=""

# If you want DX to have more memory when executing, uncomment the following
# line and adjust the value accordingly. Use "java -X" for a list of options
# you can pass here.
# 
javaOpts="-Xmx512M"

# Alternatively, this will extract any parameter "-Jxxx" from the command line
# and pass them to Java (instead of to dx). This makes it possible for you to
# add a command-line parameter such as "-JXmx256M" in your ant scripts, for
# example.
while expr "x$1" : 'x-J' >/dev/null; do
    opt=`expr "$1" : '-J\(.*\)'`
    javaOpts="${javaOpts} -${opt}"
    shift
done

if [ "$OSTYPE" = "cygwin" ] ; then
    jarpath=`cygpath -w  "$libdir/$jarfile"`
else
    jarpath="$libdir/$jarfile"
fi

# add current location to path for aapt
PATH=$PATH:`pwd`;
export PATH;
exec java $javaOpts -jar "$jarpath" "$@"

``

这篇文章因为是接手项目,所以这里所做总结主要是为了备忘,很多地方写的并不通顺,也不包含细节讲解 原因也在于并不是很熟悉这块的开发

你可能感兴趣的:(自动化打包apk总结并整合资料 (继))