迁移Android应用至最新Google Play Services Location时遇LocationAvailability类找不到崩溃
Android应用迁移至新版Google Play Services Location组件崩溃问题
将Android应用迁移至最新版Google Play Services Location组件时,出现如下崩溃:
Exception java.lang.RuntimeException: at android.app.ActivityThread.handleReceiver (ActivityThread.java:4458) at android.app.ActivityThread.access$1700 (ActivityThread.java:261) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2165) at android.os.Handler.dispatchMessage (Handler.java:111) at android.os.Looper.loopOnce (Looper.java:238) at android.os.Looper.loop (Looper.java:355) at android.app.ActivityThread.main (ActivityThread.java:8080) at java.lang.reflect.Method.invoke at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1026) Caused by android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.google.android.gms.location.LocationAvailability at android.os.Parcel.readParcelableCreator (Parcel.java:3417) at android.os.Parcel.readParcelable (Parcel.java:3325) at android.os.Parcel.readValue (Parcel.java:3227) at android.os.Parcel.readArrayMapInternal (Parcel.java:3624) at android.os.BaseBundle.initializeFromParcelLocked (BaseBundle.java:292) at android.os.BaseBundle.unparcel (BaseBundle.java:236) at android.os.BaseBundle.containsKey (BaseBundle.java:516) at android.content.Intent.hasExtra (Intent.java:8326) at com.google.android.gms.location.LocationResult.hasResult (com.google.android.gms:play-services-location@@21.0.1:1) at com.xxx.yyy.receivers.LocationReceiver.onReceive (LocationReceiver.java:27) at android.app.ActivityThread.handleReceiver (ActivityThread.java:4449) Caused by java.lang.ClassNotFoundException: com.google.android.gms.location.LocationAvailability at java.lang.Class.classForName at java.lang.Class.forName (Class.java:454) at android.os.Parcel.readParcelableCreator (Parcel.java:3391)
使用的开发环境为Android Studio 2022.1.1 Patch 2,依赖配置如下:
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.google.android.gms:play-services-maps:18.1.0' implementation 'com.google.android.gms:play-services-location:21.0.1' implementation 'com.google.android.gms:play-services-awareness:19.0.1' }
这不是Google Play Services的Bug,问题根源在于类加载器不匹配:系统广播传递Parcelable对象时使用系统类加载器,但com.google.android.gms.location.LocationAvailability仅存在于应用自身的类加载器中,导致系统无法找到该类。
解决方法
- 避免直接在广播接收器的
onReceive中调用LocationResult.hasResult(intent),该方法内部会尝试解析Intent中的Parcelable对象,触发类加载异常。 - 改为手动判断Intent Action,再调用
LocationResult.extractResult(intent)并捕获异常:
@Override public void onReceive(Context context, Intent intent) { if ("com.google.android.gms.location.LOCATION_UPDATES".equals(intent.getAction())) { try { LocationResult result = LocationResult.extractResult(intent); // 处理位置更新逻辑 } catch (BadParcelableException | ClassNotFoundException e) { // 捕获解析异常,避免崩溃 e.printStackTrace(); } } }
- 优先使用动态注册广播接收器,静态注册的接收器由系统进程初始化,更易出现类加载器不匹配问题。
- 临时方案可降级
play-services-location至20.x.x版本,但建议优先修复代码逻辑,避免依赖旧版本。
内容的提问来源于stack exchange,提问作者greywolf82
相关产品推荐
相关产品推荐

