之前项目中使用了retrofit网络请求框架和protobuf2的数据结构。因为protobuf3出来也有一段时间了,所以准备在新项目中换成protobuf3,但是在如果还是使用以前的那些库会导致编译出来的proto文件全部报错以及retrofit的converter-protobuf无法解析服务端返回的数据,下面给出proto2时候的需要的包,以及proto3时候需要的包。
protobuf2:
compile 'com.squareup.retrofit2:converter-protobuf:2.1.0'
compile 'com.google.protobuf:protobuf-java:2.6.1'
protobuf3:
compile 'com.squareup.retrofit2:converter-protobuf:2.2.0'
compile 'com.google.protobuf:protobuf-java:3.2.0'
要注意,'com.squareup.retrofit2:converter-protobuf'这个库的版本如果是2.1.0或者一下的话是解析不出来protobuf3的数据结构的,converter中用了反射的方法去获取字段的,proto2和proto3是不一样的,所以需要升级到最新的'com.squareup.retrofit2:converter-protobuf:2.2.0'