1、先确保你的服务器有安装ant和Android SDK,sdk中至少要有你指定的target所对应的版本,如我的project.properties中指定的是:target=android-19

2、修改你的android项目下的build.xml,没有的创建一个。我的需求比较简单,无需为各个渠道build一个版本,只需一个就够了,然后apk的命名需加上AndroidManifest.xml里面的versionName,我的build.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project name="myapp">
    
	<!-- <property file="build.properties" /> -->
    <property name="jar.libs.dir" value="${jar.libs.dir}" />
	<property environment="env"/>
    <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 an env var"
        	unless="sdk.dir"/>

    <target name="-set-release-mode" depends="-set-mode-check">
        <!-- <mkdir dir="${out.absolute.dir}/${themanifest.manifest.android:versionName}"/> -->
        
        <!-- 解析 AndroidManifest.xml,获得versionname-->
        <xmlproperty file="AndroidManifest.xml" prefix="themanifest" collapseAttributes="true" /> 
        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-release-unsigned.apk" />
        <property name="out.final.file" location="${out.absolute.dir}/${apk.name}_${themanifest.manifest.android:versionName}-release.apk" />
        <property name="build.is.mode.set" value="true" />

        <!-- record the current build target -->
        <property name="build.target" value="release" />

        <property name="build.is.instrumented" value="false" />

        <!-- release mode is only valid if the manifest does not explicitly
             set debuggable to true. default is false. -->
        <xpath input="${manifest.abs.file}" expression="/manifest/application/@android:debuggable"
                output="build.is.packaging.debug" default="false"/>

        <!-- signing mode: release -->
        <property name="build.is.signing.debug" value="false" />

        <!-- Renderscript optimization level: aggressive -->
        <property name="renderscript.opt.level" value="${renderscript.release.opt.level}" />

        <if condition="${build.is.packaging.debug}">
            <then>
                <echo>*************************************************</echo>
                <echo>****  Android Manifest has debuggable=true   ****</echo>
                <echo>**** Doing DEBUG packaging with RELEASE keys ****</echo>
                <echo>*************************************************</echo>
            </then>
            <else>
                <!-- property only set in release mode.
                     Useful for if/unless attributes in target node
                     when using Ant before 1.8 -->
                <property name="build.is.mode.release" value="true"/>
            </else>
        </if>
        	<antcall target="clean"/>
    </target>

    <import file="${sdk.dir}/tools/ant/build.xml" />

    
    
</project>

上面大家注意到有sdk.dir等变量,这些变量在哪里配置呢,如果没用jenkins等工具的的话可以新建一个build.properties,里面的内容如下:

有些是可选的,根据你实际的情况

project.name=myapp
apk.name=myapp
jar.libs.dir=libs
build.first=false
key.alias=my_app
key.alias.password=123456
#java.dir=D\:\\Program Files\\Java\\jdk1.6.0_33
key.store=MY_APP
#project.dir=E\:\\appworkspace\\myapp
sdk.dir=D\:\\Android\\android-sdk
key.store.password=123456
#out.absolute.dir=E\:\\apk_dir

其中:key.store是你的签名用到的key,我放在放在项目更路径下,out.absolute.dir是build后的文件输出目录,不配置就生成到bin目录下,然后把上面的build.xml里面的

<property file="build.properties" />
加上。


如果用到jenkins的话, 把build里面的build.propertis那里注释掉,然后在配置页面里面的“构建”的propertis增加以上配置,以方便管理,如下:



注意:Targets要加上:clean release.




Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