Jag*_*uar 8 android google-analytics
我正在尝试将我的Google Analytics代码拆分为来自我的主应用程序(包com.xyz)的单独模块(包com.abc).我正面临着这些问题:
我尝试过(并且失败了):
在使用 2 个模块(主应用程序模块和库模块)的项目中实施 Google Analytics 时,我遇到了类似的问题。有一个通过 Java Reflection 的解决方案如何在库模块中获取 GA 跟踪器。
compile 'com.google.android.gms:play-services-analytics:9.0.0'这是在库模块中使用分析类所必需的。最后,您将在主应用程序模块中进行 GA 配置。您可以按照指南中的说明使用它:
// Obtain the shared Tracker instance in main module
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
Run Code Online (Sandbox Code Playgroud)
对于从自定义应用程序类获取跟踪器的库模块,您可以使用反射:
public Tracker getGATracker(Application application) {
Tracker tracker = null;
try {
Method m = application.getClass().getMethod("getDefaultTracker", (Class<?>[]) null);
Object result = null;
if (m != null) {
result = m.invoke(application, (Object[]) null);
if (result != null) {
tracker = (Tracker) result;
}
}
} catch (Exception e) {
Log.w(TAG, "Can't get GA tracker: ", e);
}
return tracker;
}
Run Code Online (Sandbox Code Playgroud)
将上面的方法与此代码片段一起使用:
// Obtain the shared Tracker instance from main module
Tracker tracker = getGATracker(getApplication());
Run Code Online (Sandbox Code Playgroud)
所以你可以在库模块中使用GA跟踪器并在主模块中进行配置。
| 归档时间: |
|
| 查看次数: |
919 次 |
| 最近记录: |