本例不含依赖工程的情况,如有依赖工程,请先导出为jar包再打包:
1. 基于Android 2.1以上版本;
2. 签名的密钥文件生成;
1). 创建证书:keytool -genkey -alias mykey.keystore -keyalg RSA -keystore mykey.keystore 密码同系统登陆密码
3). 查看证书库:keytool -list -keystore mykey.keystore
4). 导出到证书文件:keytool -export -alias mykey.keystore -file mykey.crt -keystore mykey.keystore
5). 导入证书的信息:keytool -import -keystore mykey.keystore -file mykey.crt mykey.crt导入到名为mykey.keystore的证书库中
6). 查看证书信息:keytool -printcert -file mykey.crt
7). 删除密钥库中的条目:
keytool -list -keystore mykey.keystore
keytool -delete -keystore mykey.keystore -alias mykey.keystore
8). 修改证书条目的口令:keytool -keypasswd -alias mykey.keystore -keystore mykey.keystore
3. proguard.cfg文件
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-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 com.android.vending.licensing.ILicensingService
#使用xml布局并且混淆apk时,请在混淆配置中加入以下这行代码
-keep class com.mediav.** {*;}
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
4. Build.xml文件
<?xml version="1.0" encoding="GBK"?>
<!-- 步骤
1.用aapt生成R.java
2.JAVAC编译JAVA文件
3.批处理生成classes.dex
4.aapt打包资源
5.apkbuilder包资源和类文件为APK文件
6.jarsinger 对APK进行签名-->
<project name="ieltsls" default="zipalign" basedir=".">
<property file="local.properties" />
<property name="proguard-home" value="${sdk.dir}/tools/proguard/lib" />
<property name="sdk-platform" value="${sdk.dir}/platforms/android-2.2_r02-windows" />
<property name="sdk-platform-tools" value="${sdk.dir}/platforms/android-2.2_r02-windows/tools" />
<!-- 定义自己的输出目录 -->
<property name="outdir" value="outdir" />
<property name="out.absolute.dir" location="${outdir}" />
<!-- 读取目录 -->
<property name="resource-dir" value="res" />
<property name="asset-dir" value="assets" />
<property name="src-dir" value="src" />
<property name="src-dir-ospath" value="${basedir}/${src-dir}" />
<!-- 输出目录 -->
<property name="outdir-classes" value="${outdir}/classes" />
<property name="out.classes.absolute.dir" location="${outdir-classes}" />
<!-- 临时类资源文件 -->
<property name="dex-file" value="classes.dex" />
<property name="intermediate-dex" value="${out.absolute.dir}/${dex-file}" />
<!-- 最终生成文件 -->
<property name="out-package" value="${outdir}/${ant.project.name}.apk" />
<!-- 要使用的工具 -->
<property name="aapt" value="${sdk-platform-tools}/aapt.exe" />
<property name="aidl" value="${sdk-platform-tools}/aidl.exe" />
<property name="dx" value="${sdk-platform-tools}/dx.bat" />
<property name="sdk-tools" value="${sdk.dir}/tools" />
<property name="adb" value="${sdk-tools}/adb" />
<property name="apk-builder" value="${sdk-tools}/apkbuilder.bat" />
<property name="android-jar" value="${sdk-platform}/android.jar" />
<!-- 最终生成各种包文件 -->
<property name="resources-package" value="${outdir}/${ant.project.name}" />
<property name="resources-package-ospath" value="${basedir}/${resources-package}" />
<property name="out-unsigned-package" value="${outdir}/${ant.project.name}-unsigned.apk" />
<property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />
<property name="out-signed-package" value="${outdir}/${ant.project.name}-signed.apk" />
<property name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" />
<property name="zipalign-package-ospath" value="${basedir}/${outdir}/${ant.project.name}_release.apk" />
<!--签名工具-->
<property name="jdk-home" value="${java.home}" />
<property name="jarsigner" value="C:/Program Files/Java/jdk1.6.0_13/bin/jarsigner.exe" />
<property name="keystore" value="mykey.keystore" />
<property name="keypass" value="************************" />
<!--优化APK工具-->
<property name="zipalign" value="${sdk-tools}/zipalign.exe" />
<!-- 定义 R.java 所有目录 -->
<property name="outdir-gen" value="${src-dir}/com/baby" />
<property name="lib.dir" value="${basedir}/libs"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="bin.dir" value="${basedir}/bin"/>
<!-- Directory for the third party java libraries -->
<property name="external.libs.dir" value="${basedir}/libs" />
<property name="external.libs.absolute.dir" location="${external.libs.dir}" />
<!-- Directory for the native libraries -->
<property name="native.libs.dir" value="libs" />
<property name="native.libs.absolute.dir" location="${native.libs.dir}" />
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<!-- 初始化 -->
<target name="init">
<echo>Creating all output directories </echo>
<!--delete dir="${outdir}" /-->
<delete dir="${outdir-classes}" />
<mkdir dir="${outdir}" />
<mkdir dir="${outdir-classes}" />
<!--mkdir dir="${outdir-gen}" /-->
</target>
<!--步骤 1: aapt生成R.java -->
<target name="resource-src" depends="init">
<echo>Generating R.java...</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-m" />
<arg value="-J" />
<arg value="${outdir-gen}" />
<arg value="-M" />
<arg value="AndroidManifest.xml" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
</exec>
</target>
<!-- Generate java classes from .aidl files. -->
<target name="aidl" depends="init">
<echo>Compiling aidl files into Java classes...</echo>
<apply executable="${aidl}" failonerror="true">
<arg value="-p${android-framework}" />
<arg value="-I${src-dir}" />
<fileset dir="${src-dir}">
<include name="**/*.aidl" />
</fileset>
</apply>
</target>
<!-- 步骤 2. 编译所有class文件. -->
<target name="compile" depends="init, resource-src, aidl">
<javac srcdir="${src.dir}" destdir="${outdir-classes}" target="1.6" encoding="UTF-8" classpathref="classpath" bootclasspath="${android-jar}"/>
</target>
<!-- 混淆代码 参照proguard.cfg -->
<target name="proguard" depends="compile">
<jar basedir="${outdir-classes}" destfile="temp.jar" />
<java jar="${proguard-home}/proguard.jar" fork="true" failonerror="true">
<jvmarg value="-Dmaximum.inlined.code.length=32" />
<arg value="-injars temp.jar" />
<arg value="-outjars optimized.jar" />
<arg value="-libraryjars ${android-jar}" />
<arg value="-libraryjars ${external.libs.absolute.dir}" />
<arg value="@${basedir}/proguard.cfg" />
</java>
<delete file="temp.jar" />
<delete dir="${outdir-classes}" />
<mkdir dir="${outdir-classes}" />
<unzip src="optimized.jar" dest="${outdir-classes}" />
<delete file="optimized.jar" />
</target>
<!-- 步骤 3. 转换class文件为dex文件. -->
<target name="dex" depends="proguard">
<echo>Converting compiled files into ${intermediate-dex}... </echo>
<exec executable="${dx}" failonerror="true">
<arg value="--dex" />
<arg value="--output=${intermediate-dex}" />
<arg value="${external.libs.absolute.dir}/*.jar" />
<arg path="${out.classes.absolute.dir}" />
</exec>
</target>
<!-- 步骤 4. 将资源和清单打包. -->
<target name="package-res-and-assets">
<echo>Packaging resources and assets...</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-f" />
<arg value="-M" />
<arg value="AndroidManifest.xml" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-A" />
<arg value="${asset-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
<arg value="-F" />
<arg value="${resources-package}" />
</exec>
</target>
<!-- 步骤 5. 将打好的包编译为APK文件. -->
<target name="debug" depends="dex, package-res-and-assets">
<echo>release:Packaging ${out-debug-package-ospath}, and signing it with a debug key...</echo>
<exec executable="${apk-builder}" failonerror="true">
<arg value="${out-unsigned-package-ospath}" />
<arg value="-u" />
<arg value="-z" />
<arg value="${resources-package-ospath}" />
<arg value="-f" />
<arg value="${intermediate-dex}" />
<arg value="-rf" />
<arg value="${src-dir-ospath}" />
<arg value="-nf" />
<arg value="${native.libs.absolute.dir}" />
<arg value="-rj" />
<arg value="${external.libs.absolute.dir}" />
</exec>
</target>
<!--步骤 6. 签名APK文件 -->
<target name="jarsigner" depends="debug">
<echo> jarsigner ${out-signed-package-ospath}</echo>
<exec executable="${jarsigner}" failonerror="true">
<arg value="-verbose" />
<arg value="-storepass" />
<arg value="${keypass}" />
<arg value="-keystore" />
<arg value="${keystore}" />
<arg value="-signedjar" />
<arg value="${out-signed-package-ospath}" />
<arg value="${out-unsigned-package-ospath}" />
<arg value="${keystore}" />
</exec>
</target>
<!--zipalign优化android程序-->
<target name="zipalign" depends="jarsigner">
<echo> zipalign ${zipalign-package-ospath}</echo>
<exec executable="${zipalign}" failonerror="true">
<arg value="-v" />
<arg value="-f" />
<arg value="4" />
<arg value="${out-signed-package-ospath}" />
<arg value="${zipalign-package-ospath}" />
</exec>
</target>
<!-- 安装到默认的手机或模拟器-->
<target name="install" depends="zipalign">
<echo>Installing ${out-debug-package} onto default emulator...</echo>
<exec executable="${adb}" failonerror="true">
<arg value="install" />
<arg value="${out-signed-package}" />
</exec>
</target>
</project>
原文地址:http://blog.csdn.net/liu76xt/article/details/43152527