您的位置:首页 > 编程语言 > Java开发

ant批量打包混淆修改资源文件

2016-07-21 11:47 411 查看

一、开发环境

 操作系统:Windows

 

 JDK:1.6+

 

 开发工具:eclipse

 

 ant:apache-ant-1.9.4

 

二、安装和配置

 1、下载ant:apache-ant-1.9.4,另需下载一个支持for语句的jar文件 ant-contrib-1.0b3.jar,如下载本文中的ant文件,则已经自带了。如没有下载本文中的ant,那么需下载后放到ant根目录中的lib目录中。

 

 2、配置环境变量:

 

 ANT_HOME: D:\apache-ant-1.9.4-bin\apache-ant-1.9.4  此为你的ANT安装路径;

 

 PATH:    ;%ANT_HOME%\bin  注意前面的分号,如果PATH中最后没有分号“;”的话要加上,有的话就可去除。

 

 CLASSPATH: ;C:/apache-ant-1.8.1/lib 注意前面的分号,如果CLASSPATH中最后没有分号“;”的话要加上,有的话就可去除。

 

 3、测试ant是否安装成功

 

 输入 ant -version 如出现ant版本号则安装成功

 

 


 

三、ant脚本配置

 1、ant.properties 该文件如没有需要新建(该文件中的中文复制进项目中记得删除,因为properties文件不识别中文)

 

######################################################
#date
date=2016-07-19
#version code
vcode=260
#version name 以上为apk命名辅助属性
vname=2.6.0
#channels 渠道号用英文逗号分隔
market_channels=aaa,bbb
#app label app名称
app_labels=test

######################################################
####DO NOT MODIFY BELOW
######################################################
# the config file for batch package. application文件所在目录,不包括文件名
application.package=com.xxx.android.client
#对应build.xml文件中project属性name字段
ant.project.name=xxx
#字符编码
java.encoding=utf-8

#混淆输出临时文件
out.absolute.dir=C:/tmp
#打包签名APK输出目录
gos.path=C:/xxxPkg
#签名文件信息
key.store=E:/xxx.keystore
key.store.password=xxx123456
key.alias=xxx
key.alias.password=xxx123
#批量打包渠道号修改java文件所在目录,不包括文件名
src.const=src/com/xxx/android/client/common
#本地批量替换资源文件目录
local.res=C:/Users/xxx/Desktop/logos/logos
#项目中要替换的资源文件目录如果是需要
res.drawable.m=res/drawable-mdpi
res.drawable.h=res/drawable-hdpi
res.drawable.xh=res/drawable-xhdpi
res.drawable.xxh=res/drawable-xxhdpi

#channel pkgs apk文件包名
app_version_suffix=EnterPrise_on_${vcode}_${vname}_${date}_h


本地资源文件目录注意!!!!!

 1、上面文件中的 local.res=C:/Users/xxx/Desktop/logos/logos目录必须是用渠道号的名称命名,因为ant脚本运行时会去匹配和渠道号相应的目录。

 

 


 2、渠道号文件目录里的格式如下图,本文因为是要替换logo文件,所以文件名都相同,其他需求请自行研究。


 

  2、build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
<!--对应着ant.properties文件中的ant.project.name-->
name="xxx"
default="help" >

<!--
The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems.

-->
<!--引入项目中的local.properties文件-->
<property file="local.properties" />
<!--
The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant dpecific build properties.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.

-->
<!--引入项目中的ant.properties文件-->
<property file="ant.properties" />
<!--
if sdk.dir was not set from one of the property file, then
get it from the ANDROID_HOME env var.
This must be done before we load project.properties since
the proguard config can use sdk.dir

-->

<property environment="env" />

<condition
property="sdk.dir"
value="${env.ANDROID_HOME}" >

<isset property="env.ANDROID_HOME" />
</condition>
<!--
The project.properties file is created and updated by the 'android'
tool, as well as ADT.
This contains project dpecific properties such as project target, and library
dependencies. Lower level build properties are stored in ant.properties
(or in .clasdpath for Eclipse projects).
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.

