大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Android技巧 > android自动打包方法(ant+proguard+签名)

android自动打包方法(ant+proguard+签名)(1)

关键词:android自动打包  阅读(1353) 赞(19)

[摘要]本文是对android自动打包方法(ant+proguard+签名)的讲解,对学习Android编程技术有所帮助,与大家分享。

前段时间做了一个android的网游项目,现在优化减少体积和防止别人反编译,需要把编译后.class进行混淆,开始在网上看了一些关于 ProGuard的介绍,基本上都是使用ADT自带的打包方式,那个打包方式太慢了,还要手工输密码,一个字烦。

于是开始寻找ant+proguard+签名的打包方式,遗憾的是资料不是缺手就是断脚。
好吧,看来得食自己了,!@#¥@#!@#!@##¥@#¥!@#@ 转眼一周,我++,终于把东西搞出来
ps:我们项目还有一个特殊需求,要把版本号,推广ID打到包里去,方便做推广什么的。这里可以用replace的方法对string.xml进行修改
好吧,废话不说了,直接上build文件
1. [代码][xml]代码 跳至 [1] [全屏预览]
<?xml version="1.0" encoding="UTF-8"?>
<project name="xiyou_base_" default="deployableAllDevice">
<!-- proguard4的路径 -->
<property name="proguard.home" value="D:/software/j2me/proguard4.5.1/proguard4.5.1"/>
<!-- sdk的路径 -->
<property name="sdk.dir" value="E:\dev\android-sdk-windows"/>
<!-- 是否使用签名 -->
<property name="has.keystore" value="true" />
<!-- 签名密码 -->
<property name="has.password" value="true" />
<!--签名相关的key -->
<property name="key.alias" value="key.keystore" />
<property name="key.store" value="key.keystore" />
<!-- 签名相关的密码 -->
<property name="key.store.password" value="xxxx" />
<property name="key.alias.password" value="xxxx" />
<!--
default.properties 内容
target=android-4
proguard.config=proguard.cfg
-->
<property file="default.properties" />
<!-- Custom Android task to deal with the project target, and import the
proper rules.
This requires ant 1.6.0 or above. -->
<path id="android.antlibs">
<pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
<pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
<pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
</path>
<taskdef name="setup" classname="com.android.ant.SetupTask" classpathref="android.antlibs" />
<setup import="false" />
<!-- Custom tasks -->
<taskdef name="aapt" classname="com.android.ant.AaptExecLoopTask" classpathref="android.antlibs" />
<taskdef name="aidl" classname="com.android.ant.AidlExecTask" classpathref="android.antlibs" />
<taskdef name="apkbuilder" classname="com.android.ant.ApkBuilderTask" classpathref="android.antlibs" />
<taskdef name="xpath" classname="com.android.ant.XPathTask" classpathref="android.antlibs" />
<taskdef name="if" classname="com.android.ant.IfElseTask" classpathref="android.antlibs" />
<!-- Properties -->
<!-- Tells adb which device to target. You can change this from the command line
by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for
the emulator. -->
<property name="adb.device.arg" value="" />
<property name="android.tools.dir" location="${sdk.dir}/tools" />
<property name="android.platform.tools.dir" location="${sdk.dir}/platform-tools" />
<!-- Name of the application package extracted from manifest file -->
<xpath input="AndroidManifest.xml" expression="/manifest/@package" output="manifest.package" />
<!-- Value of the hasCode attribute (Application node) extracted from manifest file -->
<xpath input="AndroidManifest.xml" expression="/manifest/application/@android:hasCode" output="manifest.hasCode" default="true" />
<!-- 源文件及资源路径 -->
<property name="source.dir" value="src" />
<property name="source.absolute.dir" location="${source.dir}" />
<property name="gen.dir" value="gen" />
<property name="gen.absolute.dir" location="${gen.dir}" />
<property name="resource.dir" value="res" />
<property name="resource.absolute.dir" location="${resource.dir}" />
<property name="asset.dir" value="assets" />
<property name="asset.absolute.dir" location="${asset.dir}" />
<!-- Directory for the third party java libraries -->
<property name="jar.libs.dir" value="libs" />
<property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />
<!-- create a path with all the jar files, from the main project and the
libraries -->
<path id="jar.libs.ref">
<fileset dir="${jar.libs.absolute.dir}" includes="*.jar" />
<path refid="project.libraries.jars" />
</path>
<!-- Directory for the native libraries -->
<property name="native.libs.dir" value="libs" />
<property name="native.libs.absolute.dir" location="${native.libs.dir}" />
<!-- 输出路径 -->
<property name="out.dir" value="out" />
<property name="out.absolute.dir" location="${out.dir}" />
<property name="out.classes.dir" value="${out.absolute.dir}/classes" />
<property name="out.classes.absolute.dir" location="${out.classes.dir}" />
<!-- Intermediate files -->
<property name="dex.file.name" value="classes.dex" />
<property name="intermediate.dex.file" location="${out.absolute.dir}/${dex.file.name}" />
<property name="resource.package.file.name" value="${ant.project.name}.ap_" />
<!-- The final package file to generate
These can be overridden by setting them earlier to
different values -->
<property name="out.debug.unaligned.file" location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
<property name="out.debug.file" location="${out.absolute.dir}/${ant.project.name}-debug.apk" />
<property name="out.unsigned.file.name" value="${ant.project.name}-unsigned.apk" />
<property name="out.unsigned.file" location="${out.absolute.dir}/${out.unsigned.file.name}" />
<property name="out.unaligned.file.name" value="${ant.project.name}-unaligned.apk" />
<property name="out.unaligned.file" location="${out.absolute.dir}/${out.unaligned.file.name}" />
<property name="out.release.file.name" value="${ant.project.name}-release.apk" />
<property name="out.release.file" location="${out.absolute.dir}/${out.release.file.name}" />
<property name="proguard.enabled" value="true" />
<property name="android-jar" value="${sdk.dir}/platforms/${target}/android.jar" />
<!-- set some properties used for filtering/override. If those weren't defined
before, then this will create them with empty values, which are then ignored
by the custom tasks receiving them. -->
<property name="version.code" value="" />
<property name="aapt.resource.filter" value="" />
<property name="filter.abi" value="" />
<!-- java源文件编码,编译的目标平台,为1.5 or 1.6都可以 -->
<property name="java.encoding" value="UTF-8" />
<property name="java.target" value="1.5" />
<property name="java.source" value="1.5" />
<!-- Verbosity -->
<property name="verbose" value="false" />
<!-- Verbosity -->
<property name="verbose" value="false" />
<!-- This is needed by emma as it uses multilevel verbosity instead of simple 'true' or 'false'
The property 'verbosity' is not user configurable and depends exclusively on 'verbose'
value.-->
<condition property="verbosity" value="verbose" else="quiet">
<istrue value="${verbose}" />
</condition>
<!-- This is needed to switch verbosity of zipalign. Depends exclusively on 'verbose'
-->
<condition property="v.option" value="-v" else="">
<istrue value="${verbose}" />
</condition>
<!-- This is needed to switch verbosity of dx. Depends exclusively on 'verbose' -->
<condition property="verbose.option" value="--verbose" else="">
<istrue value="${verbose}" />
</condition>
<!-- properties for signing in release mode -->
<condition property="has.keystore" value="true">
<and>
<isset property="key.store" />
<length string="${key.store}" when="greater" length="0" />
<isset property="key.alias" />
</and>
</condition>
<condition property="has.password" value="passwordxxxxx">
<and>
<isset property="has.keystore" />
<isset property="key.store.password" />
<isset property="key.alias.password" />
</and>
</condition>
<!-- Tools -->
<condition property="exe" value=".exe" else="">
<os family="windows" />
</condition>
<property name="adb" location="${android.platform.tools.dir}/adb${exe}" />
<property name="zipalign" location="${android.tools.dir}/zipalign${exe}" />
<!-- Emma configuration -->
<property name="emma.dir" value="${sdk.dir}/tools/lib" />
<path id="emma.lib">
<pathelement location="${emma.dir}/emma.jar" />
<pathelement location="${emma.dir}/emma_ant.jar" />
</path>
<taskdef resource="emma_ant.properties" classpathref="emma.lib" />
<!-- End of emma configuration -->
<!-- Macros -->
<!-- Configurable macro, which allows to pass as parameters output directory,
output dex filename and external libraries to dex (optional) -->
<macrodef name="dex-helper">
<element name="external-libs" optional="yes" />
<element name="extra-parameters" optional="yes" />
<sequential>
<!-- sets the primary input for dex. If a pre-dex task sets it to
something else this has no effect -->
<property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" />
<!-- set the secondary dx input: the project (and library) jar files
If a pre-dex task sets it to something else this has no effect -->
<if>
<condition>
<isreference refid="out.dex.jar.input.ref" />
</condition>
<else>
<path id="out.dex.jar.input.ref">
<path refid="jar.libs.ref" />
</path>
</else>
</if>
<echo>Converting compiled files and external libraries into ${intermediate.dex.file}...</echo>
<apply executable="${dx}" failonerror="true" parallel="true">
<arg value="--dex" />
<arg value="--output=${intermediate.dex.file}" />
<extra-parameters />
<arg line="${verbose.option}" />
<arg path="${out.dex.input.absolute.dir}" />
<path refid="out.dex.jar.input.ref" />
<external-libs />
</apply>
</sequential>
</macrodef>
<!-- This is macro that enable passing variable list of external jar files to ApkBuilder
Example of use:
<package-helper output.filepath="/path/to/foo.apk">
<extra-jars>
<jarfolder path="my_jars" />
<jarfile path="foo/bar.jar" />
<jarfolder path="your_jars" />
</extra-jars>
</package-helper> -->
<macrodef name="package-helper">
<attribute name="output.filepath" />
<element name="extra-jars" optional="yes" />
<sequential>
<apkbuilder outfolder="${out.absolute.dir}" resourcefile="${resource.package.file.name}" apkfilepath="@{output.filepath}" debugpackaging="${build.packaging.debug}" debugsigning="${build.signing.debug}" abifilter="${filter.abi}" verbose="${verbose}" hascode="${manifest.hasCode}">
<dex path="${intermediate.dex.file}" />
<sourcefolder path="${source.absolute.dir}" />
<sourcefolder refid="project.libraries.src" />
<jarfolder path="${jar.libs.absolute.dir}" />
<jarfolder refid="project.libraries.libs" />
<nativefolder path="${native.libs.absolute.dir}" />
<nativefolder refid="project.libraries.libs" />
<extra-jars />
</apkbuilder>
</sequential>
</macrodef>
<!-- This is macro which zipaligns in.package and outputs it to out.package. Used by targets
debug, -debug-with-emma and release.-->
<macrodef name="zipalign-helper">
<attribute name="in.package" />
<attribute name="out.package" />
<sequential>
<echo>Running zip align on final apk...</echo>
<exec executable="${zipalign}" failonerror="true">
<arg line="${v.option}" />
<arg value="-f" />
<arg value="4" />
<arg path="@{in.package}" />
<arg path="@{out.package}" />
</exec>
</sequential>
</macrodef>
<!-- This is macro used only for sharing code among two targets, -install and
-install-with-emma which do exactly the same but differ in dependencies -->
<macrodef name="install-helper">
<sequential>
<echo>Installing ${out.debug.file} onto default emulator or device...</echo>
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}" />
<arg value="install" />
<arg value="-r" />
<arg path="${out.debug.file}" />
</exec>
</sequential>
</macrodef>
<!-- Rules -->
<!-- Creates the output directories if they don't exist yet. -->
<target name="-dirs">
<echo>Creating output directories if needed...</echo>
<mkdir dir="${resource.absolute.dir}" />
<mkdir dir="${jar.libs.absolute.dir}" />
<mkdir dir="${out.absolute.dir}" />
<if condition="${manifest.hasCode}">
<then>
<mkdir dir="${gen.absolute.dir}" />
<mkdir dir="${out.classes.absolute.dir}" />
</then>
</if>
</target>
<!-- empty default pre-build target. Create a similar target in
your build.xml and it'll be called instead of this one. -->
<target name="-pre-build" />
<!-- Generates the R.java file for this project's resources. -->
<target name="-resource-src" depends="-dirs, -pre-build">
<if condition="${manifest.hasCode}">
<then>
<echo>Generating R.java / Manifest.java from the resources...</echo>
<aapt executable="${aapt}" command="package" verbose="${verbose}" manifest="AndroidManifest.xml" androidjar="${android.jar}" rfolder="${gen.absolute.dir}">
<res path="${resource.absolute.dir}" />
</aapt>
</then>
<else>
<echo>hasCode = false. Skipping...</echo>
</else>
</if>
</target>
<!-- Generates java classes from .aidl files. -->
<target name="-aidl" depends="-dirs">
<if condition="${manifest.hasCode}">
<then>
<echo>Compiling aidl files into Java classes...</echo>
<aidl executable="${aidl}" framework="${android.aidl}" genFolder="${gen.absolute.dir}">
<source path="${source.absolute.dir}" />
<source refid="project.libraries.src" />
</aidl>
</then>
<else>
<echo>hasCode = false. Skipping...</echo>
</else>
</if>
</target>
<!-- empty default pre-compile target. Create a similar target in
your build.xml and it'll be called instead of this one. -->
<target name="-pre-compile" />
<!-- Compiles this project's .java files into .class files. -->
<target name="compile" depends="-resource-src, -aidl, -pre-compile" description="Compiles project's .java files into .class files">
<if condition="${manifest.hasCode}">
<then>
<!-- If android rules are used for a test project, its classpath should include
tested project's location -->
<condition property="extensible.classpath" value="${tested.project.absolute.dir}/${out.dir}/classes" else=".">
<isset property="tested.project.absolute.dir" />
</condition>
<condition property="extensible.libs.classpath" value="${tested.project.absolute.dir}/libs" else="${jar.libs.dir}">
<isset property="tested.project.absolute.dir" />
</condition>
<javac encoding="${java.encoding}" source="${java.source}" target="${java.target}" debug="true" extdirs="" destdir="${out.classes.absolute.dir}" bootclasspathref="android.target.classpath" verbose="${verbose}" classpath="${extensible.classpath}" classpathref="jar.libs.ref">
<src path="${source.absolute.dir}" />
<src path="${gen.absolute.dir}" />
<src refid="project.libraries.src" />
<classpath>
<fileset dir="${extensible.libs.classpath}" includes="*.jar" />
</classpath>
</javac>
</then>
<else>
<echo>hasCode = false. Skipping...</echo>
</else>
</if>
</target>
<!-- empty default post-compile target. Create a similar target in
your build.xml and it'll be called instead of this one. -->
<target name="-post-compile" />
<!-- Obfuscate target
This is only active in release builds when proguard.config is defined
in default.properties.
To replace Proguard with a different obfuscation engine:
Override the following targets in your build.xml, before the call to <setup>
-release-obfuscation-check
Check whether obfuscation should happen, and put the result in a property.
-debug-obfuscation-check
Obfuscation should not happen. Set the same property to false.
-obfuscate
** Make sure unless="do.not.compile" is used in the target definition **
check if the property set in -debug/release-obfuscation-check is set to true.
If true:
Perform obfuscation
Set property out.dex.input.absolute.dir to be the output of the obfuscation
-->
<target name="-obfuscate" unless="do.not.compile">
<if condition="${proguard.enabled}">
<then>
<property name="obfuscate.absolute.dir" location="${out.absolute.dir}/proguard" />
<property name="preobfuscate.jar.file" value="${obfuscate.absolute.dir}/original.jar" />
<property name="obfuscated.jar.file" value="${obfuscate.absolute.dir}/obfuscated.jar" />
<!-- input for dex will be proguard's output -->
<property name="out.dex.input.absolute.dir" value="${obfuscated.jar.file}" />
<!-- Add Proguard Tasks -->
<property name="proguard.jar" location="${proguard.home}/lib/proguard.jar" />
<taskdef name="proguard" classname="proguard.ant.ProGuardTask" classpath="${proguard.jar}" />
<!-- Set the android classpath Path object into a single property. It'll be
all the jar files separated by a platform path-separator.
-->
<property name="android.libraryjars" refid="android.target.classpath" />
<!-- Build a path object with all the jar files that must be obfuscated.
This include the project compiled source code and any 3rd party jar
files. -->
<path id="project.jars.ref">
<pathelement location="${preobfuscate.jar.file}" />
<path refid="jar.libs.ref" />
</path>
<!-- Set the project jar files Path object into a single property. It'll be
all the jar files separated by a platform path-separator.
-->
<property name="project.jars" refid="project.jars.ref" />
<mkdir dir="${obfuscate.absolute.dir}" />
<delete file="${preobfuscate.jar.file}" />
<delete file="${obfuscated.jar.file}" />
<jar basedir="${out.classes.dir}" destfile="${preobfuscate.jar.file}" />
<!-- 混淆相关参数 -->
<proguard>
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-repackageclasses
-allowaccessmodification
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
«上一页12下一页»


相关评论