0

我的应用支持 Android 1.5 (API3) 到 4.1.2 (API16)。当我添加 GoogleAnalyticsV2 并重新测试它时 - 它在模拟器中出现 API3 错误:

11-08 20:00:58.813: E/dalvikvm(732): Could not find method
android.content.pm.PackageManager.getInstallerPackageName, referenced
from method
com.google.analytics.tracking.android.GAThread.fillAppParameters 11-08
20:00:58.887: W/dalvikvm(732): VFY: unable to resolve virtual method
226: Landroid/content/pm/PackageManager;.getInstallerPackageName
(Ljava/lang/String;)Ljava/lang/String; 11-08 20:00:58.925:
W/dalvikvm(732): VFY:  rejecting opcode 0x6e at 0x000c 11-08
20:00:58.934: W/dalvikvm(732): VFY:  rejected
Lcom/google/analytics/tracking/android/GAThread;.fillAppParameters
(Ljava/util/Map;)V 11-08 20:00:58.934: W/dalvikvm(732): Verifier
rejected class Lcom/google/analytics/tracking/android/GAThread; 11-08
20:00:58.964: D/AndroidRuntime(732): Shutting down VM 11-08
20:00:58.964: W/dalvikvm(732): threadid=3: thread exiting with
uncaught exception (group=0x4000fe70) 11-08 20:00:58.985:
E/AndroidRuntime(732): Uncaught handler: thread main exiting due to
uncaught exception 11-08 20:00:59.084: E/AndroidRuntime(732):
java.lang.VerifyError: com.google.analytics.tracking.android.GAThread
11-08 20:00:59.084: E/AndroidRuntime(732):  at
com.google.analytics.tracking.android.GoogleAnalytics.<init>(GoogleAnalytics.java:55)
11-08 20:00:59.084: E/AndroidRuntime(732):  at
com.google.analytics.tracking.android.GoogleAnalytics.getInstance(GoogleAnalytics.java:91)
11-08 20:00:59.084: E/AndroidRuntime(732):  at
com.google.analytics.tracking.android.EasyTracker.setContext(EasyTracker.java:309)
11-08 20:00:59.084: E/AndroidRuntime(732):  at
com.tonycode.primenumbers.ListNumsActivity.onCreate(ListNumsActivity.java:32)
11-08 20:00:59.084: E/AndroidRuntime(732):  at
...

虽然在真实设备上测试 Android 2.3.3 和 Android 4.0.3 都可以。

在上面的日志中,我发现 Analytics 无法启动,因为 API3 中不存在方法 android.content.pm.PackageManager.getInstallerPackageName,它仅存在于 [API5][http://developer.android.com /reference/android/content/pm/PackageManager.html#getInstallerPackageName(java.lang.String] 1

那么,您能帮帮我吗 - (1) 有什么方法可以在 API3 和 API4 中启用 GoogleAnalyticsV2 吗?(2) 如果没有,如果应用程序安装在 API3/4 上,如何在代码中禁用 GoogleAnalyticsV2?

4

1 回答 1

1

您可以通过检查仅对 API 5+ 启用 Google Analytics Build.VERSION.SDK_INT

if (Build.VERSION.SDK_INT >= 5) {
    // Start Google Analytics...
}

编辑

在 API 4 之前,SDK_INT不存在。不过,您可以使用稍微不太方便的RELEASE(字符串),它适用于所有 Android 版本。一些简单的RELEASE值解析应该可以解决您的问题:

if (android.os.Build.VERSION.RELEASE.startsWith("1.5") == false) {
    // Start Google Analytics...
}

如果您决定支持比 3 (Android 1.5) 更早的 API 级别,那么您还需要将它们过滤掉。

于 2012-11-08T20:51:03.337 回答