對於Android開發者而言,最麻煩的就是屏幕適配和需要打包多個渠道的包!
下面提供一個快速打包的方法。
1.不需要把渠道號放在manifest文件中
如果我們使用的渠道號沒有必要非放在manifest文件中,我們可以選擇放在工程的assets文件夾下,因爲這個文件夾被編譯之後是沒有改變的。
例如:
1)我們把渠道號放在/assets/channel/channel.txt文件中。
2)然後生成生成一個沒有簽名的包(可以使用eclipse生成,也可以使用ant編譯出一個未簽名的apk)
3)使用rar壓縮來進行解壓、修改渠道號、壓縮、簽名。
- rd unsignedAPK\ /s /q
- rd signedAPK\ /s /q
- md signedAPK\
- md unsignedAPK\
- for /f "tokens=1-2 delims=','" %%i IN (chid_list.txt)do winrar x Test.apk Test_temp\&&del Test_temp\assets\channel\channel.txt&&echo %%i>>Test_temp\assets\channel\channel.txt&&winrar a -afzip -ep1 -r0 %%j.apk Test_temp\*.*&&rd Test_temp\ /s /q&© %%j.apk unsignedAPK\&&del %%j.apk&&jarsigner -verbose -keystore sign -storepass pass -signedjar signedAPK\%%j.apk unsignedAPK\%%j.apk biween
- rd unsignedAPK\ /s /q
- pause
上面是一個批處理文件,其中Test.apk 是你的未簽名的包,chidlist.txt是你需要打包的渠道號列表(格式是:2401,Test2.4.1_2401,逗號前面的是渠道號,後面的是簽名後生成的文件名),sign 是你的證書文件,pass 是證書的密碼。
試試效果吧,只要把上面的準備工作完成,點擊批處理,基本幾百個包5分鐘之內就能打完。
注:批處理依賴WinRAR,需要先裝Winrar
2.對於必須要在manifest裏面加渠道號的
有些情況下必須要把渠道號放在manifest裏面,比如個人開發者需要加入廣告,而廣告提供方提供的庫是從manifest裏面讀取渠道號的。
這種情況下就沒有什麼好辦法了,只能ant編譯,但也可以做一些優化。
具體的ant環境搭建就不再多說了,需要用到一個for循環的庫。
具體的實現方案就是執行一遍編譯把所需要的class,資源,等等都編譯出來,然後你會發現在/bin/裏面也會有一個manifest,這個是ant編譯的時候直接從工程裏拷過來的,我們要做的就是修改這個manifest裏的渠道號,然後重新編譯資源包,再簽名等等。
下面是ant編譯的腳本:
- <property name="market_channels_names" value="2361:EOE" /><!-- 這是1個渠道號,用:分開的,如果多個都以這種形式用逗號分開 -->
- <!-- 版本號 -->
- <property name="app_version" value="2.4.1 />
- <!-- 簽名信息 -->
- <property name="key.store" value="證書" />
- <property name="key.alias" value="alias名稱" />
- <property name="key.store.password" value="密碼" />
- <property name="key.alias.password" value="alias密碼" />
- <!-- 編碼 -->
- <property name="encoding" value="UTF-8" />
- <property name="out.channel.dir" value="out_channel" />
- <!-- 導入編譯文件 -->
- <import file="build_ext.xml" />
- <!-- 把各種資源打包成apk -->
- <target name="rebuild"
- depends="-set-release-mode, -release-obfuscation-check, -build-apk, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build"
- description="Builds the application in release mode.">
- </target>
- <!-- -build-setup -->
- <target name="-build-apk" depends="-build-setup,-package-resources">
- <!-- only package apk if *not* a library project -->
- <do-only-if-not-library elseText="Library project: do not package apk..." >
- <if condition="${build.is.instrumented}">
- <then>
- <package-helper>
- <extra-jars>
- <!-- Injected from external file -->
- <jarfile path="${emma.dir}/emma_device.jar" />
- </extra-jars>
- </package-helper>
- </then>
- <else>
- <package-helper />
- </else>
- </if>
- </do-only-if-not-library>
- </target>
- <target name="make" >
- <!-- 刪除並創建輸出文件夾(打包簽名好的文件) -->
- <delete dir="${out.channel.dir}"/>
- <mkdir dir="${out.channel.dir}" />
- <!-- 清空已編譯的文件,再編譯 -->
- <!-- -->
- <antcall target="clean" />
- <antcall target="release" />
- <foreach target="modify_manifest_channels" list="${market_channels_names}" param="nameandchannel" delimiter="," />
- </target>
- <!-- 修改channel, Umeng channel, Adview channel, channel值爲2361對應腳本中的${channelname},Umeng channel, Adview channel值爲EOE對應腳本中的${channelkey} -->
- <target name="modify_manifest_channels" >
- <!-- 獲取渠道名字 -->
- <propertyregex override="true" property="channelname" input="${nameandchannel}" regexp="(.*):" select="\1" />
- <!-- 獲取渠道號碼 -->
- <propertyregex override="true" property="channelkey" input="${nameandchannel}" regexp=":(.*)" select="\1" />
- <replaceregexp flags="g" byline="false">
- <regexp pattern="android:value="(.*)" android:name="CHANNEL"" />
- <substitution expression="android:value="${channelname}" android:name="CHANNEL"" />
- <fileset dir="${out.absolute.dir}" includes="AndroidManifest.xml" />
- </replaceregexp>
- <replaceregexp flags="g" byline="false">
- <regexp pattern="android:value="(.*)" android:name="UMENG_CHANNEL"" />
- <substitution expression="android:value="${channelkey}" android:name="UMENG_CHANNEL"" />
- <fileset dir="${out.absolute.dir}" includes="AndroidManifest.xml" />
- </replaceregexp>
- <replaceregexp flags="g" byline="false">
- <regexp pattern="android:name="AdView_CHANNEL" android:value="(.*)"" />
- <substitution expression="android:name="AdView_CHANNEL" android:value="${channelkey}"" />
- <fileset dir="${out.absolute.dir}" includes="AndroidManifest.xml" />
- </replaceregexp>
- <property
- name="out.release.file"
- location="${out.absolute.dir}/${ant.project.name}_${channelname}_${app_version}.apk" />
- <!-- 各版本輸出位置 -->
- <property
- name="out.final.file"
- location="${out.absolute.dir}/${ant.project.name}_${channelname}_${app_version}.apk" />
- <antcall target="rebuild" />
- <!-- 複製打包完的文件到渠道文件夾 -->
- <copy file="${out.final.file}" tofile="${out.channel.dir}/${ant.project.name}_${channelname}_${app_version}.apk"/>
- <!-- 刪除複製完成的文件 -->
- <delete file="${out.final.file}"/>
- <!-- -->
- <delete file="${out.absolute.dir}/${ant.project.name}-release-unsigned.apk" />
- <delete file="${out.absolute.dir}/${ant.project.name}-release-unaligned.apk" />
- </target>
通過上面的方法能夠提高一些打包的速度,但是也不是很快。
現在好點的打包方法介紹完了,如果哪位有更好的辦法,歡迎分享出來!