Android 多渠道自動打包

首先確保你已經配置好了Android 開發環境,並創建了ANDROID_SDK_HOME 環境變量。

ANT配置

下載Ant並解壓,這裏我們選擇了ant-1.9.0 解壓目錄是D:\JAVA\apache-ant-1.9.0 配置環境變量ANT_HOME=D:\JAVA\apache-ant-1.9.在PATH中添加 %ANT_HOME%\bin;

ant-contrib-1.0b3.jar 配置

下載ant-contrib-1.0b3.jar 並將其放到D:\JAVA\apache-ant-1.9.0\lib中。

build.xml創建

             在命令行下進入工程所在目錄 例如:
             cd F:\workspace\HelloWorld>

            首先 需要 在AndroidManafesyt.xml 文件中天加
           <meta-data  android:name="qudao"  android:value="channel" /> 注意裏面的channe會被最終的渠道l替換掉。

     執行 android update project -p F:\workspace\HelloWorld -t 2
             -p 指定工程路徑 這裏必須是全路徑 -t 指定編譯的目標SDK 參數由android list targets命令給出  我的SDK只有兩個(下面爲執行結果)我選android-19 也就是ID爲2的。所以參數是2.
             F:\workspace\HelloWorld>android list targets
Available Android targets:
----------
id: 1 or "android-10"
     Name: Android 2.3.3
     Type: Platform
     API level: 10
     Revision: 2
     Skins:
     ABIs : no ABIs.
----------
id: 2 or "android-19"
     Name: Android 4.4.2
     Type: Platform
     API level: 19
     Revision: 2
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, W
XGA720, WXGA800, WXGA800-7in
     ABIs : armeabi-v7a
             之後,工程會多出兩個文件build.xml local.properties
             build.xml內容如下:
             
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<project name="MainActivity" default="help">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />

    <!-- The ant.properties file can be created by you. It is only edited by the
         'android' tool to add properties to it.
         This is the place to change some Ant specific build properties.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         For other overridable properties, look at the beginning of the rules
         files in the SDK, at tools/ant/build.xml

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="ant.properties" />

    <!-- if sdk.dir was not set from one of the property file, then
         get it from the ANDROID_HOME env var.
         This must be done before we load project.properties since
         the proguard config can use sdk.dir -->
    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>

    <!-- The project.properties file is created and updated by the 'android'
         tool, as well as ADT.

         This contains project specific properties such as project target, and library
         dependencies. Lower level build properties are stored in ant.properties
         (or in .classpath for Eclipse projects).

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <loadproperties srcFile="project.properties" />

    <!-- quick check on sdk.dir -->
    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
            unless="sdk.dir"
    />

    <!--
        Import per project custom build rules if present at the root of the project.
        This is the place to put custom intermediary targets such as:
            -pre-build
            -pre-compile
            -post-compile (This is typically used for code obfuscation.
                           Compiled code location: ${out.classes.absolute.dir}
                           If this is not done in place, override ${out.dex.input.absolute.dir})
            -post-package
            -post-build
            -pre-clean
    -->
    <import file="custom_rules.xml" optional="true" />

    <!-- Import the actual build file.

         To customize existing targets, there are two options:
         - Customize only one target:
             - copy/paste the target into this file, *before* the
               <import> task.
             - customize it to your needs.
         - Customize the whole content of build.xml
             - copy/paste the content of the rules files (minus the top node)
               into this file, replacing the <import> task.
             - customize to your needs.

         ***********************
         ****** IMPORTANT ******
         ***********************
         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
         in order to avoid having your file be overridden by tools such as "android update project"
    -->
    <!-- version-tag: 1 -->
    <import file="${sdk.dir}/tools/ant/build.xml" />

</project>



            我們修改並添加信息 修改之後:
<?xml version="1.0" encoding="UTF-8"?>
<project name="HelloWorld" default="release">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />

    <!-- The ant.properties file can be created by you. It is only edited by the
         'android' tool to add properties to it.
         This is the place to change some Ant specific build properties.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         For other overridable properties, look at the beginning of the rules
         files in the SDK, at tools/ant/build.xml

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="ant.properties" />

    <!-- if sdk.dir was not set from one of the property file, then
         get it from the ANDROID_HOME env var.
         This must be done before we load project.properties since
         the proguard config can use sdk.dir -->
    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_SDK_HOME}">
        <isset property="env.ANDROID_SDK_HOME" />
    </condition>

    <!-- The project.properties file is created and updated by the 'android'
         tool, as well as ADT.

         This contains project specific properties such as project target, and library
         dependencies. Lower level build properties are stored in ant.properties
         (or in .classpath for Eclipse projects).

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <loadproperties srcFile="project.properties" />

    <!-- quick check on sdk.dir -->
    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
            unless="sdk.dir"
    />

    <!--
        Import per project custom build rules if present at the root of the project.
        This is the place to put custom intermediary targets such as:
            -pre-build
            -pre-compile
            -post-compile (This is typically used for code obfuscation.
                           Compiled code location: ${out.classes.absolute.dir}
                           If this is not done in place, override ${out.dex.input.absolute.dir})
            -post-package
            -post-build
            -pre-clean
    -->
    <import file="custom_rules.xml" optional="true" />

    <!-- Import the actual build file.

         To customize existing targets, there are two options:
         - Customize only one target:
             - copy/paste the target into this file, *before* the
               <import> task.
             - customize it to your needs.
         - Customize the whole content of build.xml
             - copy/paste the content of the rules files (minus the top node)
               into this file, replacing the <import> task.
             - customize to your needs.

         ***********************
         ****** IMPORTANT ******
         ***********************
         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
         in order to avoid having your file be overridden by tools such as "android update project"
    -->
    <!-- version-tag: 1 -->
    <import file="${sdk.dir}\tools\ant\build.xml" />

    <property name="out.unaligned.dir" value="F:\HelloWorld_${app_version}/" />
    <mkdir dir="${out.unaligned.dir}" />
    <target name="modify_update_file">
        <echo>*********************** make channel ${channel}</echo>
        
        <replaceregexp file="AndroidManifest.xml"
            match='channel'
            replace='${channel}'
            byline="false"
            encoding="utf-8"
        />
        <property name="out.unaligned.file" location="${out.unaligned.dir}/HelloWorld_${app_version}_${channel}_android.apk"/>
        
    </target>

    <target name="make_one_channels" depends="savemanifest,modify_update_file,release,replacemanifest,deletebin" description="description">
    </target>

    <target name="replacemanifest">
        <echo>*********************** replacemanifest</echo>
        <delete file="${basedir}\AndroidManifest.xml"/>
        <copy file="..\temp\build\META-INF\AndroidManifest.xml" todir="${basedir}" encoding="utf-8"/>
    </target>

    <target name="savemanifest">
        <echo>*********************** savemanifest</echo>
        <copy file="${basedir}\AndroidManifest.xml" todir="..\temp\build\META-INF" encoding="utf-8" />
    </target>

    <target name="deletebin">
        <delete dir="${basedir}\bin" />
    </target>

    <taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" classpath="F:\adt-bundle-windows-x86-20131030\sdk\tools\lib\ant-contrib-1.0b3.jar" />
    <target name="make_channels">
        <foreach target="make_one_channels" list="${market_channels}" delimiter="," param="channel">
        </foreach>
    </target>
</project><img src="https://img-my.csdn.net/uploads/201406/02/1401642364_9125.png" alt="" />
修改點:

1.0  

<project name="HelloWorld" default="release">

2.0 

<condition property="sdk.dir" value="${env.ANDROID_SDK_HOME}">

<isset property="env.ANDROID_SDK_HOME" />

</condition>

3.0      

 <property name="out.unaligned.dir" value="F:\HelloWorld_${app_version}/" /> 輸出文件目錄

4.0       

<property name="out.unaligned.file" location="${out.unaligned.dir}/HelloWorld_${app_version}_${channel}_android.apk"/> 輸出文件名稱

5.0       

<taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" classpath="F:\adt-bundle-windows-x86-20131030\sdk\tools\lib\ant-contrib-1.0b3.jar"               /> 指定ant-contrib-1.0b3.jar文件目錄。

6.0        注意

<foreach target="make_one_channels" list="${market_channels}" delimiter="," param="channel">中的channel

 和 AndroidManefest.xml 中的 <meta-data  android:name="qudao"  android:value="channel" />  channel 該值必須一樣。根據該屬性配置的

ant.properties 文件              

             在eclipse中工程裏創建ant.properties 文件 在裏面添加
             key.store=F:/workspace/release_key                  
key.alias=release_key
key.store.password=123456
key.alias.password=123456
market_channels=xx,yy,zz
app_version=1_0_build_0
分別爲:證書文件 別名 證書密碼 別名密碼 渠道名字 版本號 
這裏的證書路徑 一定已/ 分割 而不是\ (我在這裏就遇到了問題

編譯

繼續執行命令:

 ant make_channels

如果成功 會出現

 BUILD SUCCESSFUL

Total time: 3 seconds 同時出現編譯完的文件

             

自己編譯時 出現的錯誤:

F:\adt-bundle-windows-x86-20131030\sdk\tools\ant\build.xml:1139: The following e
rror occurred while executing this line:
F:\adt-bundle-windows-x86-20131030\sdk\tools\ant\build.xml:1151: Value for 'keys
tore' is not valid. It must resolve to a single path

證書文件指定的路徑不對導致。通過ant release命令也會得到同樣的錯誤。 

            

            
發佈了20 篇原創文章 · 獲贊 6 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章