混淆打包搞了好几天才初步了解,其中碰到很多Debug正常,Release的apk不能用,基本都是第三方的jar的问题,所以要排除混淆。

1. Json解析对象出错

用到fastJson或者GJson的apk混淆编码时要加上这句:

  1. -keepattributes Signature

2.百度地图不能用,注意添加下面的语句

  1. -libraryjars libs/baidumapapi_v2_1_0.jar #替换成自己所用版本的jar包
  2. -keep class com.baidu.** { *; }
  3. -keep class vi.com.gdi.bgl.android.**{*;}

3.融云IM

  1. -keep class io.rong.imkit.** {*;}
  2. -keep class io.rong.imlib.** {*;}
  3. -keepattributes Exceptions,InnerClasses
  4. -dontwarn io.rong.**
  5. -keep class io.rong.** {*;}
  6. -keep public class com.yourpackage.R** {*;} #自己的包名
  7. -keep class *.R$ { *; }

4.保持自定义的View不混淆

  1. -keepclasseswithmembers class * { # 保持自定义控件类不被混淆
  2. public <init>(android.content.Context, android.util.AttributeSet);
  3. }
  4.  
  5. -keepclasseswithmembers class * { # 保持自定义控件类不被混淆
  6. public <init>(android.content.Context, android.util.AttributeSet, int);
  7. }

可以把自定义的View放入一个包。然后排除掉这个包。

5.其他第三方的jar包

  1. -dontwarn org.apache.http.**
  2. -keep class org.apache.http.** {*;}
  3. -dontwarn com.ant.liao.**
  4. -keep class com.ant.liao.** {*;}
  5. -dontwarn org.json.**
  6. -keep class org.json.** {*;}
  7. -dontwarn pl.droidsonroids.gif.**
  8. -keep class pl.droidsonroids.gif.** {*;}
  9. -dontwarn com.cmcc.analytics.**
  10. -keep class com.cmcc.analytics.** {*;}
  11. -keep class android.** {*;}

