ant 實現批量打包android應用

來源:互聯網
上載者:User

          很多的應用中需要加上應用推廣的統計,如果一個一個的去產生不同渠道包的應用,效率低不說,還有可能不小心弄錯了分發渠道,使用ant可以批量產生應用。

一、添加渠道包資訊

       為了統計渠道資訊,就不得不在程式的某個地方加入渠道的資訊,然後針對不同的渠道打不同的包。一般可以在Manifest檔案中加入渠道編號,而不直接寫在代碼中。這樣做的好處是,可以針對不同渠道,自動化去修改Manifest檔案中的渠道編號,然後自動為該渠道打包。

Manifest檔案支援Meta Data標籤,建議使用這種自訂標籤。例如下面的檔案片段。

 

 

 <meta-data android:value="000000" android:name="CHANNEL"/>

 

 

二、渠道包讀取

public static String getChanel(Context ctx){String CHANNELID="000000";try {           ApplicationInfo  ai = ctx.getPackageManager().getApplicationInfo(           ctx.getPackageName(), PackageManager.GET_META_DATA);           Object value = ai.metaData.get("");           if (value != null) {           CHANNELID= value.toString();           }       } catch (Exception e) {           //       }return CHANNELID;}

 

三、自動打包實現

         Ant編譯android程式        簡單介紹了使用ant命令打包android程式,實現批量打包需要的加一個類似於for迴圈的功能即可,在Ant的核心包裡沒有相關的For迴圈的Task,要下載相應的擴充包。可以使用開源的Ant-contrib包。:http://ant-contrib.sourceforge.net/  。下載後的解壓得到的jar檔案放到ant的lib目錄。

        在build.xml中增加如下代碼就可以實現批量打包:

 

taskdef resource="net/sf/antcontrib/antcontrib.properties">  <classpath><pathelement location="lib/ant-contrib-1.0b3.jar"/>  </classpath></taskdef> <target name="deploy">   <foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=",">   </foreach> </target><target name="modify_manifest"><replaceregexp flags="g" byline="false"><regexp pattern="android:value="(.*)" android:name="CHANNEL"" /><substitution expression="android:value="${channel}" android:name="CHANNEL"" /><fileset dir="" includes="AndroidManifest.xml" /></replaceregexp><property name="out.release.file"  location="${out.absolute.dir}/${ant.project.name}_${channel}_${app_version}.apk" /><antcall target="release" /></target>

taskdef 聲明需要放到較前位置,因為if condition也會用到此聲明。

build.properties檔案增加:

taskdef 聲明需要放到較前位置,因為if condition也會用到此聲明。

build.properties檔案增加:

 

  market_channels=000000,012345   app_version=1.2.1

market名稱用逗號分隔

執行ant deploy即可。

 

 /**
* @author 張興業
* 郵箱:xy-zhang#163.com
* android開發進階群:278401545
*
*/

 http://www.cnitblog.com/zouzheng/archive/2011/01/12/72638.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.