Android Auto使用DHU调试时出现ANR(GET_APP_VERSION)及Binder异常问题求助
Android Auto适配DHU测试ANR问题排查
问题背景
- 正在开发适配Android Auto的移动应用,使用DHU(Desktop Head Unit,桌面车机模拟单元)搭建测试环境时,触发ANR(Application Not Responding,应用无响应)弹窗,无法正常查看车机端的应用布局
- 抓取到的关联Logcat报错如下:
2021-03-04 15:36:07.654 19595-19616/com.ooono.app W/Binder: Caught a RuntimeException from the binder stub implementation. java.lang.SecurityException: Binder invocation to an incorrect interface at android.os.Parcel.nativeEnforceInterface(Native Method) at android.os.Parcel.enforceInterface(Parcel.java:650) at androidx.car.app.ICarApp$Stub.onTransact(ICarApp.java:207) at android.os.Binder.execTransactInternal(Binder.java:1159) at android.os.Binder.execTransact(Binder.java:1123)
已实现的核心代码与配置
车机端服务核心实现
import android.content.Intent import android.content.pm.ApplicationInfo import androidx.car.app.* import androidx.car.app.model.* import androidx.car.app.navigation.model.NavigationTemplate import androidx.car.app.validation.HostValidator class CarActivity : CarAppService() { override fun createHostValidator(): HostValidator { return if (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE !== 0) { HostValidator.ALLOW_ALL_HOSTS_VALIDATOR } else { HostValidator.Builder(this) .addAllowedHosts(R.array.hosts_allowlist_sample) .build() } } override fun onCreateSession(): Session { return object : Session(){ override fun onCreateScreen(intent: Intent): Screen { return NavScreen(carContext) } } } class NavScreen(carContext: CarContext) : Screen(carContext){ override fun onGetTemplate(): Template { val listTemplate = ListTemplate.Builder().apply { addSectionedList(SectionedItemList.create( ItemList.Builder() .addItem(Row.Builder().setTitle("list 1").addText("hellowrodl").build()) .build(),"List" )) } return listTemplate.build() } private fun actionStrip():ActionStrip{ val strip = ActionStrip.Builder().apply { addAction(Action.Builder().setIcon(CarIcon.APP_ICON).setTitle("Ooono").setOnClickListener { }.build()) }.build() return strip } } }
Manifest声明配置
<meta-data android:name="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc" /> <service android:name=".aa.CarActivity" android:label="@string/common_app_name" android:icon="@mipmap/ic_launcher" android:foregroundServiceType="location" android:exported="true"> <intent-filter> <action android:name="androidx.car.app.CarAppService"/> <category android:name="androidx.car.app.category.NAVIGATION"/> </intent-filter> </service>
问题定位与解决方案
- 核心触发原因:版本不匹配导致Binder接口校验失败
日志中出现的SecurityException: Binder invocation to an incorrect interface是ANR的直接诱因:项目引入的androidx.car.app依赖版本,和当前DHU内置的车机Host端跨进程通信接口版本存在差异,导致Binder传输过程中接口合法性校验不通过,Host端无法接收到应用返回的Session初始化结果,等待超时后触发ANR弹窗。
解决方式:将androidx.car.app:car依赖版本调整为和当前DHU对应的Android Auto SDK版本完全对齐,禁止跨大版本调用未做向后兼容的Binder接口。 - 配置疏漏:车机应用描述文件缺少类型声明
Manifest中引用的automotive_app_desc.xml资源文件,未按照导航类Android Auto应用的规范声明对应的应用能力,会导致Host端校验应用类型不匹配,进一步放大接口调用的异常概率。
解决方式:在res/xml/automotive_app_desc.xml中补充导航能力声明:<automotiveApp> <uses name="navigation" /> </automotiveApp> - 实现疏漏:导航类应用的模板结构不满足Host校验规则
代码中定义的actionStrip()动作栏没有被绑定到实际返回的ListTemplate模板上,导航类应用的模板结构缺失必填的动作栏组件时,部分版本的Host会因为模板校验失败拒绝渲染界面,拉长等待时长触发ANR。
解决方式:在构建ListTemplate时调用setActionStrip(actionStrip())完成动作栏绑定,同时补充基础头部组件,满足导航类应用的模板结构规范。
内容的提问来源于stack exchange,提问作者MikkelT
相关产品推荐
相关产品推荐

