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.