一、理论知识  

ProGuard是一款免费的Java类文件压缩器、优化器和混淆器。它能发现并删除无用类、字段(field)、方法和属性值(attribute)。它也能优化字节码并删除无用的指令。最后,它使用简单无意义的名字来重命名你的类名、字段名和方法名。经过以上操作的jar文件会变得更小,并很难进行逆向工程。
二、基本使用
  在Android应用程序也可以使用ProGuard来进行混洗打包,大大的优化Apk包的大小。但是注意ProGuard对文件路径的名名很有讲究,不支持括号,也不支持空格。在混淆过后,可以在工程目录的proguard中的mapping.txt看到混淆后的类名,方法名,变量名和混淆前的类名,方法名,变量名。

   在使用Eclipse或Ant打包应用程序时,都是使用Android工程目录的project.properties文件来指定配置。关于Android中如何使用ant打包请参考《Android中使用Ant编译打包
在使用Eclipse新建一个工程,都会在工程目录下生产配置project.properties和proguard-project.tx。
文件如下所示:
例1
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
 
# Project target.
target=android-10
project.properties用于配置Android工程的一些属性,#号的话表示当前行是注释,这里的proguard.config就用于指定ProGuard的混淆配置文件,并对使用release方式打包应用程序时开启代码混淆功能。对于是否是使用release方式打包,和AndroidManifest.xml中application的android:debuggable属性有很多关系。如果该值为android:debuggable="true",那么最终就是debug方式打包。最明智的方式就是在AndroidManifest.xml并不显示的指定它,而是是打包工具在打包时来决定它最终的值。对于ant就是ant release或ant debug。而对于直接在Eclipse中使用run 或debgu来打包的话就是debug,使用export的话就是release.

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

这里的话指定了混淆的基本配置文件proguard-android.txt,和混淆的个性化配置文件proguard-project.txt。这里proguard-project.txt文件用于对前面的基本的混淆配置文件proguard-android.txt的配置进行override和添加。
混淆的基本配置文件proguard-android.txt如下:
文件1

# This is a configuration file for ProGuard. # http://proguard.sourceforge.net/index.html#manual/usage.html -dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -verbose # Optimization is turned off by default. Dex does not like code run # through the ProGuard optimize and preverify steps (and performs some # of these optimizations on its own). -dontoptimize -dontpreverify # Note that if you want to enable optimization, you cannot just # include optimization flags in your own project configuration file; # instead you will need to point to the # "proguard-android-optimize.txt" file instead of this one from your # project.properties file. -keepattributes *Annotation* -keep public class com.google.vending.licensing.ILicensingService -keep public class com.android.vending.licensing.ILicensingService # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native -keepclasseswithmembernames class * { native <methods>; } # keep setters in Views so that animations can still work. # see http://proguard.sourceforge.net/manual/examples.html#beans -keepclassmembers public class * extends android.view.View { void set*(***); *** get*(); } # We want to keep methods in Activity that could be used in the XML attribute onClick -keepclassmembers class * extends android.app.Activity { public void *(android.view.View); } # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } -keepclassmembers class **.R$* { public static <fields>; } # The support library contains references to newer platform versions. # Don't warn about those in case this app is linking against an older # platform version. We know about them, and they are safe. -dontwarn android.support.**

以下则个是我们项目混淆的个性化配置文件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 *;
#}

-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-dontwarn android.**
-dontwarn edu.edut.lsf.payment.link.**
-libraryjars ..\Download_Install\lib\classes.jar

-keep class org.jboss.netty.util.internal.AtomicFieldUpdaterUtil
-keep class org.jboss.netty.util.internal.AtomicFieldUpdaterUtil$Node
-keep class org.jboss.netty.util.internal.LinkedTransferQueue$Node
-keep class edu.edut.robin.activities.LeWebJsActivity$AppStoreInterface
-keepclasseswithmembers class * {
public static void main(java.lang.String[]);
}
-keepclasseswithmembers class org.jboss.netty.util.internal.AtomicFieldUpdaterUtil$Node {
*;
}

-keepclasseswithmembers class edu.edut.robin.activities.LeWebActionActivity$AppstoreWebInterface {
*;
}
-keepclasseswithmembers class edu.edut.robin.utils.SilentInstallAssistant$* {
*;
}
-keepclasseswithmembers class edu.edut.robin.silentinstaller.utils.SilentInstallAssistant$* {
*;
}
-keepclasseswithmembers class edu.edut.robin.utils.Pm$* {
*;
}
-keepclasseswithmembers class org.jboss.netty.util.internal.LinkedTransferQueue {
volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node head;
volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node tail;
volatile transient int sweepVotes;

}
-keepclasseswithmembers class org.jboss.netty.util.internal.LinkedTransferQueue$Node {
*;
}
-keepclasseswithmembers class edu.edut.robin.activities.LeWebJsActivity$AppStoreInterface {
*;
}
-keepclasseswithmembers class * extends edu.edut.lsf.payment.WebSubmitInterface {
*;
}
-keepclasseswithmembers class edu.edut.lsf.payment.WebSubmitInterface {
*;
}
-keep public class com.unionpay.** {*; }
-keep public class edu.edut.lsf.** {*; }

:由于牵扯到保密的问题,一些关于项目的东西换成了edu.edut或edu.edut.robin

三、混淆配置详解
另外以下是关于混淆配置文件的一些说明:
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable 保留原代码的行号信息
-injars  androidtest.jar【jar包所在地址】 
-outjars  out【输出地址】
-libraryjars    'D:\android-sdk-windows\platforms\android-9\android.jar' 【引用的库的jar,用于解析injars所指定的jar类】
 
-optimizationpasses 5
-dontusemixedcaseclassnames 【混淆时不会产生形形色色的类名 】puzzle
-dontskipnonpubliclibraryclasses 【指定不去忽略非公共的库类。 】 puzzle
-dontpreverify 【不预校验】
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 【优化】puzzle
-keep public class * extends android.app.Activity  【不进行混淆类名的类,保持其原类名和包名】
 
