关闭

proguard混淆

131人阅读 评论(1) 收藏 举报
分类:

作为进阶必备的技能,proguard混淆语法不得不在这里做点笔记

Proguard 英语详解:http://proguard.sourceforge.net/index.html#manual/usage.html

Proguard官网地址:http://developer.android.com/tools/help/proguard.html

Proguard 中文详解:http://www.jianshu.com/p/0202845db617

Proguard 大牛详解:
http://blog.csdn.net/banketree/article/details/41928175

有盆友收集的关于Proguard的一些问题:http://blog.csdn.net/new8899_/article/details/9094057

proguard 基本语法(下面的申明并不是所有的都是必须的,根据自己的项目及实际需求来添加):

#忽略警告,避免打包时某些警告出现
-ignorewarnings
#指定代码的压缩级别 设置混淆的压缩比率 0 ~ 7 
-optimizationpasses 5
#是否使用大小写混合
-dontusemixedcaseclassnames
#如果应用程序引入的有jar包,并且想混淆jar包里面的class 
-dontskipnonpubliclibraryclasses
#混淆时是否做预校验 这里表示不用预先检查
-dontpreverify
#混淆时是否记录日志 添加了表示不用输出详细的过程
-verbose

#表示混淆采用的算法
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
#表示对所有Activity的子类不会进行混淆
-keep public class * extends android.app.Activity
#表示对所有Application 的子类不会进行混淆
-keep public class * extends android.app.Application  
#表示对所有Service 的子类不会进行混淆
-keep public class * extends android.app.Service  
#表示对所有BroadcastReceiver的子类不会进行混淆
-keep public class * extends  android.content.BroadcastReceiver
#表示对所有ContentProvider 的子类不会进行混淆   
-keep public class * extends android.content.ContentProvider
-keep public class com.android.vending.licensing.ILicensingService
#对support.v4包的申明
-keep class android.support.v4.** { *; }  
#表示对所有继承support.v4包里面类的子类不会进行混淆
-keep public class * extends android.support.v4.** 
 #缺省proguard 会检查每一个引用是否正确,但是第三方库里面往往有些不会用到的类,没有正确引用。如果不配置的话,系统就会报错。
-dontwarn android.support.v4.**     
#表示对所有android.app.Fragment 的子类不会进行混淆 
-keep public class * extends android.app.Fragment 
#表示对所有android.support.v4.widget 的子类不会进行混淆 
-keep public class * extends android.support.v4.widget  
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference

如果自己写的类继承了第三方jar包里的类,那么也需要设置,避免出现错误警告
除了对于继承的类进行处理,还有一些方法也需要声明,因为所有在xml 文件注册的都不可以进行混淆,一旦混淆后我们的程序将找不到引用会报错!

#所有native的方法不能去混淆
-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);  
}  
# 保持枚举 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 *;  
}  

-keepclassmembers class * extends android.app.Activity {*;} 表示内部所有都不混淆

-keep public class * extends android.app.Activity,只是类名不会混淆

如果自己定义的一些类希望被混淆,可以写:

-keep class MyClass; # 保持自己定义的类不被混淆

很多时候我们的项目都会引用第三方jar包

#libraryjars {classpath} 指定要处理的应用程序jar,war,ear和目录所需要的程序库文件 ,这里表示处理libs文件夹下xUtils-2.6.8.jar,如果有多个jar,用多行列出
-libraryjars libs/xUtils-2.6.8.jar

到此,Proguard配置基本已经完成,但是有时候在打包的时候依然会失败并打印一些错误,这里列出我遇到的:

[2016-05-31 13:11:26 - AioFiveSJ] feb8dae4 disconnected! Cancelling 'com.example.mtreader.SplashActivity activity launch'!
[2016-05-31 17:02:11 - AioFiveSJ] Proguard returned with error code 1. See console
[2016-05-31 17:02:11 - AioFiveSJ] Note: there were 1000 duplicate class definitions.
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.Graphics
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.Graphics2D
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.GraphicsConfiguration
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.Graphics
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.Graphics2D
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.GraphicsConfiguration
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.GraphicsDevice
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.GraphicsEnvironment
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.image.BufferedImage
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.image.BufferedImage
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.image.BufferedImage
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.image.BufferedImage
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.image.BufferedImage
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.image.MemoryImageSource
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.image.PixelGrabber
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.image.PixelGrabber
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class javax.imageio.ImageIO
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class javax.swing.ImageIcon
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class javax.swing.ImageIcon
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.Image
[2016-05-31 17:02:11 - AioFiveSJ] Warning: cn.jelly.a.a.a: can't find referenced class java.awt.image.BufferedImage
[2016-05-31 17:02:11 - AioFiveSJ]       You should check if you need to specify additional program jars.
[2016-05-31 17:02:11 - AioFiveSJ] Warning: there were 37 unresolved references to classes or interfaces.
[2016-05-31 17:02:11 - AioFiveSJ]          You may need to specify additional library jars (using '-libraryjars').
[2016-05-31 17:02:11 - AioFiveSJ] java.io.IOException: Please correct the above warnings first.
[2016-05-31 17:02:11 - AioFiveSJ]   at proguard.Initializer.execute(Initializer.java:321)
[2016-05-31 17:02:11 - AioFiveSJ]   at proguard.ProGuard.initialize(ProGuard.java:211)
[2016-05-31 17:02:11 - AioFiveSJ]   at proguard.ProGuard.execute(ProGuard.java:86)
[2016-05-31 17:02:11 - AioFiveSJ]   at proguard.ProGuard.main(ProGuard.java:492)

从上面的这段警告中我们可以看到Warning: cn.jelly.a.a.a重复出现了多次,这里的意思是我们的程序并没有找到 cn.jelly.a.a.a 这个包里面的一些引用类,如果这些警告对你的程序并不会造成什么影响,那我们可以添加

#表示不用输出cn.jelly.a.a.a 相关的警告 
-dontwarn cn.jelly.a.a.a

与-dontwarn(不用输出警告)参数相似的还有 -dontnote(不用输出通知)


对于apk我们既要学会混淆来保护,也要学会怎么去反编译它(不要问我为什么~~),这里有个盆友写的挺详细的:http://blog.csdn.net/vipzjyno1/article/details/21039349/

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:33968次
    • 积分:1072
    • 等级:
    • 排名:千里之外
    • 原创:74篇
    • 转载:4篇
    • 译文:0篇
    • 评论:4条
    文章分类