三思笔记

Last week I was working on a feature that syncs data between phone and backend. That sync mechanism is triggered under certain conditions and we needed a way to bypass those conditions and force the sync when enabling some preference in debug mode. Leaving aside the details, the resulting code was something like the following:

上周,我正在开发一项功能,该功能可以在手机和后端之间同步数据。 该同步机制在某些条件下触发,我们需要一种方法来绕过这些条件,并在调试模式下启用某些首选项时强制进行同步。 抛开细节,最终的代码如下:

fun syncData() {if (shouldForceSync() || shouldSync()) {// Trigger the process}
}private fun shouldForceSync() = BuildConfig.DEBUG && isForceSyncPreferenceEnabled()private fun shouldSync(): Boolean {// Real validation rules here
}

Pretty simple. I relied on the BuildConfig.DEBUGconstant generated by Android Studio.

很简单 我依靠Android Studio生成的BuildConfig.DEBUG常量。

问题 (The problem)

Everything seemed fine to me until I received a comment in the pull request:

在我收到请求请求的评论之前,一切对我来说似乎还不错:

I would not want to ship Debug functionality in our Production APK/Bundle code.

我不想在我们的Production APK / Bundle代码中提供调试功能。

I agree that we shouldn’t ship debug functionality in the published artifact, and there are many reasons for that:

我同意我们不应该在已发布的工件中提供调试功能,这有很多原因: