Google Analytics service Intent crashes on Android

2019-02-21 10:35发布

I have upgraded my app to support SDK 26 and now Google Analytics causes crashes when running on Android Oreo:

Fatal Exception: java.lang.RuntimeException: Unable to start receiver com.google.android.gms.analytics.AnalyticsReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.gms.analytics.ANALYTICS_DISPATCH cmp=com.example.android/com.google.android.gms.analytics.AnalyticsService }: app is in background uid UidRecord{3f302e5 u0a107 RCVR idle procs:1 seq(0,0,0)} at android.app.ActivityThread.handleReceiver(ActivityThread.java:3259) at android.app.ActivityThread.-wrap17(Unknown Source) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.gms.analytics.ANALYTICS_DISPATCH cmp=com.example.android/com.google.android.gms.analytics.AnalyticsService }: app is in background uid UidRecord{3f302e5 u0a107 RCVR idle procs:1 seq(0,0,0)} at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1505) at android.app.ContextImpl.startService(ContextImpl.java:1461) at android.content.ContextWrapper.startService(ContextWrapper.java:644) at android.content.ContextWrapper.startService(ContextWrapper.java:644) at com.google.android.gms.internal.zzaot.onReceive(Unknown Source:68) at com.google.android.gms.analytics.AnalyticsReceiver.onReceive(Unknown Source:11) at android.app.ActivityThread.handleReceiver(ActivityThread.java:3252) at android.app.ActivityThread.-wrap17(Unknown Source) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

This is my relevant Manifest:

<meta-data
    android:name="com.google.android.gms.analytics.globalConfigResource"
    android:resource="@xml/analytics"/>
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version"/>

<!-- Optionally, register AnalyticsReceiver and AnalyticsService to support background
     dispatching on non-Google Play devices -->
<receiver
    android:name="com.google.android.gms.analytics.AnalyticsReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH"/>
    </intent-filter>
</receiver>
<service
    android:name="com.google.android.gms.analytics.AnalyticsService"
    android:enabled="true"
    android:exported="false"/>

<!-- Optionally, register CampaignTrackingReceiver and CampaignTrackingService to enable
     installation campaign reporting -->
<receiver
    android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER"/>
    </intent-filter>
</receiver>
<service
    android:name="com.google.android.gms.analytics.CampaignTrackingService"
    android:exported="true"/>

Having hundreds of Oreo devices crashing due to this in past 24 hours. Seems to be affecting only on install. I am going to try and add android:permission="android.permission.BIND_JOB_SERVICE" to the 2 analytics services and see. Will report.

I Will be happy to provide more info if needed. Thanks

2条回答
闹够了就滚
2楼-- · 2019-02-21 10:54

because CampaignTrackingService is now Deprecated

just remove this line from your manifest

<service android:name="com.google.android.gms.analytics.CampaignTrackingService"
         android:exported="true"/>
查看更多
一纸荒年 Trace。
3楼-- · 2019-02-21 10:58

I recommend upgrading to play-services version 11.4.0. Add this in your Gradle file:

compile "com.google.android.gms:play-services-analytics:11.4.0

From the Play Services Revision 11.4.0, analytics section:

  • Added a new class AnalyticsJobService. This class provides compatibility support for Android O and is used by Analytics to upload data. No action is necessary to use this class, it’s added automatically as part of the Analytics package.
  • Deprecated the getContext() method of the AnalyticsService class.
  • Deprecated the CampaignTrackingService class.

and also you have to get permission. android:permission="android.permission.BIND_JOB_SERVICE"

See below https://developers.google.com/android/reference/com/google/android/gms/analytics/AnalyticsJobService

查看更多
登录 后发表回答