android ant打包問題總結


 用Ant打包的好處就在於可以打多個渠道 常用的就是修改友盟的渠道號,而不需要每改一次輸入一次keystore密碼的繁瑣過程。

android 不知道在什么版本之后tools目錄下就沒有在apkbuilder.bat這個文件了,如果從別人那邊 copy 過來也無法使用,提示“ apkbuilder 不穩定。。”。

所以在現在的版本上,進過查找各種資料 我在 ADT 22 版本下找到 Ant 新的解決方法,如有不對,不行請大家指出

1.Ant  和 android 環境變量配置,不多說了。

2.在 Android 工程目錄下 執行 android update project -p F:\workspace\AntTest   (-p 后加入工程目錄)

如果成功就會出現 

在工程目錄下就會出現 兩個個文件,build.xml ; local.properties ; 

閱讀build.xml 就會發現 里面有一句很重要的代碼:<import file="${sdk.dir}/tools/ant/build.xml" />

個人覺得這個是 Android 新版里添加的,為了減少開放者的時間,寫好了 “父” 版本,然后我們去繼承好了,瀏覽下“父” build.xml 發現的確沒有apkbuilder.bat這個文件在使用,替換的是

 

 <!-- find location of build tools -->
        <getbuildtools name="android.build.tools.dir" />
        <property name="aidl" location="${android.build.tools.dir}/aidl${exe}" />
        <property name="aapt" location="${android.build.tools.dir}/aapt${exe}" />
        <property name="dx" location="${android.build.tools.dir}/dx${bat}" />
        <property name="renderscript" location="${android.build.tools.dir}/llvm-rs-cc${exe}"/>

 

這幾個工具就llvm-rs-cs.exe 不同以前的了。

粗略了解之后就是自己的配置好 local.properties 這個文件,

sdk.dir=D:\\android-sdk-windows   
key.store=F:/workspace/AntTest/android.keystore
key.store.password=1234
key.alias=android.keystore
key.alias.password=1234
#android.library.reference.1=..\\SouTaoProject\\TopAndroid

配置好 SDK 目錄 簽名工具,最后一句后面再解釋。

Ok,這樣在工程目錄下運行命令 ant release 就會在bin目錄下生成一個 默認的MainActivity-release.apk

在不修改用 android update project 生成的 build.xml 功能能做的很少 比如 你的工程引入其他工程 Library 這些操作就無法滿足

所以 要支持 Library 需做一下操作:

1.在 引入的工程下執行 android update project -p  也生成一個 build.xml  不用去修改它,看看在project.properties文件中是否有這句話  android.library=true 沒有就添加。

2.在自己的工程目錄下 project.properties 是否有 android.library.reference.1=../Library/TopAndroid   目錄一定要寫對 不然會報  Fail find Library path。

最后就是執行 ant release 命令 這樣應該就可以通過。

最后就是多渠道問題了 這個我是直接引用其他博客上的

<target name="setup-channel">
        <property name="matchChannel.start" value='UMENG_CHANNEL\" android:value=\"' />
        <property name="matchChannel.end" value='\"' />            
        <replaceregexp file="AndroidManifest.xml" match='${matchChannel.start}[^"]*${matchChannel.end}' replace="${matchChannel.start}${meta.channel}${matchChannel.end}" />

    </target>
    <target name="build-channel" >
        <echo>channel: building ${meta.channel}</echo>
        <antcall target="setup-channel" />
        <antcall target="release" />
<move file="bin/${ant.project.name}-release.apk" tofile="bin/${ant.project.name}-${meta.channel}.apk"/>
    </target>
    <target name="channel_91" >
        <antcall target="build-channel">
            <param name="meta.channel" value="91"/>
        </antcall>
    </target>
    <target name="channel_hiapk" >
        <antcall target="build-channel">
            <param name="meta.channel" value="apk"/>
        </antcall>
    </target>

<target name="all" depends="channel_91,channel_hiapk">
    </target>

將以上代碼添加到 你的 build.xml 然后執行 ant all 就會在 bin目錄下生成對應的渠道apk。

可能以上只是針對比較簡單的工程打包 如果還有引用 JNI , so(以上方法試過好像可以通過)等還有帶考究。

以上是結合網絡多篇文章自己摸索出來的,可能有紕漏,請大俠支出。

看過的文章: http://www.cnblogs.com/qianxudetianxia/archive/2012/07/04/2573687.html  (這是舊sdk打包和ant最基礎的教程)

                http://my.eoe.cn/luoxiangyu001/archive/3430.html

      http://zgame.blog.51cto.com/6144241/1075003  (這個是舊的sdk打包方式)

      http://www.oschina.net/code/snippet_16_6782 (加入了混淆的)

                http://blog.csdn.net/smallbutstrong/article/details/8457064

                http://code.google.com/p/android/issues/detail?id=21720 (這個bbs討論了Library的問題  2樓里給了解決方式)

補充:

  1.每添加一個Library 就要在 local.properties這個文件里添加目錄,並在該Library里加入ant.properties;local.properties;build.xml這三個”初始“文件。

  2.如果遇到

error: No resource identifier found for attribute 'xxx' in package 'com.xxx.xxx' main.xml

我的修改方式是把 xmlns:app="http://schemas.android.com/apk/res/com.xxx.xxx"------>改成 xmlns:app="http://schemas.android.com/apk/lib/com.xxx.xxx"

參考地址:http://stackoverflow.com/questions/5819369/error-no-resource-identifier-found-for-attribute-adsize-in-package-com-googl 

3.非法字符 \ufeff...

  將該文件保存為UTF-8 NOT BOM

4.獲取Apk的版本

  

<xmlproperty file="AndroidManifest.xml" 
                     prefix="themanifest" 
                     collapseAttributes="true"/>
調用-->${themanifest.manifest.android:versionName}

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM