一行代码解决Android某个字段gson解析失败,导致网络请求整体失败的问题。
1.工程根目录加入repositories
buildscript { repositories { maven { url 'https://jitpack.io' } }}allprojects { repositories { maven { url 'https://jitpack.io' } }}
2.工程根目录build.gradle加入ClassPath
dependencies { classpath 'com.github.LianjiaTech:gson-plugin:2.0.1'}
3.工程app目录build.gradle加入依赖
apply plugin: 'com.ke.gson.plugin'
4.可选调用(监听异常json字段,建议收到后上报给服务器)
ReaderTools.setListener(new ReaderTools.JsonSyntaxErrorListener() { @Override public void onJsonSyntaxError(String exception, String invokeStack) { //upload error info to server Log.e("test", "json syntax exception: " + exception); Log.e("test", "json syntax invokeStack: " + invokeStack); }});
5.添加混淆keep
-keep class com.google.gson.** { *; }-keep class com.ke.gson.** { *; }