Android打包之Eclipse打渠道包

Android自动打包流程详细图:


步骤一:

在工程中新建一个build.xml.

 

步骤二:

给工程配置ant工具。

 

选择ant工具的步骤如下:

Windows->Shown view->Ant 这样就出项了ant视图框。在ant视图框中单击右键,选择addBuilderfile添加ant编译文件。

添加好ant编译文件后,我们还需要对编译文件进行设置:选中编译文件,右键单击选择Run As->external Tools configurations

弹出了ant编译的配置框。

在编译ant文件时,我们常常遇到编译进程卡住现象,这是因为ant编译器找不到日志输出文件造成的。可以在ant编译框中的Main->Argument文本框中添加如下语句:-logger org.apache.tools.ant.NoBannerLogger,这样指定ant编译时的日志编译器。

 

如果在编译工程时,遇到不可映射的字符集(工程字符集和ant编译器的字符集不一致)时,可以修改ant的编译时字符集

步骤如下:在ant编译配置框中选择common->Console Encoding设置ant的字符集

 

步骤三:

编写build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="AndroidTouchDemo">
	<!--     指定配置文件      -->
	<property file="default.properties">
	</property>
	<property file="local.properties">
	</property>

	<!--     定义工具目录      -->
	<property environment="env">
	</property>
	<property name="platforms.target" value="android-19">
	</property>
	<property name="build.tools.target" value="19.1.0">
	</property>
	<property name="sdk.dir" value="${env.ANDROID_SDK}">
	</property>
	<property name="android.tools.dir" value="${sdk.dir}/tools">
	</property>
	<property name="android.buildtools.dir" value="${sdk.dir}/build-tools/${build.tools.target}">
	</property>
	<property name="android.platformtools.dir" value="${sdk.dir}/platform-tools">
	</property>
	<property name="android.platforms.dir" value="${sdk.dir}/platforms/${platforms.target}">
	</property>
	<property name="android.ant.dir" value="${sdk.dir}/tools/ant">
	</property>

	<!--
		<echo message="sdk.dir:${sdk.dir}"/>
		<echo message="android.tools.dir:${android.tools.dir}"/>
		<echo message="android.platformtools.dir:${android.platformtools.dir}"/>
		<echo message="android.platforms.dir:${android.platforms.dir}"/>
		<echo message="android.ant.dir:${android.ant.dir}"/> 
	-->
	<echo message="android.ant.dir:${android.ant.dir}"/>

	<property name="android.tools.absolute.dir" location="${android.tools.dir}">
	</property>
	<property name="android.platformtools.absolute.dir" location="${android.platformtools.dir}">
	</property>
	<property name="android.platforms.absolute.dir" location="${android.platforms.dir}">
	</property>
	<property name="android.buildtools.absolute.dir" location="${android.buildtools.dir}">
	</property>
	<property name="android.ant.absolute.dir" location="${android.ant.dir}">
	</property>

	<!--
		<echo message="android.tools.absolute.dir:${android.tools.absolute.dir}"/>
		<echo message="android.platformtools.absolute.dir:${android.platformtools.absolute.dir}"/>
		<echo message="android.platformtools.dir:${android.platformtools.dir}"/>
		<echo message="android.platforms.absolute.dir:${android.platforms.absolute.dir}"/> 
		<echo message="android.ant.absolute.dir:${android.ant.absolute.dir}"/> 
	-->

	<!--     定义工具   -->
	<property name="proguard.jar" location="${android.tools.dir}/proguard/lib/proguard.jar">
	</property>
	<property name="android.jar" location="${android.platforms.absolute.dir}/android.jar">
	</property>
	<property name="tools.dx" location="${android.buildtools.dir}/dx">
	</property>
	<property name="tools.apkbuilder" location="${android.buildtools.absolute.dir}/apkbuilder">
	</property>
	<property name="tools.adb" location="${android.platformtools.absolute.dir}/adb">
	</property>
	<property name="tools.zipalign" location="${android.buildtools.dir}/zipalign">
	</property>
	<property name="tools.aapt" location="${android.buildtools.absolute.dir}/aapt">
	</property>
	<property name="tools.aidl" location="${android.buildtools.absolute.dir}/aidl">
	</property>
	<property name="tools.jarsigner" location="${env.JAVA_HOME}/bin/jarsigner">
	</property>

	<property name="channelname" value="" />
	<property name="channelkey" value="" />
	<!-- 渠道名:渠道号 -->
	<property name="key" value="UM:aaaaaa,BAI:bbbb" />

	<!-- 
		<echo message="proguard.jar: ${proguard.jar}"/>
		<echo message="android.jar: ${android.jar}"/>
		<echo message="tools.dx:${tools.dx}"/>
		<echo message="tools.apkbuilder:${tools.apkbuilder}"/>
		<echo message="tools.adb:${tools.adb}"/>
		<echo message="tools.zipalign:${tools.zipalign}"/>
		<echo message="tools.aapt:${tools.aapt}"/>
		<echo message="tools.aidl:${tools.aidl}"/>
		<echo message="tools.jarsigner:${tools.jarsigner}"/>
	-->

	<!--     定义源代码及资源等输入目录       -->
	<property name="in.source.dir" value="src">
	</property>
	<property name="in.resource.dir" value="res">
	</property>
	<property name="in.asset.dir" value="assets">
	</property>

	<property name="in.source.absolute.dir" location="${in.source.dir}">
	</property>
	<property name="in.resource.absolute.dir" location="${in.resource.dir}">
	</property>
	<property name="in.asset.absolute.dir" location="${in.asset.dir}">
	</property>

	<!-- 
		<echo message="in.source.dir:${in.source.dir}"/>
		<echo message="in.resource.dir:${in.resource.dir}"/>
		<echo message="in.asset.dir:${in.asset.dir}"/>
	-->

	<!--     定义本地库/第三方工具库文件目录        -->
	<property name="in.external.libs.dir" value="libs">
	</property>
	<property name="in.native.libs.dir" value="libs">
	</property>

	<property name="in.external.libs.absolute.dir" location="${in.external.libs.dir}">
	</property>
	<property name="in.native.libs.absolute.dir" location="${in.native.libs.dir}">
	</property>

	<!-- 
		<echo message="in.external.libs.dir:${in.external.libs.dir}"/>
		<echo message="in.native.libs.dir:${in.native.libs.dir}"/>
	-->

	<!--     定义输入文件      -->
	<property name="in.manifest.file" value="AndroidManifest.xml">
	</property>
	<property name="in.android.aidl.file" value="${android.platforms.dir}/framework.aidl">
	</property>

	<property name="in.manifest.absolute.file" location="${in.manifest.file}">
	</property>
	<property name="in.android.aidl.absolute.file" location="${in.android.aidl.file}">
	</property>

	<!-- 
		<echo message="in.manifest.file:${in.manifest.file}"/>
		<echo message="in.android.aidl.file:${in.android.aidl.file}"/>
	-->

	<!--     定义输出文件目录    -->
	<property name="out.gen.dir" value="gen">
	</property>
	<property name="out.dir" value="bin">
	</property>
	<property name="out.classes.dir" value="${out.dir}/classes">
	</property>

	<property name="out.gen.absolute.dir" location="${out.gen.dir}">
	</property>
	<property name="out.absolute.dir" location="${out.dir}">
	</property>
	<property name="out.classes.absolute.dir" location="${out.classes.dir}">
	</property>

	<!-- 
		<echo message="out.gen.dir:${out.gen.dir}"/>
		<echo message="out.dir:${out.dir}"/>
		<echo message="out.classes.dir:${out.classes.dir}"/>
	-->

	<!--     定义输出文件          -->
	<property name="out.dex.file" value="${ant.project.name}-classes.dex">
	</property>
	<property name="out.resource.package.file" value="${ant.project.name}-resource.apk">
	</property>
	<property name="out.unsigned.package.file" value="${ant.project.name}-unsigned.apk">
	</property>
	<property name="out.signed.package.file" value="${ant.project.name}-signed.apk">
	</property>
	<property name="out.aligned.package.file" value="${ant.project.name}-aligned.apk">
	</property>
	<property name="release.package.file" value="${ant.project.name}-release.apk">
	</property>

	<property name="out.dex.absolute.file" location="${out.dir}/${out.dex.file}">
	</property>
	<property name="out.resource.package.absolute.file" location="${out.dir}/${out.resource.package.file}">
	</property>
	<property name="out.unsigned.package.absolute.file" location="${out.dir}/${out.unsigned.package.file}">
	</property>
	<property name="out.signed.package.absolute.file" location="${out.dir}/${out.signed.package.file}">
	</property>
	<property name="out.aligned.package.absolute.file" location="${out.dir}/${out.aligned.package.file}">
	</property>
	<property name="release.package.absolute.file" location="${out.dir}/${release.package.file}">
	</property>

	<!-- 
		<echo message="out.dex.file:${out.dex.file}"/>
		<echo message="out.resource.package.file:${out.resource.package.file}"/>
		<echo message="out.unsigned.package.file:${out.unsigned.package.file}"/>
		<echo message="out.signed.package.file:${out.signed.package.file}"/>
		<echo message="out.aligned.package.file:${out.aligned.package.file}"/>
		<echo message="release.package.file:${release.package.file}"/>
		<echo message="out.dex.absolute.file:${out.dex.absolute.file}"/>	
		<echo message="out.resource.package.absolute.file:${out.resource.package.absolute.file}"/>
		<echo message="out.unsigned.package.absolute.file:${out.unsigned.package.absolute.file}"/>
		<echo message="out.signed.package.absolute.file:${out.signed.package.absolute.file}"/>
		<echo message="out.aligned.package.absolute.file:${out.aligned.package.absolute.file}"/>
		<echo message="release.package.absolute.file:${release.package.absolute.file}"/>
	-->

	<property name="keystore" location="release.keystore">
	</property>
	<property name="password" value="!@#$%^">
	</property>
	<property name="keyalias" value="release">
	</property>

	<!-- 
		<echo message="keystore:${keystore}"/>
		<echo message="password:${password}"/>
		<echo message="keyalias:${keyalias}"/>
	-->

	<!--  
	        <target>标签  
	        一个项目标签下可以有一个或多个target标签。一个target标签可以依赖其他的target标签。例  
	        如,有一个target用于编译程序,另一个target用于声称可执行文件。在生成可执行文件之前必  
	        须先编译该文件,因策可执行文件的target依赖于编译程序的target。Target的所有属性如下。  
	            (1)name表示标明,这个属性是必须的。  
	            (2)depends表示依赖的目标。  
	            (3)if表示仅当属性设置时才执行。  
	            (4)unless表示当属性没有设置时才执行。  
	            (5)description表示项目的描述。  
	        Ant的depends属性指定了target的执行顺序。Ant会依照depends属性中target出现顺序依次执行  
	        每个target。在执行之前,首先需要执行它所依赖的target。程序中的名为run的target的  
	        depends属性compile,而名为compile的target的depends属性是prepare,所以这几个target执  
	        行的顺序是prepare->compile->run。  
	        一个target只能被执行一次,即使有多个target依赖于它。如果没有if或unless属性,target总  
	        会被执行。     
	-->
	<target name="-clean">
		<echo>Creating output directories if needed...</echo>
		<delete dir="${out.absolute.dir}" />
		<delete dir="${out.gen.absolute.dir}" />
	</target>

	<target name="-dirs" depends="-clean">
		<echo>Creating output directories if needed...</echo>
		<mkdir dir="${in.resource.absolute.dir}" />
		<mkdir dir="${in.external.libs.absolute.dir}" />
		<mkdir dir="${out.gen.absolute.dir}" />
		<mkdir dir="${out.absolute.dir}" />
		<mkdir dir="${out.classes.absolute.dir}" />
	</target>

	<!--  
	        第一步 生成R.java类文件:  
	        Eclipse中会自动生成R.java,ant和命令行使用android SDK提供的aapt.ext程序生成R.java。  
	    -->
	<target name="-resource-src" depends="-dirs">
		<echo>Generating R.java / Manifest.java from the resources...</echo>
		<exec executable="${tools.aapt}" failοnerrοr="true">
			<arg value="package" />
			<arg value="-m" />
			<arg value="-J" />
			<arg path="${out.gen.absolute.dir}" />
			<arg value="-M" />
			<arg path="${in.manifest.absolute.file}" />
			<arg value="-S" />
			<arg path="${in.resource.absolute.dir}" />
			<arg value="-I" />
			<arg path="${android.jar}" />
		</exec>
	</target>

	<!--  
	        第二步 将.aidl文件生成.java类文件:  
	        Eclipse中自动生成,ant和命令行使用android SDK提供的aidl.exe生成.java文件。   
	    -->
	<!-- Generates java classes from .aidl files. -->
	<target name="-aidl" depends="-dirs">
		<echo>Compiling aidl files into Java classes...</echo>
		<apply executable="${tools.aidl}" failοnerrοr="true">
			<arg value="-p${in.android.aidl.file}" />
			<arg value="-I${in.source.absolute.dir}" />
			<arg value="-o${out.gen.absolute.dir}" />
			<fileset dir="${in.source.absolute.dir}">
				<include name="**/*.aidl" />
			</fileset>
		</apply>
	</target>

	<!--  
	        第三步 编译.java类文件生成class文件:  
	        Eclipse中自动生成,ant和命令行使用jdk的javac编译java类文件生成class文件。   
	         -->
	<!-- Compiles this project's .java files into .class files. -->
	<target name="compile" depends="-resource-src, -aidl" >
		<echo>Compiles project's .java files into .class files</echo>
		<javac encoding="utf-8" target="1.5" debug="true" extdirs="" srcdir="."   
	            destdir="${out.classes.absolute.dir}" bootclasspath="${android.jar}">
			<classpath>
				<fileset dir="${in.external.libs.absolute.dir}" includes="*.jar" />
				<fileset dir="${in.external.libs.absolute.dir}" includes="*.zip" />
			</classpath>
		</javac>
	</target>

	<!--Execute proguard class flies-->
	<target name="optimize" depends="compile">
		<echo>optimize classes are put to "${out.absolute.dir}"   .</echo>
		<jar basedir="${out.classes.absolute.dir}" destfile="${out.absolute.dir}/temp.jar"/>
		<taskdef resource="proguard/ant/task.properties"  classpath="${proguard.jar}" />
		<proguard>    
	            -injars                 ${out.absolute.dir}/temp.jar     
	            -outjars                    ${out.absolute.dir}/optimized.jar  
	            -libraryjars                ${android.jar}     
	            -optimizationpasses 5  
	            -dontusemixedcaseclassnames  
	            -dontskipnonpubliclibraryclasses  
	            -dontpreverify  
	            -verbose  
	            -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*  
	            -include proguard-project.txt    
				
				-keep public class * extends android.app.Activity
				-keep public class * extends android.app.Application
				-keep public class * extends android.app.Service
				-keep public class * extends android.content.BroadcastReceiver
				-keep public class * extends android.content.ContentProvider
				-keep public class * extends android.app.backup.BackupAgentHelper
				-keep public class * extends android.preference.Preference
				-keep public class * extends com.android.vending.licensing.ILicensingService
				-keep public class * extends android.os.*
				-keep public class * extends android.view.*
				-keep public class * extends android.widget.*
				-keep interface * extends java.lang.*
				-keep interface * extends android.*
	          </proguard>
		<delete file="${out.absolute.dir}/temp.jar"/>
		<delete dir="${out.classes.dir}" failοnerrοr="false" />
		<mkdir dir="${out.classes.dir}"/>
		<unzip src="${out.absolute.dir}/optimized.jar" dest="${out.classes.absolute.dir}"/>
		<delete file="${out.absolute.dir}/optimized.jar"/>
	</target>

	<!--  
	        第四步 将class文件打包生成classes.dex文件:  
	        Eclipse中自动生成,ant和命令行使用android SDK提供的dx.bat命令行脚本生成classes.dex文件。   
	    -->
	<target name="dex" depends="optimize">
		<echo>Converting compiled files and external libraries into ${out.absolute.dir}/${out.dex.file}    ...</echo>
		<apply executable="${tools.dx}" failοnerrοr="true" parallel="true">
			<arg value="--dex" />
			<arg value="--output=${out.dex.absolute.file}" />
			<arg path="${out.classes.absolute.dir}" />
			<fileset dir="${in.external.libs.absolute.dir}" includes="*.jar"/>
		</apply>
		<delete dir="${out.classes.absolute.dir}"/>
	</target>

	<!--  
	        第五步 打包资源文件(包括res、assets、androidmanifest.xml等):  
	        Eclipse中自动生成,ant和命令行使用Android SDK提供的aapt.exe生成资源包文件。   
	    -->
	<target name="package-resource">
		<echo>Packaging resources and assets ${out.resource.package.absolute.file}    ...</echo>
		<exec executable="${tools.aapt}" failοnerrοr="true">
			<arg value="package" />
			<arg value="-f" />
			<arg value="-M" />
			<arg value="${in.manifest.file}" />
			<arg value="-S" />
			<arg value="${in.resource.absolute.dir}" />
			<arg value="-A" />
			<arg value="${in.asset.absolute.dir}" />
			<arg value="-I" />
			<arg value="${android.jar}" />
			<arg value="-F" />
			<arg value="${out.resource.package.absolute.file}" />
		</exec>
	</target>

	<!--  
	        第六步 生成未签名的apk安装文件:  
	        Eclipse中自动生成debug签名文件存放在bin目录中,ant和命令行使用android SDK提供的apkbuilder.bat命令脚本生成未签名的apk安装文件。   
	    -->
	<!-- Package the application without signing it.  This allows for the application to be signed later with an official publishing key. -->
	<target name="package" depends="dex, package-resource">
		<echo>Packaging ${out.unsigned.package.absolute.file} for release...</echo>
		<exec executable="${tools.apkbuilder}" failοnerrοr="true">
			<arg value="${out.unsigned.package.absolute.file}" />
			<arg value="-u" />
			<arg value="-z" />
			<arg value="${out.resource.package.absolute.file}" />
			<arg value="-f" />
			<arg value="${out.dex.absolute.file}" />
			<arg value="-rf" />
			<arg value="${in.source.absolute.dir}" />
			<arg value="-rj" />
			<arg value="${in.external.libs.absolute.dir}" />
		</exec>
		<echo>It will need to be signed with jarsigner before being published.</echo>
		<delete file="${out.resource.package.absolute.file}" />
		<delete file="${out.dex.absolute.file}" />
	</target>

	<!-- 
	        第七步 对未签名的apk进行签名生成签名后的android文件: 
	    -->
	<!-- Package the application without signing it.  This allows for the application to be signed later with an official publishing key. -->
	<target name="jarsigner" depends="package">
		<echo>Packaging ${out.unsigned.package.absolute.file} for release...</echo>
		<exec executable="${tools.jarsigner}" failοnerrοr="true">
			<arg value="-keystore" />
			<arg value="${keystore}" />
			<arg value="-storepass" />
			<arg value="${password}" />
			<arg value="-keypass" />
			<arg value="${password}" />
			<arg value="-signedjar" />
			<arg value="${out.signed.package.absolute.file}" />
			<arg value="${out.unsigned.package.absolute.file}" />
			<arg value="${keyalias}" />
		</exec>
		<delete file="${out.unsigned.package.absolute.file}" />
	</target>

	<!-- 
	        第七步 签名的文件进行字节对齐; 
	    -->
	<target name="zipalign" depends="jarsigner">
		<echo>Zipalign ${out.aligned.package.absolute.file} for release...</echo>
		<exec executable="${tools.zipalign}">
			<arg value="-f" />
			<arg value="-v" />
			<arg value="4" />
			<arg value="${out.signed.package.absolute.file}" />
			<arg value="${out.aligned.package.absolute.file}" />
		</exec>
		<delete file="${out.signed.package.absolute.file}" />
	</target>

	<!-- 
	        第八步 签名的文件进行字节对齐; 
	    -->
	<target name="release" depends="zipalign">
		<!--<copy tofile="${release.package.absolute.file}">
			<fileset dir="${out.absolute.dir}" includes="${out.aligned.package.file}"/>
		</copy> -->
		<copy tofile="${out.dir}-${channelname}/${ant.project.name}-${channelname}-${channelkey}-release.apk">
			<fileset dir="${out.absolute.dir}" includes="${out.aligned.package.file}" />
		</copy>
		<delete file="${out.aligned.package.absolute.file}" />
	</target>

	<!-- 使用第三方的ant包,使ant支持for循环-->
	<taskdef resource="net/sf/antcontrib/antcontrib.properties">
		<classpath>
			<pathelement location="${android.ant.absolute.dir}/ant-contrib-1.0b3.jar" />
		</classpath>
	</taskdef>

	<!--循环打包 -->
	<target name="deploy">
		<echo>deploy ...</echo>
		<foreach target="modify_manifest" list="${key}" param="nameandchannel" delimiter=",">
		</foreach>
	</target>

	<target name="modify_manifest">
		<echo>modify_manifest ...</echo>
		<echo>modify_manifest nameandchannel:${nameandchannel} ...</echo>
		<!-- 获取渠道名字 -->
		<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" encoding="UTF-8">
			<regexp pattern='meta-data android:value="(.*)" android:name="app_key"' />
			<substitution expression='meta-data android:value="${channelkey}" android:name="app_key"' />
			<fileset dir="" includes="AndroidManifest.xml" />
		</replaceregexp>
		<antcall target="release" />
	</target>

</project>

第四步:

执行project的build即可。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值