已识乾坤大,犹怜草木青
由于公司业务发展,需要开发海外版项目,深入了解 Firebase 在此不得不羡慕国外开发者的幸福啊。在国内,我们可能需要集成多家的SDK,而Firebase算是集大成者。
Firebase 基于Google基础架构而构建,其提供了登录授权,用户数据分析,数据库,消息推送及性能监测与崩溃报告等功能,详细内容,请移步:Firebase官网
今天,在集成 Firebase Crashlytics 崩溃分析报告和 Firebase Performance Monitoring 性能监测时,在app/build.gradle中添加以下两句话:
// Add the Firebase Crashlytics SDK.
implementation 'com.google.firebase:firebase-crashlytics:17.1.1'
// Add the dependency for the Performance Monitoring library
implementation 'com.google.firebase:firebase-perf:19.0.7'
重新编译时遇到了以下错误:
java.lang.RuntimeException: Duplicate class com.google.protobuf.AbstractMessageLite found in modules protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder found in modules protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder$LimitedInputStream found in modules protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
大概意思是:protobuf-javalite 和 protobuf-lite包含了相同的类。
很显然,看到Duplicate 就知道是重复问题。最简单的办法就是在其中采用exclude 即可。即:
// Add the Firebase Crashlytics SDK.
implementation 'com.google.firebase:firebase-crashlytics:17.1.1'
// Add the dependency for the Performance Monitoring library
implementation 'com.google.firebase:firebase-perf:19.0.7', {
exclude group: 'com.google.protobuf'
}
这里有几个相关内容的议题:
1. java.lang.RuntimeException: Duplicate class com.google.protobuf.AbstractMessageLite when adding firebase remote config
2. Migrate Firebase SDKs from protobuf-lite to protobuf-javalite
WTF protobuf ?
今日的主角—— Google Protocol Buffers
Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.
Protocol buffers 是Google的语言无关、平台无关、可扩展的序列化结构数据格式,相较于XML,更小,更快,更简单。
推荐一篇文章:Google Protocol Buffer 的使用和原理
读完这篇文章,我们不难猜测出,为什么只有在Firebase特定的几个功能(Firebase RemoteConfig,Firebase Crashlytics 崩溃分析报告,Firebase Performance Monitoring 性能监测 and so on…)会看到protobuf()的身影。
想要更多了解Protocol buffers 的详细内容,请参看:Protobuf的Github地址