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

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

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

[摘要]本文是对android自动打包方法(ant+proguard+签名)的讲解,对学习Android编程技术有所帮助,与大家分享。
-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 com.android.vending.licensing.ILicensingService
-injars ${project.jars}
-outjars ${obfuscated.jar.file}
-libraryjars ${android.libraryjars}
</proguard>
</then>
</if>
</target>
<target name="pre" depends="-obfuscate">
</target>
<!-- Converts this project's .class files into .dex files -->
<!--<target name="-dex" depends="compile, -post-compile, -obfuscate" unless="do.not.compile">-->
<target name="-dex" depends="compile, -post-compile, optimize" unless="do.not.compile">
<if condition="${manifest.hasCode}">
<then>
<dex-helper />
</then>
<else>
<echo>hasCode = false. Skipping...</echo>
</else>
</if>
</target>
<target name="optimize" depends="compile,-obfuscate">
<if condition="${proguard.enabled}">
<then>
<mkdir dir="${out.dir}/out/class" />
<!-- 创建文件夹-->
<!--别人的<jar basedir="${out-folder}" destfile="temp.jar"/>-->
<property name="proguard-jar" value="${proguard.home}/lib/proguard.jar" />
<java jar="${proguard-jar}" fork="true" failonerror="true">
<jvmarg value="-Dmaximum.inlined.code.length=32" />
<arg value="-injars ${out.dir}/classes" />
<!-- 原来的类文件,使用Bin/classes下的-->
<arg value="-outjars ${out.dir}/out/classes" />
<!-- 生成的混淆Class位置-->
<arg value="-libraryjars ${android-jar}" />
<!--
<arg value=" -libraryjars ${library-jar}/some_lib_used.jar"/>
-->
<arg value="-keep public class * extends android.app.Activity" />
<arg value="-keep public class * extends android.app.Service" />
<arg value="-keep public class * extends android.content.BroadcastReceiver" />
<arg value="-keep public class * extends android.content.ContentProvider" />
<arg value="-keep public class * extends android.view.View" />
<arg value="-dontwarn" />
<arg value="-dontpreverify" />
<arg value="-optimizationpasses 7" />
<arg value="-dontusemixedcaseclassnames" />
<arg value="-dontskipnonpubliclibraryclasses" />
<arg value="-repackageclasses" />
<arg value="-allowaccessmodification" />
<!--<arg value="-dontskipnonpubliclibraryclassmembers"/>-->
</java>
<!--这些是原来的Jar<delete file="temp.jar"/>-->
<!--<delete dir="${out-folder}"/>-->
<!--<mkdir dir="${out-folder}"/>
<unzip src="optimized.jar" dest="${out-folder}"/>
<delete file="optimized.jar"/>-->
</then>
</if>
</target>
<!-- Puts the project's resources into the output package file
This actually can create multiple resource package in case
Some custom apk with specific configuration have been
declared in default.properties.
-->
<target name="-package-resources">
<echo>Packaging resources</echo>
<aapt executable="${aapt}" command="package" versioncode="${version.code}" debug="${build.packaging.debug}" manifest="AndroidManifest.xml" assets="${asset.absolute.dir}" androidjar="${android.jar}" apkfolder="${out.absolute.dir}" resourcefilename="${resource.package.file.name}" resourcefilter="${aapt.resource.filter}">
<res path="${resource.absolute.dir}" />
<!-- <nocompress /> forces no compression on any files in assets or res/raw -->
<!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw -->
</aapt>
</target>
<!-- Packages the application and sign it with a debug key. -->
<target name="-package-debug-sign" depends="-dex, -package-resources">
<package-helper output.filepath="${out.debug.unaligned.file}" />
</target>
<!-- Packages the application without signing it. -->
<target name="-package-release" depends="-dex, -package-resources">
<package-helper output.filepath="${out.unsigned.file}" />
</target>
<target name="-compile-tested-if-test" if="tested.project.dir" unless="do.not.compile.again">
<subant target="compile">
<fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
</subant>
</target>
<target name="-debug-obfuscation-check">
<!-- proguard is never enabled in debug mode -->
<property name="proguard.enabled" value="true" />
</target>
<target name="-set-debug-mode" depends="-debug-obfuscation-check">
<!-- property only set in debug mode.
Useful for if/unless attributes in target node
when using Ant before 1.8 -->
<property name="build.mode.debug" value="true" />
<!-- whether the build is a debug build. always set. -->
<property name="build.packaging.debug" value="true" />
<!-- signing mode: debug -->
<property name="build.signing.debug" value="true" />
</target>
<!-- Builds debug output package, provided all the necessary files are already dexed -->
<target name="debug" depends="-set-debug-mode, -compile-tested-if-test, -package-debug-sign" description="Builds the application and signs it with a debug key.">
<zipalign-helper in.package="${out.debug.unaligned.file}" out.package="${out.debug.file}" />
<echo>Debug Package: ${out.debug.file}</echo>
</target>
<!-- called through target 'release'. Only executed if the keystore and
key alias are known but not their password. -->
<target name="-release-prompt-for-password" if="has.keystore" unless="has.password">
<!-- Gets passwords -->
<echo>Gets passwords ${has.keystore} ${has.password}</echo>
<input message="Please enter keystore password (store:${key.store}):" addproperty="key.store.password" defaultvalue="5201314.." />
<input message="Please enter password for alias '${key.alias}':" addproperty="key.alias.password" defaultvalue="5201314.." />
</target>
<!-- called through target 'release'. Only executed if there's no
keystore/key alias set -->
<target name="-release-nosign" unless="has.keystore">
<echo>No key.store and key.alias properties found in build.properties.</echo>
<echo>Please sign ${out.unsigned.file} manually</echo>
<echo>and run zipalign from the Android SDK tools.</echo>
</target>
<target name="-release-obfuscation-check">
<condition property="proguard.enabled" value="true" else="false">
<and>
<isset property="build.mode.release" />
<isset property="proguard.config" />
</and>
</condition>
<if condition="${proguard.enabled}">
<then>
<!-- Secondary dx input (jar files) is empty since all the
jar files will be in the obfuscated jar -->
<path id="out.dex.jar.input.ref" />
</then>
</if>
</target>
<target name="-set-release-mode">
<!-- release mode is only valid if the manifest does not explicitly
set debuggable to true. default is false.
We actually store build.packaging.debug, not build.release -->
<xpath input="AndroidManifest.xml" expression="/manifest/application/@android:debuggable" output="build.packaging.debug" default="false" />
<!-- signing mode: release -->
<property name="build.signing.debug" value="false" />
<if condition="${build.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.mode.release" value="true" />
</else>
</if>
</target>
<!-- This runs -package-release and -release-nosign first and then runs
only if release-sign is true (set in -release-check,
called by -release-no-sign)-->
<target name="release" depends="-set-release-mode, -release-obfuscation-check, -package-release, -release-prompt-for-password, -release-nosign" if="has.keystore" description="Builds the application. The generated apk file must be signed before
it is published.">
<!-- Signs the APK -->
<echo>Signing final apk...</echo>
<signjar jar="${out.unsigned.file}" signedjar="${out.unaligned.file}" keystore="${key.store}" storepass="${key.store.password}" alias="${key.alias}" keypass="${key.alias.password}" verbose="${verbose}" />
<!-- Zip aligns the APK -->
<zipalign-helper in.package="${out.unaligned.file}" out.package="${out.release.file}" />
<echo>Release Package: ${out.release.file}</echo>
</target>
<target name="clean" description="Removes output files created by other targets.">
<delete dir="${out.absolute.dir}" verbose="${verbose}" />
<delete dir="${gen.absolute.dir}" verbose="${verbose}" />
</target>
<target name="deployableAllDevice" description="build all device packet">
<!-- uid和sdk都是自定义参数,可有可无,有多小个包要打,就在这里copy多小行,修改相关参数,传入到程序里即可 -->
<antcall target="release" inheritAll="true"><param name="uid" value="100" /><param name="sdk" value="91" /></antcall>
<antcall target="release" inheritAll="true"><param name="uid" value="101" /><param name="sdk" value="90" /></antcall>
</target>
</project>
«上一页12下一页»


相关评论