6.Log文件

  1. -assumenosideeffects
  2. class android.util.Log
  3. {
  4. public static ***
  5. e(...);
  6. public static ***
  7. w(...);
  8. public static ***
  9. wtf(...);
  10. public static ***
  11. d(...);
  12. public static ***
  13. v(...);

7.还有就是自定义的实体类,bean或者Entity。因为它们基本都是序列化的。

我的体会:序列化和用到反射机制的都不能混淆。

这篇文章有一个比较好的版本Android 混淆打包标准proguard 配置

  1. # To enable ProGuard in your project, edit project.properties
  2. # to define the proguard.config property as described in that file.
  3. #
  4. # Add project specific ProGuard rules here.
  5. # By default, the flags in this file are appended to flags specified
  6. # in ${sdk.dir}/tools/proguard/proguard-android.txt
  7. # You can edit the include path and order by changing the ProGuard
  8. # include property in project.properties.
  9. #
  10. # For more details, see
  11. # http://developer.android.com/guide/developing/tools/proguard.html
  12.  
  13. # Add any project specific keep options here:
  14.  
  15. # If your project uses WebView with JS, uncomment the following
  16. # and specify the fully qualified class name to the JavaScript interface
  17. # class:
  18. #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
  19. # public *;
  20. # 指定代码的压缩级别
  21. -optimizationpasses 5
  22. # 是否使用大小写混合
  23. -dontusemixedcaseclassnames
  24. # 是否混淆第三方jar
  25. -dontskipnonpubliclibraryclasses
  26. # 混淆时是否做预校验
  27. -dontpreverify
  28. # 混淆时是否记录日志
  29. -verbose
  30. # 混淆时所采用的算法
  31. -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
  32. #-ignorewarnings
  33.  
  34. -libraryjars libs/android-support-v4.jar
  35. -libraryjars libs/baidumapapi_v3_0_0.jar
  36. -libraryjars libs/commons-codec.jar
  37. -libraryjars libs/commons-httpclient-3.1.jar
  38. -libraryjars libs/gson-2.2.2.jar
  39. -libraryjars libs/httpmime-4.2.jar
  40. -libraryjars libs/locSDK_3.1.jar
  41. -libraryjars libs/ShareSDK-Core-2.3.9.jar
  42. -libraryjars libs/ShareSDK-QQ-2.3.9.jar
  43. -libraryjars libs/ShareSDK-SinaWeibo-2.3.9.jar
  44.  
  45. # 保持哪些类不被混淆
  46. -keep public class * extends android.app.Activity
  47. -keep public class * extends android.app.Application
  48. -keep public class * extends android.app.Service
  49. -keep public class * extends android.content.BroadcastReceiver
  50. -keep public class * extends android.content.ContentProvider
  51. -keep public class * extends android.app.backup.BackupAgentHelper
  52. -keep public class * extends android.preference.Preference
  53. -keep public class com.android.vending.licensing.ILicensingService
  54.  
  55. #gson解析不被混淆
  56. -keep class com.google.**{*;}
  57. -keepclassmembers class * implements java.io.Serializable {
  58. static final long serialVersionUID;
  59. private static final java.io.ObjectStreamField[] serialPersistentFields;
  60. private void writeObject(java.io.ObjectOutputStream);
  61. private void readObject(java.io.ObjectInputStream);
  62. java.lang.Object writeReplace();
  63. java.lang.Object readResolve();
  64. }
  65. ##---------------Begin: proguard configuration for Gson ----------
  66. # Gson uses generic type information stored in a class file when working with fields. Proguard
  67. # removes such information by default, so configure it to keep all of it.
  68. -keepattributes Signature
  69. # Gson specific classes
  70. -keep class sun.misc.Unsafe {*;}
  71. #-keep class com.google.gson.stream.** {*;}
  72. # Application classes that will be serialized/deserialized over Gson
  73. -dontwarn com.u14studio.entity.**
  74. -keep class com.u14studio.entity.**{*;}
  75. ##---------------End: proguard configuration for Gson ----------
  76. #shareSDK、
  77. -keep class cn.sharesdk.**{*;}
  78. -keep class com.sina.**{*;}
  79. -keep class **.R$* {*;}
  80. -keep class **.R{*;}
  81. -dontwarn cn.sharesdk.**
  82. -dontwarn **.R$*
  83. -keep class m.framework.**{*;}
  84. #shareSDK结束
  85.  
  86. #百度地图不混淆
  87. -dontwarn com.baidu.**
  88. -keep class com.baidu.**{*;}
  89. -keep class vi.com.gdi.bgl.android.**{*;}
  90. -keep class android.content.Context.getExternalFilesDirs
  91. -keep public class * extends android.content.Context.getExternalFilesDirs
  92. #百度地图不混淆结束
  93.  
  94. -dontwarn org.apache.**
  95. -keep class org.apache.**{*;}
  96. -dontwarn android.support-v4.**
  97. -keep class android.support-v4.**{*;}
  98. -dontwarn com.alipay.android.app.**
  99. -keep class com.alipay.android.app.**{*;}

下面就是些闲话了,是自己解决问题时写的记录,也一并留着吧。参照的文章都写上了,谢谢大家的分享。也谢谢QQ群里某位热心人的帮助。

今天遇到一个很奇怪的问题,混淆打包导致的,至今没明白为什么。主要现象是一个实现parcelable的UserBean,数据成员包含一个UserTag的list对象,后者也实现了parcel接口,然后就是利用json与服务器之间传递值。当不设置标签时,一切OK,只要设置标签,就会报错,错误只能定位到这个转化问题。

java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to com.cmcc.wepa.bean.UserTagBean

查看了代码,转化是没有问题的啊

  1. public void run()
  2. {
  3. super.run();
  4. Message resultMsg = mHandler.obtainMessage();
  5. HashMap<String, String> param = new HashMap<String, String>();
  6. param.put("userId", mUserId);
  7. // 获取返回
  8. String jsonStr = null;
  9. // 返回参数
  10. try
  11. {
  12. jsonStr = NetworkManager.getInstance().httpConnectOpt(
  13. Constant.URL_USER_GET_INTO, param);
  14. // 将字符串转换成jsonObject对象
  15. JSONObject jsonObject = new JSONObject();
  16. jsonObject = JSONObject.parseObject(jsonStr);
  17. String result = jsonObject.getString("result");
  18. LogUtil.d(jsonObject);
  19. LogUtil.d(result);
  20. Bundle data = null;
  21. if (result != null && result.length() != 0)
  22. {
  23. // 获取返回参数
  24. UserBean bean = JSON.parseObject(result, UserBean.class);
  25. LogUtil.d(bean);
  26. LogUtil.d(bean.getTags());
  27. data = new Bundle();
  28. data.putParcelable("result", bean);
  29. }
  30.  
  31. // 获取返值
  32. String code = (String) jsonObject.get("code");
  33. if ("0".equals(code))
  34. {
  35.  
  36. resultMsg.what = 1;
  37. resultMsg.setData(data);
  38. } else if ("1".equals(code))
  39. {
  40.  
  41. resultMsg.what = -1;
  42.  
  43. }
  44. } catch (Exception e)
  45. {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }

但是不混淆编码或者Debug的包都是好用的,标签一直OK。

难道是parcelable对象之中嵌套parcelable对象成员会有问题吗?还是忘记了什么keep。

mark一下,悬而未决。

其中在$sdk_dir$\tools\proguard\proguard-android.txt,里面吧基本的keep都定义好了,工程自己的在proguard-project.txt中定义。

这里原来理解错了吗?看到这篇文说是要把proguard-android.txt中的copy到proguard-project.txt中,个人感觉不用,应该会自动读取,个人验证确实没有影响。

Android代码混淆-添加了Gson遇到的问题

这篇文章则详细介绍了常用的各个字段的意义。Android proguard 详解

折腾了俩天还是没效果,最后参照上面CSDN解决Gson的方法,我的是fastJson,然后可能是Parcel出了问题,加上下面的排除混淆。

  1. -keepclassmembers class * implements android.os.Parcel { public *;}
  2. -dontwarn com.cmcc.wepa.bean.**
  3. -keep class com.cmcc.wepa.bean.** {*;}
  4. -keepattributes Signature
  1. 打包,竟然好用了。还是因为不理解造成很多弯路,哈哈,解决了就好,虽然还是懵懵懂懂的,但有了一些印象。
    也就是说实现Parcel或者Seriallizable的对象一定不要混淆,连同它们的接口对象一起排除混淆。大概就是这样。估计一般实体类不混淆。
  2.  
  3. 还有个问题就是必须加上-keepattributes Signature,不加这句我的apk也不好用,有的blog说:
    当有注解的时候
  1. -keepattributes Signature
  2. -keepattributes *Annotation*
  1. 加上上面两句减少错误。最后还有个总结的还可以的Android 混淆代码总结
    下面给出我的proguard-project.txt
  2.  
  3. 后记:经过N次尝试,发现这句也没有用,去掉后不会报错,不过加上更好吧~呵呵
    -keepclassmembers class * implements android.os.Parcel { public *;}
    -libraryjars libs/fastjson-1.2.3.jar
    其实,最后就是神奇的-keepattributes Signature的作用。不明白的还有很多啊。
  4.  
  1. # To enable ProGuard in your project, edit project.properties
  2. # to define the proguard.config property as described in that file.
  3. #
  4. # Add project specific ProGuard rules here.
  5. # By default, the flags in this file are appended to flags specified
  6. # in ${sdk.dir}/tools/proguard/proguard-android.txt
  7. # You can edit the include path and order by changing the ProGuard
  8. # include property in project.properties.
  9. #
  10. # For more details, see
  11. # http://developer.android.com/guide/developing/tools/proguard.html
  12.  
  13. # Add any project specific keep options here:
  14. # If your project uses WebView with JS, uncomment the following
  15. # and specify the fully qualified class name to the JavaScript interface
  16. # class:
  17.  
  18. -keepclassmembers class * implements android.os.Parcel { public *;}
  19.  
  20. # the third-party library
  21. -libraryjars libs/fastjson-1.2.3.jar
  22. -dontwarn net.tsz.afinal.**
  23. -keep class net.tsz.afinal.** {*;}
  24. -dontwarn com.alibaba.fastjson.**
  25. -keep class com.alibaba.fastjson.** { *; }
  26.  
  27. -dontwarn com.baidu.**
  28. -keep class com.baidu.** {*;}
  29. -dontwarn io.rong.**
  30. -keep class io.rong.** {*;}
  31. -dontwarn org.apache.http.**
  32. -keep class org.apache.http.** {*;}
  33. -dontwarn com.ant.liao.**
  34. -keep class com.ant.liao.** {*;}
  35. -dontwarn org.json.**
  36. -keep class org.json.** {*;}
  37. -dontwarn pl.droidsonroids.gif.**
  38. -keep class pl.droidsonroids.gif.** {*;}
  39. -dontwarn com.cmcc.analytics.**
  40. -keep class com.cmcc.analytics.** {*;}
  41. -keep class android.** {*;}
  42.  
  43. -dontwarn com.my.bean.**
  44. -keep class com.my.bean.** {*;}
  45. -keepattributes Signature
  46.  
  47. -assumenosideeffects
  48. class android.util.Log
  49. {
  50. public static ***
  51. e(...);
  52. public static ***
  53. w(...);
  54. public static ***
  55. wtf(...);
  56. public static ***
  57. d(...);
  58. public static ***
  59. v(...);
  60. }
  1.  
  1.  

apk混淆打包注意事项的更多相关文章

  1. apk混淆打包和反编译(转)

    前面有人写过了,我就直接引用了,大家就不乱找了.以后有问题再继续更新. 一.混淆打包.编译 1.Android 代码混淆.http://blog.csdn.net/zjclugger/article/ ...

  2. Android Studio混淆打包

    1.apk混淆打包 如果要对apk进行混淆,你要先告知gradle这个app需要混淆,并告知其混淆规则. 告知gradle需要混淆的代码 在Project/app/build.gradle中把mini ...

  3. Android项目混淆打包

    以下为我此期项目中的关于混淆打包的总结:(本人第一次混淆打包,呵呵,错误很多!列了一些比较头疼的)一.项目混淆过程中注意事项:由于我的sdk版本较高,因此新建android项目下只有proguard- ...

  4. Android项目实战(二十五):Android studio 混淆+打包+验证是否成功

    前言: 单挑Android项目,最近即时通讯用到环信,集成sdk的时候 官方有一句 在 ProGuard 文件中加入以下 keep. -keep class com.hyphenate.** {*;} ...

  5. AndroidStudio 混淆打包

    AndroidStudio中的项目可以用compile的形式引入github上的开 源项目,可以引用module,而不一定都要用libs文件夹中添加jar包的形式.在最终realease打包时,混淆的 ...

  6. Android Studio和eclipse混淆打包总结

    最近项目有点闲,考虑到以前的项目没有做过混淆,只是用了加固软件进行加固,为了安全性,准备给项目加上,这里做个总结,都经本人亲自在项目实践,说是为了安全性,这好像说大了,一来项目中没用到什么特别的技术, ...

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

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

  8. Android 混淆打包

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

  9. android应用程序的混淆打包

    android应用程序的混淆打包 1 . 在工程文件project.properties中加入下proguard.config=proguard.cfg , 如下所示: target=android- ...

随机推荐

  1. 本地/远程Service 和Activity 的交方式(转)

    android SDK提供了Service,用于类似*nix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service):用于应用程序内部 远程服务(Remo ...

  2. hdu 2769 uva 12169 Disgruntled Judge 拓展欧几里德

    //数据是有多水 连 10^10的枚举都能过 关于拓展欧几里德:大概就是x1=y2,y1=x2-[a/b]y2,按这个规律递归到gcd(a,0)的形式,此时公因数为a,方程也变为a*x+0*y=gcd ...

  3. Productivity Improvements for the Entity Framework(实体框架设计)【转】

    Background We’ve been hearing a lot of good feedback on the recently released update to the Entity F ...

  4. ORA-02287: 此处不同意序号

    ORA-02287: 此处不同意序号 insert into gls_vchitem     (viid,      yr,      km)     select gls_vchitem_seq.n ...

  5. 取得正在运行的Activity

    在main.xml中: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  6. linux shadow破解

    主要学习了下linux /etc/shadow文件中密码的破解:学习了hashcat工具的简单实用,具体的可以参加:https://samsclass.info/123/proj10/p12-hash ...

  7. 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s

    这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html   ---by 墨染之樱 ...

  8. ZOJ3784 String of Infinity 高大上的AC自动机 数据原来这么水啊!不算输入输出只有5-7行

    找给定s集合里面word全部是同一个字符的,这样的word有几个,如果数量<m就yes,否则就no.#include<iostream> #include<cstring> ...

  9. 我也能上google

    之所以起个这么隐晦的标题就是因为怕被黑.但是不能上谷歌,对于我们搞技术的人来说真的很残忍. 目前只找到几个镜像网址: https://xie.lu/ https://www.gotosearch.in ...

  10. leetcode Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...