在开发时,可以禁用谷歌Analytics(分析)Analytics

2023-09-06 03:59:23 作者:梦破碎的声音

我的问题是pretty的简单:有什么办法为谷歌Analytics(分析)在应用程序的调试证书签名将自动禁用?意味着它应该只在发布版本是活动的。谢谢你在前进。

My question is pretty simple: is there any way for Google Analytics to be disabled automatically when the application is signed with the debug certificate? Means it should be active only in release version. Thank you in advance.

推荐答案

如果您正在使用ADT 17岁以上,你可以利用BuildConfig类:

If you're using ADT 17 and above, you can utilize the BuildConfig class:

if(BuildConfig.DEBUG) {
    GoogleAnalytics googleAnalytics = GoogleAnalytics.getInstance(getApplicationContext());
    googleAnalytics.setAppOptOut(true);
}

BuildConfig 类会自动像 R.java产生是。它只包含 DEBUG 布尔值,它被设置为默认情况下,和当你导出的apk。

The BuildConfig class is automatically generated like R.java is. It only contains the DEBUG boolean, which is set to true by default, and to false when you export an apk.