当前位置: 老葡京网站娱乐 > 操作系统 > 手机系统 > 正文

android ant最简单的打包签名及混淆方法

时间:2016-09-05 csdn博客 qq_25193681

老葡京网站娱乐 www.sdguanhua.com 使用ant打包,如果脚本都是我们自己一步一步来写的话,是一个比较麻烦的东西。

关于ant,我们详细看下:

ant支持 ant debug,ant release等命令,我们需要签名混淆,那么就需要ant release这个命令。

首先:ant环境搭建,这我就不多说了,很多博客都有介绍,包括一些环境配置

然后:ant环境可以之后,我们需要一个 build.xml文件,这个是ant编译去找的一个东西

<?xml version="1.0" encoding="UTF-8"?>
<project
    name="EPAPP"
    default="helpme" >
    <property file="local.properties" />
    <property file="ant.properties" />
    <loadproperties srcFile="project.properties" />
    <!-- quick check on sdk.dir -->
    <fail
        message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
        unless="sdk.dir" />
    <import
        file="custom_rules.xml"
        optional="true" />
    <!-- import file="build_of_sdk.xml" / -->
    <import file="${sdk.dir}/tools/ant/build.xml" />
	
    <target
        name="cleand"
        depends="clean" >
		<delete file="./assets/config.properties"/> 
	</target>

    <target
        name="d"
        depends="cleand" >

        <property
            name="out.final.file"
            value="./dist/develop/ep.apk" />
			
		<mkdir dir="./dist/develop"/>
			
        <antcall target="release" />
    </target>

    <target name="helpme">
        <echo>Android Ant Build For EPAPP. Available targets:</echo>
        <echo>   helpme:    Displays this help.</echo>
        <echo>   d:         Builds for final.</echo>
        <echo>              Final release for production.</echo>
        <echo>Please feel free to ask help from wangle@easipass.com</echo>
        <echo>Sorry for this messed English show. :) </echo>
    </target>
</project>

这个脚本对应  ant 的build.xml就简单了不是一点点了,不过注意配置环境变量哦。

其中 <import file="${sdk.dir}/tools/ant/build.xml" /> 则是导入ant自带的那个 build.xml,正因为导入了这个脚本 

<antcall target="release" />这句话才能正常执行

再然后:我们添加一个ant使用的配置脚本:这个脚本是由 ant的build.xml去调用执行的,我们就不管了

文件名为  ant.properties

proguard.config=proguard.cfg
key.store=./xxxxx.keystore
key.alias=xxxx
key.store.password=123456
key.alias.password=123456

URL:http://www.sdguanhua.com/OS/extra/201609/50425.htm

这个不用说也知道是干嘛用的了哦,打开混淆,导入签名啦。第一行是打开混淆,第二行往下则是导入签名,以及签名密码

至于签名,就不说啦,有了签名

key.store=./xxxxx.keystore 这个是给出签名的路径,我是直接将签名拷贝到了工程目录下了,你可以自己配置哦.

到这里架子已经搭建出来了哦,接下来就是根据自己的特殊需要,添加一些处理了

这里稍微花点控件介绍一下混淆

proguard.cfg这个是混淆脚本,由于一些三方包和android自带的四大组件等等,我们不能让他们混淆(三方包一般已经混淆过一次,再混淆则会出问题,而四大组建是因为它们会被android系统调用,混淆了,系统就找不到了),否则会出问题

关于json的混淆尤其要注意,可参考上一篇博客  http://blog.csdn.net/brightshadow11111/article/details/9469159

我把我的proguard.cfg脚本贴上来哦

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-dontoptimize
-ignorewarnings 
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keepattributes Signature
#这里是导入工程中所用到的一些三方包
-libraryjars libs/android-support-v4.jar
-libraryjars libs/umeng_sdk.jar
-libraryjars libs/android-wheel.jar
-libraryjars libs/com.springsource.org.apache.commons.beanutils-1.8.0.jar
-libraryjars libs/com.springsource.org.apache.commons.collections-3.2.1.jar
-libraryjars libs/com.springsource.org.apache.commons.lang-2.1.0.jar
-libraryjars libs/com.springsource.org.apache.commons.net-1.4.1.jar
-libraryjars libs/core.jar
-libraryjars libs/ezmorph-1.0.1.jar
-libraryjars libs/gson-1.7.1.jar
-libraryjars libs/json-lib-2.3-jdk15.jar
-libraryjars libs/ksoap2-android-assembly-3.0.0-RC.4-jar-with-dependencies.jar
-libraryjars libs/locSDK_3.1.jar
-libraryjars libs/mobiletools.jar
-libraryjars libs/pinyin4j-1.1.0.jar

-dontskipnonpubliclibraryclassmembers
-dontskipnonpubliclibraryclasses

#这里是对gson的处理
-keep class com.google.**{*;}
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature  
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }  
# Application classes that will be serialized/deserialized over Gson
<span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">-keep class com.<span style="margin: 0px; padding: 0px; font-family: Arial, Helvetica, sans-serif;">xxxx</span><span style="margin: 0px; padding: 0px; font-family: Arial, Helvetica, sans-serif;">.cmb.entity.** { *; }  </span></span>
#这里是对gson的用到的javabean处理
-keep class com.<span style="margin: 0px; padding: 0px; font-family: Arial, Helvetica, sans-serif;">xxxx</span><span style="margin: 0px; padding: 0px; font-family: Arial, Helvetica, sans-serif;">.ct.entity.** { *; }</span><br style="margin: 0px; padding: 0px;" />
-keep class com.xxxx.entity.** { *; } -keep class com.xxxxx.entity.** { *; } ##---------------End: proguard configuration for Gson -----------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-keep public class * extends net.sf.json.xml.XMLSerializer-keep class android.support.v4.** {*;} -keep interface android.support.v4.app.** {*;} -keep public class * extends android.support.v4.** -keep
 public class * extends android.app.Fragment-keep class com.google.gson.stream.** {*;} -keep class com.google.gson.examples.android.model.** {*;} -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 *;}-keep public class com.easipass.common.net.secret
#这里是不混淆三方包
-keep class com.motorola.** { *;}-keep class com.baidu.location.** {*;}-keep class com.google.zxing.** {*;}-keep class android.support.v4.** {*;}-keep class net.sourceforge.pinyin4j.** {*;}-keep class kankan.wheel.widget.** {*;}-keep class org.apache.commons.collections.** {*;}-keep class org.apache.commons.beanutils.** {*;}-keep class org.apache.commons.lang.** {*;}-keep class org.apache.commons.net.** {*;}-keep class net.sf.ezmorph.** {*;}-keep class net.sf.json.** {*;}-keep class com.easipass.util.tools.** {*;}-keep class com.umeng.analytics.** {*;}-keep class com.umeng.common.** {*;}-keep class com.easipass.common.net.** {*;}-keep class net.sf.** {*;}-keep class com.badlogic.** {*;}-keep class * implements com.badlogic.gdx.utils.Json*