-keep public abstract interface com.asqw.android.Listener{
public protected <methods>;  【所有public protected的方法名不进行混淆】
}
-keep public class com.asqw.android{
public void Start(java.lang.String); 【对该方法不进行混淆】
}
-keepclasseswithmembernames class * { 【对所有类的native方法名不进行混淆】
native <methods>;
}
-keepclasseswithmembers class * { 【对所有类的指定方法的方法名不进行混淆】
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclassmembers class * extends android.app.Activity {【对所有类的指定方法的方法名不进行混淆】
public void *(android.view.View);
}
-keepclassmembers enum * {【对枚举类型enum的所有类的以下指定方法的方法名不进行混淆】
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {【对实现了Parcelable接口的所有类的类名不进行混淆,对其成员变量为Parcelable$Creator类型的成员变量的变量名不进行混淆】
public static final android.os.Parcelable$Creator *;
}
-keepclasseswithmembers class org.jboss.netty.util.internal.LinkedTransferQueue {【对指定类的指定变量的变量名不进行混淆】
volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node head;
volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node tail;
volatile transient int sweepVotes;

}

-keep public class com.unionpay.** {*; }【对com.unionpay包下所有的类都不进行混淆,即不混淆类名,也不混淆方法名和变量名】
结束!

Android混淆打包的更多相关文章

  1. Android混淆打包配置总结

    Android打包失败出现Proguard returned with error code 1. See console的错误 这个问题是由于代码混淆引起的,找不到引用包. 只需在你的proguar ...

  2. android -------- 混淆打包报错(warning - InnerClass annotations are missing corresponding EnclosingMember annotations)

    最近做Android混淆打包遇到一些问题,Android Sdutio 3.1 版本打包的 错误如下: Android studio warning - InnerClass annotations ...

  3. android——混淆打包

    网上搜了一大堆,在此不一一赘诉. 直接讲解了 如上图这么配置,其实就是加上一句话而已.告诉打包工具混淆打包的代码放在./proguard-project.txt这里 proguard.config=. ...

  4. Android 混淆打包

    有些时候我们希望我们自己的apk包不能被别人反编译而获取自己的源代码.这就需要我们通过Android提供的混淆打包技术来完成. 一.没有引用外部包的情况: 这种情况下代码混淆的方式相对简单: 1)只需 ...

  5. Android 混淆打包不混淆第三方jar包

    项目由于要公布,所以要混淆打包. 混淆打包流程: 1.在proguard-project.txt文件里加入不须要混淆的类和第三方的jar包   这个是保持自己包中不须要混淆的类,假设有些类调用了jni ...

  6. 微信android混淆打包减少安装包大小

    首先,感谢微信android团队的分享 微信中的资源混淆工具主要为了混淆资源ID长度(例如将res/drawable/welcome.png混淆为r/s/a.png),同时利用7z深度压缩,大大减少了 ...

  7. android混淆打包配置(忽略第三方jar)

    在project.properties里加上   proguard.config=proguard.cfg proguard.cfg 配置如下: -optimizationpasses 5-dontu ...

  8. android studio...混淆打包全揭秘

    前言,当前android studio使用的版本较新,低版本的如果有差异,或者问题,欢迎拍砖! 1.修改配置文件 找到配置文件,build.gradle,修改如下.    signingConfigs ...

  9. apk混淆打包注意事项

    混淆打包搞了好几天才初步了解,其中碰到很多Debug正常,Release的apk不能用,基本都是第三方的jar的问题,所以要排除混淆. 1. Json解析对象出错 用到fastJson或者GJson的 ...

随机推荐

  1. iOS缓存功能

    之前做缓存,没有考虑过这个具体的实现. 移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage. 但是,离线缓存会占用手机存储空间,所 ...

  2. maven常用插件pom配置

    一.问题描述: 部署一个maven打包项目时,jar包,依赖lib包全部手动上传至服务器,然后用maven部署报错:Exception in thread "main" java. ...

  3. Python之路----------内置函数

    1.abs(x)绝对值 #coding=utf-8 a = 1 b = -2 print(abs(a)) print(abs(b)) 2.all(iterable)可迭代对象里面所有内容为真返回真,空 ...

  4. 使用Cordova编译Android平台程序提示:Could not reserve enough space for 2097152KB object heap

    大体的意思是系统内存不够用,创建VM失败.试了网上好几种方法都不行,最后这个方法可以了: 开始->控制面板->系统->高级设置->环境变量->系统变量 新建变量: 变量名 ...

  5. RDIFramework.NET平台代码生成器V3.1版本全新发布-更新于2016-10-29(提供下载)

    本次主要更新内容: 1.增加对Oracle表创建语句的查看. 2.新增对MySql的代码生成支持. 3.全面重构对多线程的支持,改变以前会无故退出的现象. RDIFramework.NET代码生成器V ...

  6. [Android Tips] 23. How to fail/stop Gradle task immediately if some conditions are not met

    throw new GradleException("conditions are not met") 参考 How to fail/stop task immediately i ...

  7. GPS部标平台的架构设计(四)-百度地图设计

    部标GPS软件平台之百度地图设计 地图是客户端中不可缺少的一个模块,很多人在设计和画图时候,喜欢加上地图引擎这样高大上的字眼,显得自己的平台有内涵,说白了就是用第三方的SDK来开发,早期的GPS监 控 ...

  8. 发布一款Github博客皮肤

    Major是一款基于jekyll的皮肤,没有用hexo,原因是换机器后无法更新博客,但是可以用U盘考环境.总之很麻烦!折腾纠结好久,还是用jekyll!不用发布直接push文章即可,方便快捷.用的放心 ...

  9. LDAP 中 CN, OU, DC 的含义

    1. LDAP的存储规则 区分名(DN,Distinguished Name) 和自然界中的树不同,文件系统/LDAP/电话号码簿目录的每一片枝叶都至少有一个独一无二的属性,这一属性可以帮助我们来区别 ...

  10. 【C#进阶】多播委托和委托数组像是一回事~

    这个MathOperation类有三静态方法,参数都是double,并且没有返回值,考虑用Action<>() 这种预定义的委托哦 class MathOperations { publi ...