JSON format is the most data transmission format. After the code is mixed, the data is not uploaded successfully.

Source: Internet
Author: User

To protect the interests of our individual and company, we need to confuse the software when releasing the APK package, and in the obfuscation project, the system automatically blocks unused classes and optimizes the code. Therefore, obfuscation is strongly recommended when the APK is released. After the software obfuscation, it is found that the client and the server cannot communicate with each other, that is, the APK cannot receive server data, and the server cannot accurately accept the data uploaded by the terminal. After a variety of tossing, we finally found the problem of confusion.

APK and server communication format is JSON format, using gson-2.2.1.jar jar package as auxiliary tool. During the obfuscation process, the-keep method is used to indicate that the gson jar package is not obfuscated. After the obfuscation is completed, the software can run, however, when you want to upload data to the server, the server cannot accept the uploaded data. After a variety of tests, the final APK is obfuscated as usual, hitting a breakpoint on the server end, analyzing the received data, and finding that the format is incorrect. The fields of the uploaded data are different from those of the actual class. In this case, we suddenly realized that the basic data classes were obfuscated in the original obfuscation, resulting in the original fields becoming the corresponding 'a, B, C, S ..... and data format.
C, S. The classes on the server are not obfuscated. For the original fields, use

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

In this way, the corresponding data cannot be obtained.

The solution is to use-keep to indicate that the classes used in communication are not obfuscated. Ensure that the client format corresponds to the server format before data can be converted. -Modify the usage of keep in proguor-project.txt as follows:

# 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.** {*;}

Perform operations as needed during use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.