【问题标题】:Firebase timestamps missing from written data in release build发布版本中写入的数据中缺少 Firebase 时间戳
【发布时间】:2017-02-21 14:46:08
【问题描述】:

这是一个带有 Firebase Android SDK 9.6.1 的 Android 应用。我将数据对象用于所有数据库读/写操作,其中许多包含时间戳。每个带时间戳的数据类都包含以下属性:

@PropertyName("created_at")
protected Object createdAt = ServerValue.TIMESTAMP;

@PropertyName("updated_at")
protected Object updatedAt = ServerValue.TIMESTAMP;

@Exclude
@Nullable
public Long getCreatedAtMillis() {
    return createdAt instanceof Long ? (Long)createdAt : null;
}

@Exclude
@Nullable public Long getUpdatedAtMillis() {
    return createdAt instanceof Long ? (Long)updatedAt : null;
}

在调试版本中,一切正常。成功写入示例日志:

D/RepoOperation: set: /comments/-KTtr6cXy222oW2kcaGL
D/DataOperation: set: /comments/-KTtr6cXy222oW2kcaGL {
                   comment=Debug test
                   commenter=M6PsqPsESGfcY7CKa9V8Gcdo1qu2
                   context=observations
                   created_at={.sv=timestamp}
                   id=-KTtr6cXy222oW2kcaGL
                   parent=-KTpqfowI4T25gmXCfKY
                   updated_at={.sv=timestamp}
                 }
D/Connection: conn_1 - Sending data: {d={b={d={id=-KTtr6cXy222oW2kcaGL, context=observations, commenter=M6PsqPsESGfcY7CKa9V8Gcdo1qu2, parent=-KTpqfowI4T25gmXCfKY, created_at={.sv=timestamp}, updated_at={.sv=timestamp}, comment=Debug test}, p=comments/-KTtr6cXy222oW2kcaGL}, r=21, a=p}, t=d}

在发布模式下构建相同的代码,写入数据中缺少时间戳字段导致验证错误:

10-12 13:13:48.014 5735-5863/org.naturenet D/RepoOperation: set: /comments/-KTtsg2SpkVTR9zYWgvs
10-12 13:13:48.014 5735-5863/org.naturenet D/DataOperation: set: /comments/-KTtsg2SpkVTR9zYWgvs {
                                                              comment=Release test
                                                              commenter=M6PsqPsESGfcY7CKa9V8Gcdo1qu2
                                                              context=observations
                                                              id=-KTtsg2SpkVTR9zYWgvs
                                                              parent=-KTpqfowI4T25gmXCfKY
                                                            }
10-12 13:13:48.014 2974-2974/? D/KeyguardUpdateMonitor: received broadcast com.lge.softkeypad.intent.HIDE
10-12 13:13:48.014 5735-5863/org.naturenet D/Connection: conn_1 - Sending data: {d={b={d={id=-KTtsg2SpkVTR9zYWgvs, context=observations, commenter=M6PsqPsESGfcY7CKa9V8Gcdo1qu2, parent=-KTpqfowI4T25gmXCfKY, comment=Release test}, p=comments/-KTtsg2SpkVTR9zYWgvs}, r=21, a=p}, t=d}
10-12 13:13:48.014 5735-5863/org.naturenet D/WebSocket: ws_1 - Reset keepAlive. Remaining: 35383
10-12 13:13:48.014 5735-5863/org.naturenet D/RepoOperation: Aborting transactions for path: /comments/-KTtsg2SpkVTR9zYWgvs. Affected: /comments/-KTtsg2SpkVTR9zYWgvs
...
10-12 13:13:48.074 5735-5874/org.naturenet D/WebSocket: ws_1 - ws message: {"t":"d","d":{"r":21,"b":{"s":"permission_denied","d":"Permission denied"}}}
10-12 13:13:48.074 5735-5863/org.naturenet D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44947
10-12 13:13:48.074 5735-5863/org.naturenet D/WebSocket: ws_1 - HandleNewFrameCount: 1
10-12 13:13:48.074 5735-5863/org.naturenet D/WebSocket: ws_1 - handleIncomingFrame complete frame: {d={b={s=permission_denied, d=Permission denied}, r=21}, t=d}
10-12 13:13:48.074 5735-5863/org.naturenet D/Connection: conn_1 - received data message: {b={s=permission_denied, d=Permission denied}, r=21}
10-12 13:13:48.074 5735-5863/org.naturenet D/PersistentConnection: pc_0 - p response: {s=permission_denied, d=Permission denied}
10-12 13:13:48.074 5735-5863/org.naturenet W/RepoOperation: setValue at /comments/-KTtsg2SpkVTR9zYWgvs failed: DatabaseError: Permission denied

更新:在被指向数据对象的 proguard 配置后,我添加了推荐的规则,然后更慷慨的规则无效。

Gradle 构建类型:

release {
    minifyEnabled false
    useProguard true
    proguardFile 'proguard-rules.pro'
}

proguard-rules.pro:

-keepattributes Signature
-keepattributes *Annotation*
-keepattributes EnclosingMethod
-keepattributes InnerClasses

-keep class org.naturenet.data.model.* { *; }

更新 2:它不是 proguard

release {
    minifyEnabled false
    useProguard false
}

与以前相同的行为。

【问题讨论】:

  • 是否配置了 ProGuard 以保留您的模型类 as described in the Setup Guide?
  • 我没有,但我现在有。不用找了。我也禁用了缩小。
  • 我没有很多使用 ProGuard 的经验。查看其他示例,我认为您需要两个星号:-keep class org.naturenet.data.model.** { *; }。例如see here。另外,应该是-keepclassmembers
  • 我已经重现了这种行为,并将直接向 Firebase 数据库团队记录一个错误,我会让你知道他们说了什么。
  • 好吧,这份报告在办公室引发了大量有趣的讨论。预期的行为是,受保护的字段始终被视为私有的,而不是公共的。当我们深入研究它时,我们发现在使用带有 Instant Run 的 Android Studio 时,这种行为不受尊重。你在使用工作室吗?如果是这样,您能否查看不使用 Instant Run 时问题是否仍然存在?

标签: android firebase firebase-realtime-database


【解决方案1】:

经过多次增量更改后,我发现 Firebase 在调试版本和发布版本中序列化类成员的方式不同。在发布中,只有public 成员被序列化。更改我的时间戳字段的可见性解决了这个问题:

@PropertyName("created_at")
public Object createdAt = ServerValue.TIMESTAMP;

@PropertyName("updated_at")
public Object updatedAt = ServerValue.TIMESTAMP;
...

正确的输出:

10-12 15:53:57.155 30970-31048/org.naturenet D/RepoOperation: set: /comments/-KTuSLOHLNQXZooBgBdz
10-12 15:53:57.155 30970-31048/org.naturenet D/DataOperation: set: /comments/-KTuSLOHLNQXZooBgBdz {
                                                                comment=Release test
                                                                commenter=M6PsqPsESGfcY7CKa9V8Gcdo1qu2
                                                                context=observations
                                                                created_at={.sv=timestamp}
                                                                id=-KTuSLOHLNQXZooBgBdz
                                                                parent=-KTpqfowI4T25gmXCfKY
                                                                updated_at={.sv=timestamp}
                                                              }

【讨论】:

  • 令人惊讶。我希望 Firebaser 能够解决这种行为。
  • 谢谢我遇到了同样的问题
猜你喜欢
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多