-->
<!--引入项目中的project.properties文件-->
<loadprope
11bbd
rties 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 the ANDROID_HOME environment variable."
unless="sdk.dir" />
<!--
Import per project custom build rules if present at the root of the project.
This is the place to put custom intermediary targets such as:
-pre-build
-pre-compile
-post-compile (This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean

-->

<import
file="custom_rules.xml"
optional="true" />
<!--
Import the actual build file.
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.
***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"

-->
<!-- version-tag: 1 -->
<!-- 用于支持for、switch语句导入的包 -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties" >

<classpath>

<pathelement location="E:/tools/apache-ant-1.9.4/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<!--批量打包混淆核心代码-->
<target name="deploy" >
<!--循环调用modify_manifest target从而实现批量打包-->
<foreach
delimiter=","
list="${market_channels}"
param="channel"
target="modify_manifest" >
</foreach>
<!--如不需求混淆,把这段删掉即可-->
<antcall target="mapping" />
</target>
<!--修改manifest文件中的渠道号、java文件中的渠道号和替换资源文件-->
<target name="modify_manifest" >

<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >

<regexp pattern="android:name="Channel ID"\s+android:value="(.*)"" />

<substitution expression="android:name="Channel ID" android:value="${channel}"" />

<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>

<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >

<regexp pattern="android:name="Channel ID".*\n.*android:value="(.*)"" />

<substitution expression="android:name="Channel ID" android:value="${channel}"" />

<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>

<replaceregexp
byline="true"
encoding="utf-8"
flags="g" >
<regexp pattern="public\s+static\s+String\s+ChannelID\s+=.*$" />

<substitution expression="public static String ChannelID= "${channel}";" />

<fileset
dir="${src.const}"
includes="Constant.java" />
</replaceregexp>

<!--以下为替换资源文件,需先删除项目中的资源文件然后在复制进去。-->
<delete file="${res.drawable.m}/ic_launcher.png" />

<copy
file="${local.res}/${channel}/m/ic_launcher.png"
todir="${res.drawable.m}" >
</copy>

<delete file="${res.drawable.h}/ic_launcher.png" />

<copy
file="${local.res}/${channel}/h/ic_launcher.png"
todir="${res.drawable.h}" >
</copy>

<delete file="${res.drawable.xh}/ic_launcher.png" />

<copy
file="${local.res}/${channel}/xh/ic_launcher.png"
todir="${res.drawable.xh}" >
</copy>

<delete file="${res.drawable.xxh}/ic_launcher.png" />

<delete file="${res.drawable.xxh}/icon_login_logo.png" />

<!--批量复制文件到某个文件夹-->
<copy todir="${res.drawable.xxh}" >

<fileset dir="${local.res}/${channel}/xxh" >

<include name="ic_launcher.png" />

<include name="icon_login_logo.png" />

</fileset>
</copy>
<!--签名-->
<antcall target="release" />

<copy tofile="${gos.path}/${app_version_suffix}_${channel}.apk" >

<fileset
dir="${out.absolute.dir}/"
includes="${ant.project.name}-release.apk" />
</copy>

<delete includeEmptyDirs="true" >

<fileset
dir="${out.absolute.dir}"
excludes="**/proguard/*"
includes="**/*" />
</delete>

<echo message="===========================" />
</target>
<!--生成混淆的mapping文件-->
<target name="mapping" >

<copy tofile="${project.app.path}/${project_mapping_file_name}.txt" >

<fileset
dir="${out.absolute.dir}/proguard"
includes="mapping.txt" />
</copy>

<delete includeEmptyDirs="true" >

<fileset
dir="${out.absolute.dir}"
includes="**/*" />
</delete>

<echo message="===========================" />
</target>
<!--引入sdk中的build.xml文件-->
<import file="${sdk.dir}/tools/ant/build.xml" />

</project>


  3、local.properties

 

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.

# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read the
# header note.
#sdk的根目录
sdk.dir=E:\\tools\\sdk


  4、proguard-project.txt

 

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html 
# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

#optimization
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-optimizationpasses 5

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-verbose
-keepattributes SourceFile,LineNumberTable,Signature,Exceptions

####################################################################

#libs 项目中的jar文件不混淆
-libraryjars libs/android-async-http-1.4.6.jar
-libraryjars libs/avoscloud-v2.6.9.1.jar
-libraryjars libs/avospush-v2.6.9.1.jar
-libraryjars libs/avossearch-v2.6.9.1.jar
-libraryjars libs/avosstatistics-v2.6.9.1.jar
-libraryjars libs/baidumapapi_v3_5_0.jar
-libraryjars libs/fastjson.jar
-libraryjars libs/httpclient-4.3.5.jar
-libraryjars libs/httpcore-4.3.2.jar
-libraryjars libs/httpmime-4.3.5.jar
-libraryjars libs/locSDK_5.2.jar
-libraryjars libs/android-support-v4.jar
-libraryjars libs/gson.jar

####################################################################
#keep class for libraries 引用到jar文件的类不混淆
-keep class org.apache.** {*; }
-keep class com.loopj.** {*; }
-keep class com.google.gson.** {*; }
-keep class com.baidu.** {*; }
-keep class android.support.** {*; }
-keep class org.slf4j.** {*; }
-keep class com.android.volley.** {*; }
-keep class vi.com.gdi.bgl.android.**{*;}

#xiaomi push 推送不混淆
-keep class com.xiaomi.** {*; }
-keep class org.apache.thrift.** {*; }
-keep class com.avos.** {*; }
-keep class org.java_websocket.** {*; }

#alibaba pay 支付不混淆
-keep class com.alibaba.fastjson.** {*; }

#umeng statistics
-keep class u.aly.** {*; }
-keep class com.umeng.analytics.** {*; }

#http receiver 实体bean不混淆
-keep class com.xxx.android.client.bean.** {*; }

#jni 用到jni的地方不混淆
-keepclasseswithmembernames,allowshrinking class * {
native <methods>;
}

#R
-keepclassmembers class **.R$* {
public static <fields>;
}

#enum
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

#parcelable
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

#Serializable
-keep class * implements android.os.Serializable {
public static final android.os.Serializable$Creator *;
}
-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();
}
-keep public class * implements java.io.Serializable {*;}

