All Projects → LianjiaTech → Gson Plugin

LianjiaTech / Gson Plugin

Licence: apache-2.0
辅助 Gson 库的 gradle 插件,防止 Json 数据解析类型异常。

Programming Languages

groovy
2714 projects

Projects that are alternatives of or similar to Gson Plugin

Gradle Eclipse Aar Plugin
Gradle plugin to use Android AAR libraries on Eclipse.
Stars: ✭ 127 (-4.51%)
Mutual labels:  gradle, gradle-plugin
Gradle Build Properties Plugin
Keep your secrets secret. External build properties support for your Gradle scripts.
Stars: ✭ 110 (-17.29%)
Mutual labels:  gradle, gradle-plugin
Kordamp Gradle Plugins
A collection of Gradle plugins
Stars: ✭ 100 (-24.81%)
Mutual labels:  gradle, gradle-plugin
Forma
Meta build system with Android and Gradle support.
Stars: ✭ 127 (-4.51%)
Mutual labels:  gradle, gradle-plugin
Jib
🏗 Build container images for your Java applications.
Stars: ✭ 11,370 (+8448.87%)
Mutual labels:  gradle, gradle-plugin
Gradle Quality Plugin
Gradle quality plugin for Java and Groovy
Stars: ✭ 97 (-27.07%)
Mutual labels:  gradle, gradle-plugin
Jenkins Pipeline Shared Libraries Gradle Plugin
Gradle plugin to help with build and test of Jenkins Pipeline Shared Libraries (see https://jenkins.io/doc/book/pipeline/shared-libraries/)
Stars: ✭ 103 (-22.56%)
Mutual labels:  gradle, gradle-plugin
Gradle Semantic Build Versioning
Gradle plugin to generate version-numbers and tags using semantic versioning
Stars: ✭ 69 (-48.12%)
Mutual labels:  gradle, gradle-plugin
Clojurephant
Clojure and Clojurescript support for Gradle
Stars: ✭ 118 (-11.28%)
Mutual labels:  gradle, gradle-plugin
Okbuck
OkBuck is a gradle plugin that lets developers utilize the Buck build system on a gradle project.
Stars: ✭ 1,513 (+1037.59%)
Mutual labels:  gradle, gradle-plugin
Gradle Buildconfig Plugin
A plugin for generating BuildConstants for any kind of Gradle projects: Java, Kotlin, Groovy, etc. Designed for KTS scripts.
Stars: ✭ 85 (-36.09%)
Mutual labels:  gradle, gradle-plugin
Phoenix For Vk
Yet another VK client for Android
Stars: ✭ 131 (-1.5%)
Mutual labels:  gradle, gson
Androidanimationexercise
Android 动画各种实现,包括帧动画、补间动画和属性动画的总结分享
Stars: ✭ 1,254 (+842.86%)
Mutual labels:  gradle, gradle-plugin
Vertx Gradle Plugin
An opinionated Gradle plugin for Vert.x projects
Stars: ✭ 98 (-26.32%)
Mutual labels:  gradle, gradle-plugin
Gradle Plugins
Gradle Plugin Collection
Stars: ✭ 84 (-36.84%)
Mutual labels:  gradle, gradle-plugin
Gradle Changelog Plugin
Plugin for parsing and managing the Changelog in a "keep a changelog" style.
Stars: ✭ 102 (-23.31%)
Mutual labels:  gradle, gradle-plugin
Auto Manifest
Generates AndroidManifest.xml in simple libraries so that you don't have to
Stars: ✭ 51 (-61.65%)
Mutual labels:  gradle, gradle-plugin
Gradle S3 Build Cache
An AWS S3 Gradle build cache implementation
Stars: ✭ 54 (-59.4%)
Mutual labels:  gradle, gradle-plugin
Godot
Keep track of how much time you spend on Gradle builds
Stars: ✭ 113 (-15.04%)
Mutual labels:  gradle, gradle-plugin
Bytex
ByteX is a bytecode plugin platform based on Android Gradle Transform API and ASM. 字节码插件开发平台
Stars: ✭ 2,140 (+1509.02%)
Mutual labels:  gradle, gradle-plugin

gson-plugin

强化Android-Json解析的插件,解决Android-Json解析数据类型转换异常,不影响对Gson库的使用

诞生背景

Android主要开发语言是Java,属于强数据类型语言,不少公司后台开发采用的是PHP,属于弱数据类型的语言。
客户端与服务器在进行数据传输的过程中,常常因为某个字段数据类型不一致,导致客户端gson解析失败,从而导致整个页面的数据均无法展示。

功能描述

1.当某个字段解析失败的时候,跳过该字段继续解析其它字段,保证其它正常数据可以展示出来。
2.当某个字段解析失败的时候,通过观察者模式,将异常抛出,开发者在收到异常后可以进行相应的处理(如将异常日志上传到服务器,然后推动服务端RD解决)。
3.不影响对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.1.0'
}

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

注意事项

1.如果也apply了其它plugin插件,请把 apply plugin: 'com.ke.gson.plugin' 加入到其它apply之前
原因:gson_plugin只会处理file.name包含gson的jar包,有的插件会将jar文件进行merge,统一输出为一个jar包,导致gson_plugin匹配不到,从而不会对该文件进行处理。

2.如果引用了SNAPSHOT版本,请不要使用本地缓存
工程根目录的build.gradle与app目录的build.gradle都得加入

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

3.编译失败怎么办
taskkill /im java.exe /f 然后clean,重新build

性能对比

对如下数据进行2000次循环解析:

public class TestBean {
 public String name;
 public int age;
 public String sex;
 public boolean is_success;
 public String[] array;
 public List<String> list;
 public Map<String, String> map;
 public TestBean bean;
}

使用原生gson结果:
第1次:1374ms,第2次:1430ms,第3次:1429ms,平均:1411ms

使用gson-plugin结果:
第1次:1503ms,第2次:1381ms,第3次:1418ms,平均:1434ms

结论:
gson-plugin比原生gson解析,效率略低(多执行了几行判断逻辑代码),但可忽略不计

原理说明

侵入编译流程,在编译过程中,修改gson库的字节码,修改gson解析相关的方法

支持gson库版本

支持gson库所有版本

特殊说明

2.1.0之前的版本,对Float、Double、Map数据类型的支持不全面,建议使用2.1.0及以上的版本

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].