apk混淆打包注意事项
混淆打包搞了好几天才初步了解,其中碰到很多Debug正常,Release的apk不能用,基本都是第三方的jar的问题,所以要排除混淆。
1. Json解析对象出错
用到fastJson或者GJson的apk混淆编码时要加上这句:
- -keepattributes Signature
2.百度地图不能用,注意添加下面的语句
- -libraryjars libs/baidumapapi_v2_1_0.jar #替换成自己所用版本的jar包
- -keep class com.baidu.** { *; }
- -keep class vi.com.gdi.bgl.android.**{*;}
3.融云IM
- -keep class io.rong.imkit.** {*;}
- -keep class io.rong.imlib.** {*;}
- -keepattributes Exceptions,InnerClasses
- -dontwarn io.rong.**
- -keep class io.rong.** {*;}
- -keep public class com.yourpackage.R** {*;} #自己的包名
- -keep class *.R$ { *; }
4.保持自定义的View不混淆
- -keepclasseswithmembers class * { # 保持自定义控件类不被混淆
- public <init>(android.content.Context, android.util.AttributeSet);
- }
- -keepclasseswithmembers class * { # 保持自定义控件类不被混淆
- public <init>(android.content.Context, android.util.AttributeSet, int);
- }
可以把自定义的View放入一个包。然后排除掉这个包。
5.其他第三方的jar包
- -dontwarn org.apache.http.**
- -keep class org.apache.http.** {*;}
- -dontwarn com.ant.liao.**
- -keep class com.ant.liao.** {*;}
- -dontwarn org.json.**
- -keep class org.json.** {*;}
- -dontwarn pl.droidsonroids.gif.**
- -keep class pl.droidsonroids.gif.** {*;}
- -dontwarn com.cmcc.analytics.**
- -keep class com.cmcc.analytics.** {*;}
- -keep class android.** {*;}
6.Log文件
- -assumenosideeffects
- class android.util.Log
- {
- public static ***
- e(...);
- public static ***
- w(...);
- public static ***
- wtf(...);
- public static ***
- d(...);
- public static ***
- v(...);
7.还有就是自定义的实体类,bean或者Entity。因为它们基本都是序列化的。
我的体会:序列化和用到反射机制的都不能混淆。
这篇文章有一个比较好的版本Android 混淆打包标准proguard 配置
- # 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 *;
- # 指定代码的压缩级别
- -optimizationpasses 5
- # 是否使用大小写混合
- -dontusemixedcaseclassnames
- # 是否混淆第三方jar
- -dontskipnonpubliclibraryclasses
- # 混淆时是否做预校验
- -dontpreverify
- # 混淆时是否记录日志
- -verbose
- # 混淆时所采用的算法
- -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
- #-ignorewarnings
- -libraryjars libs/android-support-v4.jar
- -libraryjars libs/baidumapapi_v3_0_0.jar
- -libraryjars libs/commons-codec.jar
- -libraryjars libs/commons-httpclient-3.1.jar
- -libraryjars libs/gson-2.2.2.jar
- -libraryjars libs/httpmime-4.2.jar
- -libraryjars libs/locSDK_3.1.jar
- -libraryjars libs/ShareSDK-Core-2.3.9.jar
- -libraryjars libs/ShareSDK-QQ-2.3.9.jar
- -libraryjars libs/ShareSDK-SinaWeibo-2.3.9.jar
- # 保持哪些类不被混淆
- -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
- #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
- -dontwarn com.u14studio.entity.**
- -keep class com.u14studio.entity.**{*;}
- ##---------------End: proguard configuration for Gson ----------
- #shareSDK、
- -keep class cn.sharesdk.**{*;}
- -keep class com.sina.**{*;}
- -keep class **.R$* {*;}
- -keep class **.R{*;}
- -dontwarn cn.sharesdk.**
- -dontwarn **.R$*
- -keep class m.framework.**{*;}
- #shareSDK结束
- #百度地图不混淆
- -dontwarn com.baidu.**
- -keep class com.baidu.**{*;}
- -keep class vi.com.gdi.bgl.android.**{*;}
- -keep class android.content.Context.getExternalFilesDirs
- -keep public class * extends android.content.Context.getExternalFilesDirs
- #百度地图不混淆结束
- -dontwarn org.apache.**
- -keep class org.apache.**{*;}
- -dontwarn android.support-v4.**
- -keep class android.support-v4.**{*;}
- -dontwarn com.alipay.android.app.**
- -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
查看了代码,转化是没有问题的啊
- public void run()
- {
- super.run();
- Message resultMsg = mHandler.obtainMessage();
- HashMap<String, String> param = new HashMap<String, String>();
- param.put("userId", mUserId);
- // 获取返回
- String jsonStr = null;
- // 返回参数
- try
- {
- jsonStr = NetworkManager.getInstance().httpConnectOpt(
- Constant.URL_USER_GET_INTO, param);
- // 将字符串转换成jsonObject对象
- JSONObject jsonObject = new JSONObject();
- jsonObject = JSONObject.parseObject(jsonStr);
- String result = jsonObject.getString("result");
- LogUtil.d(jsonObject);
- LogUtil.d(result);
- Bundle data = null;
- if (result != null && result.length() != 0)
- {
- // 获取返回参数
- UserBean bean = JSON.parseObject(result, UserBean.class);
- LogUtil.d(bean);
- LogUtil.d(bean.getTags());
- data = new Bundle();
- data.putParcelable("result", bean);
- }
- // 获取返值
- String code = (String) jsonObject.get("code");
- if ("0".equals(code))
- {
- resultMsg.what = 1;
- resultMsg.setData(data);
- } else if ("1".equals(code))
- {
- resultMsg.what = -1;
- }
- } catch (Exception e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
但是不混淆编码或者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 proguard 详解
折腾了俩天还是没效果,最后参照上面CSDN解决Gson的方法,我的是fastJson,然后可能是Parcel出了问题,加上下面的排除混淆。
- -keepclassmembers class * implements android.os.Parcel { public *;}
- -dontwarn com.cmcc.wepa.bean.**
- -keep class com.cmcc.wepa.bean.** {*;}
- -keepattributes Signature
- 打包,竟然好用了。还是因为不理解造成很多弯路,哈哈,解决了就好,虽然还是懵懵懂懂的,但有了一些印象。
也就是说实现Parcel或者Seriallizable的对象一定不要混淆,连同它们的接口对象一起排除混淆。大概就是这样。估计一般实体类不混淆。- 还有个问题就是必须加上-keepattributes Signature,不加这句我的apk也不好用,有的blog说:
当有注解的时候
- -keepattributes Signature
- -keepattributes *Annotation*
- 加上上面两句减少错误。最后还有个总结的还可以的Android 混淆代码总结
下面给出我的proguard-project.txt- 后记:经过N次尝试,发现这句也没有用,去掉后不会报错,不过加上更好吧~呵呵
-keepclassmembers class * implements android.os.Parcel { public *;}
-libraryjars libs/fastjson-1.2.3.jar
其实,最后就是神奇的-keepattributes Signature的作用。不明白的还有很多啊。
- # 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 * implements android.os.Parcel { public *;}
- # the third-party library
- -libraryjars libs/fastjson-1.2.3.jar
- -dontwarn net.tsz.afinal.**
- -keep class net.tsz.afinal.** {*;}
- -dontwarn com.alibaba.fastjson.**
- -keep class com.alibaba.fastjson.** { *; }
- -dontwarn com.baidu.**
- -keep class com.baidu.** {*;}
- -dontwarn io.rong.**
- -keep class io.rong.** {*;}
- -dontwarn org.apache.http.**
- -keep class org.apache.http.** {*;}
- -dontwarn com.ant.liao.**
- -keep class com.ant.liao.** {*;}
- -dontwarn org.json.**
- -keep class org.json.** {*;}
- -dontwarn pl.droidsonroids.gif.**
- -keep class pl.droidsonroids.gif.** {*;}
- -dontwarn com.cmcc.analytics.**
- -keep class com.cmcc.analytics.** {*;}
- -keep class android.** {*;}
- -dontwarn com.my.bean.**
- -keep class com.my.bean.** {*;}
- -keepattributes Signature
- -assumenosideeffects
- class android.util.Log
- {
- public static ***
- e(...);
- public static ***
- w(...);
- public static ***
- wtf(...);
- public static ***
- d(...);
- public static ***
- v(...);
- }
apk混淆打包注意事项的更多相关文章
- apk混淆打包和反编译(转)
前面有人写过了,我就直接引用了,大家就不乱找了.以后有问题再继续更新. 一.混淆打包.编译 1.Android 代码混淆.http://blog.csdn.net/zjclugger/article/ ...
- Android Studio混淆打包
1.apk混淆打包 如果要对apk进行混淆,你要先告知gradle这个app需要混淆,并告知其混淆规则. 告知gradle需要混淆的代码 在Project/app/build.gradle中把mini ...
- Android项目混淆打包
以下为我此期项目中的关于混淆打包的总结:(本人第一次混淆打包,呵呵,错误很多!列了一些比较头疼的)一.项目混淆过程中注意事项:由于我的sdk版本较高,因此新建android项目下只有proguard- ...
- Android项目实战(二十五):Android studio 混淆+打包+验证是否成功
前言: 单挑Android项目,最近即时通讯用到环信,集成sdk的时候 官方有一句 在 ProGuard 文件中加入以下 keep. -keep class com.hyphenate.** {*;} ...
- AndroidStudio 混淆打包
AndroidStudio中的项目可以用compile的形式引入github上的开 源项目,可以引用module,而不一定都要用libs文件夹中添加jar包的形式.在最终realease打包时,混淆的 ...
- Android Studio和eclipse混淆打包总结
最近项目有点闲,考虑到以前的项目没有做过混淆,只是用了加固软件进行加固,为了安全性,准备给项目加上,这里做个总结,都经本人亲自在项目实践,说是为了安全性,这好像说大了,一来项目中没用到什么特别的技术, ...
- android -------- 混淆打包报错(warning - InnerClass annotations are missing corresponding EnclosingMember annotations)
最近做Android混淆打包遇到一些问题,Android Sdutio 3.1 版本打包的 错误如下: Android studio warning - InnerClass annotations ...
- Android 混淆打包
有些时候我们希望我们自己的apk包不能被别人反编译而获取自己的源代码.这就需要我们通过Android提供的混淆打包技术来完成. 一.没有引用外部包的情况: 这种情况下代码混淆的方式相对简单: 1)只需 ...
- android应用程序的混淆打包
android应用程序的混淆打包 1 . 在工程文件project.properties中加入下proguard.config=proguard.cfg , 如下所示: target=android- ...
随机推荐
- 本地/远程Service 和Activity 的交方式(转)
android SDK提供了Service,用于类似*nix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service):用于应用程序内部 远程服务(Remo ...
- hdu 2769 uva 12169 Disgruntled Judge 拓展欧几里德
//数据是有多水 连 10^10的枚举都能过 关于拓展欧几里德:大概就是x1=y2,y1=x2-[a/b]y2,按这个规律递归到gcd(a,0)的形式,此时公因数为a,方程也变为a*x+0*y=gcd ...
- 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 ...
- ORA-02287: 此处不同意序号
ORA-02287: 此处不同意序号 insert into gls_vchitem (viid, yr, km) select gls_vchitem_seq.n ...
- 取得正在运行的Activity
在main.xml中: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- linux shadow破解
主要学习了下linux /etc/shadow文件中密码的破解:学习了hashcat工具的简单实用,具体的可以参加:https://samsclass.info/123/proj10/p12-hash ...
- 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s
这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html ---by 墨染之樱 ...
- ZOJ3784 String of Infinity 高大上的AC自动机 数据原来这么水啊!不算输入输出只有5-7行
找给定s集合里面word全部是同一个字符的,这样的word有几个,如果数量<m就yes,否则就no.#include<iostream> #include<cstring> ...
- 我也能上google
之所以起个这么隐晦的标题就是因为怕被黑.但是不能上谷歌,对于我们搞技术的人来说真的很残忍. 目前只找到几个镜像网址: https://xie.lu/ https://www.gotosearch.in ...
- leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...