首页 > 代码库 > Android工程混淆打包:一个典型过程和示例文件
Android工程混淆打包:一个典型过程和示例文件
本例不含依赖工程的情况,如有依赖工程,请先导出为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=http://www.mamicode.com/"${sdk.dir}/tools/proguard/lib" />
<property name="sdk-platform" value=http://www.mamicode.com/"${sdk.dir}/platforms/android-2.2_r02-windows" />
<property name="sdk-platform-tools" value=http://www.mamicode.com/"${sdk.dir}/platforms/android-2.2_r02-windows/tools" />
<!-- 定义自己的输出目录 -->
<property name="outdir" value=http://www.mamicode.com/"outdir" />
<property name="out.absolute.dir" location="${outdir}" />
<!-- 读取目录 -->
<property name="resource-dir" value=http://www.mamicode.com/"res" />
<property name="asset-dir" value=http://www.mamicode.com/"assets" />
<property name="src-dir" value=http://www.mamicode.com/"src" />
<property name="src-dir-ospath" value=http://www.mamicode.com/"${basedir}/${src-dir}" />
<!-- 输出目录 -->
<property name="outdir-classes" value=http://www.mamicode.com/"${outdir}/classes" />
<property name="out.classes.absolute.dir" location="${outdir-classes}" />
<!-- 临时类资源文件 -->
<property name="dex-file" value=http://www.mamicode.com/"classes.dex" />
<property name="intermediate-dex" value=http://www.mamicode.com/"${out.absolute.dir}/${dex-file}" />
<!-- 最终生成文件 -->
<property name="out-package" value=http://www.mamicode.com/"${outdir}/${ant.project.name}.apk" />
<!-- 要使用的工具 -->
<property name="aapt" value=http://www.mamicode.com/"${sdk-platform-tools}/aapt.exe" />
<property name="aidl" value=http://www.mamicode.com/"${sdk-platform-tools}/aidl.exe" />
<property name="dx" value=http://www.mamicode.com/"${sdk-platform-tools}/dx.bat" />
<property name="sdk-tools" value=http://www.mamicode.com/"${sdk.dir}/tools" />
<property name="adb" value=http://www.mamicode.com/"${sdk-tools}/adb" />
<property name="apk-builder" value=http://www.mamicode.com/"${sdk-tools}/apkbuilder.bat" />
<property name="android-jar" value=http://www.mamicode.com/"${sdk-platform}/android.jar" />
<!-- 最终生成各种包文件 -->
<property name="resources-package" value=http://www.mamicode.com/"${outdir}/${ant.project.name}" />
<property name="resources-package-ospath" value=http://www.mamicode.com/"${basedir}/${resources-package}" />
<property name="out-unsigned-package" value=http://www.mamicode.com/"${outdir}/${ant.project.name}-unsigned.apk" />
<property name="out-unsigned-package-ospath" value=http://www.mamicode.com/"${basedir}/${out-unsigned-package}" />
<property name="out-signed-package" value=http://www.mamicode.com/"${outdir}/${ant.project.name}-signed.apk" />
<property name="out-signed-package-ospath" value=http://www.mamicode.com/"${basedir}/${out-signed-package}" />
<property name="zipalign-package-ospath" value=http://www.mamicode.com/"${basedir}/${outdir}/${ant.project.name}_release.apk" />
<!--签名工具-->
<property name="jdk-home" value=http://www.mamicode.com/"${java.home}" />
<property name="jarsigner" value=http://www.mamicode.com/"C:/Program Files/Java/jdk1.6.0_13/bin/jarsigner.exe" />
<property name="keystore" value=http://www.mamicode.com/"mykey.keystore" />
<property name="keypass" value=http://www.mamicode.com/"************************" />
<!--优化APK工具-->
<property name="zipalign" value=http://www.mamicode.com/"${sdk-tools}/zipalign.exe" />
<!-- 定义 R.java 所有目录 -->
<property name="outdir-gen" value=http://www.mamicode.com/"${src-dir}/com/baby" />
<property name="lib.dir" value=http://www.mamicode.com/"${basedir}/libs"/>
<property name="src.dir" value=http://www.mamicode.com/"${basedir}/src"/>
<property name="bin.dir" value=http://www.mamicode.com/"${basedir}/bin"/>
<!-- Directory for the third party java libraries -->
<property name="external.libs.dir" value=http://www.mamicode.com/"${basedir}/libs" />
<property name="external.libs.absolute.dir" location="${external.libs.dir}" />
<!-- Directory for the native libraries -->
<property name="native.libs.dir" value=http://www.mamicode.com/"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=http://www.mamicode.com/"package" />
<arg value=http://www.mamicode.com/"-m" />
<arg value=http://www.mamicode.com/"-J" />
<arg value=http://www.mamicode.com/"${outdir-gen}" />
<arg value=http://www.mamicode.com/"-M" />
<arg value=http://www.mamicode.com/"AndroidManifest.xml" />
<arg value=http://www.mamicode.com/"-S" />
<arg value=http://www.mamicode.com/"${resource-dir}" />
<arg value=http://www.mamicode.com/"-I" />
<arg value=http://www.mamicode.com/"${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=http://www.mamicode.com/"-p${android-framework}" />
<arg value=http://www.mamicode.com/"-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=http://www.mamicode.com/"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=http://www.mamicode.com/"-Dmaximum.inlined.code.length=32" />
<arg value=http://www.mamicode.com/"-injars temp.jar" />
<arg value=http://www.mamicode.com/"-outjars optimized.jar" />
<arg value=http://www.mamicode.com/"-libraryjars ${android-jar}" />
<arg value=http://www.mamicode.com/"-libraryjars ${external.libs.absolute.dir}" />
<arg value=http://www.mamicode.com/"@${basedir}/proguard.cfg" />
</java>
<delete file="temp.jar" />
<delete dir="${outdir-classes}" />
<mkdir dir="${outdir-classes}" />
<unzip src=http://www.mamicode.com/"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=http://www.mamicode.com/"--dex" />
<arg value=http://www.mamicode.com/"--output=${intermediate-dex}" />
<arg value=http://www.mamicode.com/"${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=http://www.mamicode.com/"package" />
<arg value=http://www.mamicode.com/"-f" />
<arg value=http://www.mamicode.com/"-M" />
<arg value=http://www.mamicode.com/"AndroidManifest.xml" />
<arg value=http://www.mamicode.com/"-S" />
<arg value=http://www.mamicode.com/"${resource-dir}" />
<arg value=http://www.mamicode.com/"-A" />
<arg value=http://www.mamicode.com/"${asset-dir}" />
<arg value=http://www.mamicode.com/"-I" />
<arg value=http://www.mamicode.com/"${android-jar}" />
<arg value=http://www.mamicode.com/"-F" />
<arg value=http://www.mamicode.com/"${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=http://www.mamicode.com/"${out-unsigned-package-ospath}" />
<arg value=http://www.mamicode.com/"-u" />
<arg value=http://www.mamicode.com/"-z" />
<arg value=http://www.mamicode.com/"${resources-package-ospath}" />
<arg value=http://www.mamicode.com/"-f" />
<arg value=http://www.mamicode.com/"${intermediate-dex}" />
<arg value=http://www.mamicode.com/"-rf" />
<arg value=http://www.mamicode.com/"${src-dir-ospath}" />
<arg value=http://www.mamicode.com/"-nf" />
<arg value=http://www.mamicode.com/"${native.libs.absolute.dir}" />
<arg value=http://www.mamicode.com/"-rj" />
<arg value=http://www.mamicode.com/"${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=http://www.mamicode.com/"-verbose" />
<arg value=http://www.mamicode.com/"-storepass" />
<arg value=http://www.mamicode.com/"${keypass}" />
<arg value=http://www.mamicode.com/"-keystore" />
<arg value=http://www.mamicode.com/"${keystore}" />
<arg value=http://www.mamicode.com/"-signedjar" />
<arg value=http://www.mamicode.com/"${out-signed-package-ospath}" />
<arg value=http://www.mamicode.com/"${out-unsigned-package-ospath}" />
<arg value=http://www.mamicode.com/"${keystore}" />
</exec>
</target>
<!--zipalign优化android程序-->
<target name="zipalign" depends="jarsigner">
<echo> zipalign ${zipalign-package-ospath}</echo>
<exec executable="${zipalign}" failonerror="true">
<arg value=http://www.mamicode.com/"-v" />
<arg value=http://www.mamicode.com/"-f" />
<arg value=http://www.mamicode.com/"4" />
<arg value=http://www.mamicode.com/"${out-signed-package-ospath}" />
<arg value=http://www.mamicode.com/"${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=http://www.mamicode.com/"install" />
<arg value=http://www.mamicode.com/"${out-signed-package}" />
</exec>
</target>
</project>
Android工程混淆打包:一个典型过程和示例文件