Android 5.0 (L) Service Intent must be explicit in Google analytics

后端 未结 11 1655
南旧
南旧 2020-11-29 02:01

My code worked in <5 but in Android 5.0 I\'m running into an issue that I don\'t quite understand.

10-23 10:18:18.945: E/AndroidRuntime(8987): java.lang.I         


        
11条回答
  •  孤独总比滥情好
    2020-11-29 02:12

    Since Android 5.0 (Lollipop) bindService() must always be called with an explicit intent. This was previously a recommendation, but since Lollipop it is enforced: java.lang.IllegalArgumentException: Service Intent must be explicit is thrown every time a call to bindService() is made using implicit intents. The difference between implicit and explicit intents is that the latter specifies the component to start by name (the fully-qualified class name). See the documentation about intent types here.

    The issue you are having is due to not having upgraded to a newer version of the google libraries, which comply with Android's restrictions on implicit intents when binding a service on Android 5 Lollipop. To fix the issue, you can upgrade the library to a newer version if available, or update the library code yourself and build your project with the modified version.

    If there's no suitable library upgrade in the general case, you need to modify the source code (in your case com.google.analytics.tracking.android.AnalyticsGmsCoreClient.connect()) to call intent.setPackage(packageName) before calling bindService() where intent is the first argument of the bindService() call and packageName is the name of the package containing the service the code is attempting to start (in your case "com.google.android.gms.analytics").

    You can use this code as an example how to do this: Unity's updated version of the Google Licensing Library (LVL) which calls bindService with an explicit intent and does not result in IllegalArgumentException.

    Yet another way to get around the problem is to rebuild your project and the google libraries using targetSDK no later than 19. This will make it run without crashing on Lollipop but is the less secure option and prevents you from using SDK functionality introduced in later versions (for Android 5).

提交回复
热议问题