使用json格式最为数据传输的格式,代码混淆后数据没有上传成功

为了保护我们个人和公司的利益,我们在发布apk包时都要求对软件进行混淆,并且在混淆工程中,系统自动将没有使用到的类屏蔽掉以及对代码进行相应的优化,因此在发布apk时,混淆是极力推荐的。在本次软件混淆后发现客户端和服务器端的数据不能互通,即apk不能接收到服务器的数据,服务器也不能准确接受到终端上传的数据。经过各种折腾,最终发现时混淆时出的问题。

apk与服务器端的通信格式为json格式,采用的是gson-2.2.1.jar 的jar包作为辅助工具。混淆的过程中使用-keep 方式来说明gson jar包不被混淆,混淆完成后软件能够运行,但是当想服务器上传数据时发现服务器端不能接受到上传的数据。经过各种测试没有结果,最终apk照常混淆,在服务器端打断点,分析接受的到的数据,发现格式不对,上传的数据数据的字段与实际类的字段不一样。这时才恍然大悟,原来混淆时将基本数据类进行了混淆,导致原来的在字段都变成了对应的'a,b,c,s.....等,数据的格式从而与a,b c,s....对应。而服务器端的类的没有经过混淆,为原来的字段,使用

GsonBuilder builder = new GsonBuilder();
		Gson gson = builder.create();
		User user = gson.fromJson(json, User.class);

这种方式导致不能获得对应的数据。

解决方案为使用-keep 来说明在通信中使用到的类不被混淆,保证客户端的格式与服务器的格式对应,最终才能将数据转换过来。-keep 的使用实例,在proguor-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 *;
#}
-keep class com.google.gson.** {*;}
-keep class android.support.v4.** {*;} 
-keep class com.sunbird.bean.** {*;}
在使用的过程中根据自己的需要进行相应的操作。













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值