#Activity, application, service, broadcastReceiver, contentprovider, fragment etc used in AndroidManifest.xml
-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
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment

#self-defined view used in layout xml files
-keep public class * extends android.view.View

###################################################################
-dontwarn android.support.**
-dontwarn org.apache.**
-dontwarn org.slf4j.**
-dontwarn com.android.volley.**
-dontwarn com.hbec.**
-dontwarn com.avos.**

# proguard.cfg 以下是leancloud推送不混淆的代码

-keepattributes Signature
-dontwarn com.jcraft.jzlib.**
-keep class com.jcraft.jzlib.**  { *;}

-dontwarn sun.misc.**
-keep class sun.misc.** { *;}

-dontwarn com.alibaba.fastjson.**
-keep class com.alibaba.fastjson.** { *;}

-dontwarn sun.security.**
-keep class sun.security.** { *; }

-dontwarn com.google.**
-keep class com.google.** { *;}

-dontwarn com.avos.**
-keep class com.avos.** { *;}

-keep public class android.net.http.SslError
-keep public class android.webkit.WebViewClient

-dontwarn android.webkit.WebView
-dontwarn android.net.http.SslError
-dontwarn android.webkit.WebViewClient

-dontwarn android.support.**

-dontwarn org.apache.**
-keep class org.apache.** { *;}

-dontwarn org.jivesoftware.smack.**
-keep class org.jivesoftware.smack.** { *;}

-dontwarn com.loopj.**
-keep class com.loopj.** { *;}

-dontwarn com.squareup.okhttp.**
-keep class com.squareup.okhttp.** { *;}
-keep interface com.squareup.okhttp.** { *; }

-dontwarn okio.**

-dontwarn org.xbill.**
-keep class org.xbill.** { *;}

-keepattributes *Annotation*

-keep class ** extends com.avos.avoscloud.im.v2.AVIMTypedMessage
-keepclassmembers  class ** extends com.avos.avoscloud.im.v2.AVIMTypedMessage{
*;
}


  5、project.properties

 

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#如不需要混淆则删除此代码
proguard.config=proguard-project.txt

# Project target.
target=android-19


 

四、ant脚本运行

使用命令行进入到项目的根目录里,然后输入 ant deploy 等待执行完成就行了。



 

遇到的问题

1、如引用到第三方项目,本文中是把第三方项目打包成jar文件,然后放入本项目中。

2、脚本运行时遇到过:编码 UTF-8 的不可映射字符。问题出在替换的java文件和manifest文件中,解决方法是在 replaceregexp 循环替换渠道号时加入 encoding=”utf-8” ,设置好编码格式就行了。

3、在替换资源文件时,本以为项目中有同名文件复制进去后可以直接替换,最后发现怎么都不成功,然后只能使用先删除,在复制进去,终于成功了。

4、sdk中的sdk\tools\ant\build.xml文件中的java.targetjava.source一般都是1.5的版本,这个版本会有一个问题,不支持switch中的一些关键字,但是我们运行项目、手动打包项目都不会有问题,只有在用ant批量打包时会遇到,所以要把这个版本改到1.7,前提是你的jdk版本有1.7的资源。

5、如果是用的Gson解析,那么映射字段时需要把所有的bean都实现序列化,不然bean会被混淆掉,这样映射服务端发下来的数据时,就会对应不上。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse windows jdk ant jar