Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> android ant最簡單的打包簽名及混淆方法

android ant最簡單的打包簽名及混淆方法

編輯:Android開發教程

使用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 [email protected]</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.bianceng.cn/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*
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved