你不知道GoogleAnalyticsV2的minSdkVersion吗?

时间:2012-11-08 19:12:06

标签: android android-emulator google-analytics

我的应用支持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上进行测试时,确定无错。

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

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

1 个答案:

答案 0 :(得分:1)

您只需检查Build.VERSION.SDK_INT

,即可为API 5+启用Google Analytics
if (Build.VERSION.SDK_INT >= 5) {
    // Start Google Analytics...
}

修改

在API 4之前,SDK_INT不存在。但是,您可以使用稍微不那么方便的RELEASE value(一个字符串),这将适用于所有Android版本。对RELEASE值进行一些简单的解析可以解决您的问题:

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

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