android - 无法使用 Android Auto 'android.car' 库 - 引发 RuntimeException : Stub

标签 android android-auto android-automotive

我正在尝试从运行 Android Auto 和 Android Automotive 的汽车读取车外温度传感器。

为了阅读SENSOR_TYPE_ENV_OUTSIDE_TEMPERATURE使用CarPropertyManager我需要包含 android.car 库。但是,当在 Android Auto(桌面主机)上运行它时,它会不断抛出 runtimeException

`java.lang.runtimeexception: stub! at android.car.car.createcar`

我尝试将 useLibrary 'android.car' 添加到 gradle 文件中(如所述 here )

android {
     compileSdkVersion 33
     ...
     useLibrary 'android.car'
}

但是,当在 Android Auto(桌面主机)上运行它时,我收到以下错误:

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/car/hardware/property/CarPropertyManager$CarPropertyEventCallback;

为了修复 NoClassDefFoundError,我从 SDK 文件夹导入了 lib,如下所示(如 here 所述):

val sdkDir: String = project.android.sdkDirectory.canonicalPath
val androidCarJar = "$sdkDir/platforms/android-33/optional/android.car.jar"

dependencies {
    implementation(files(androidCarJar))
}

但是,当我尝试运行该应用程序时,出现以下错误:

java.lang.runtimeexception: stub! at android.car.car.createcar

搜索错误后我发现了以下解释 on StackOverflow/and here

android.jar contains only stub implementation of the classes. It provides the means for you app to build, once you have your APK you must run it on an android device or emulator.

这是有道理的,但是我尝试在模拟器上以及汽车内运行它。

然后我浏览了official samples对于 Android Auto 并注意到他们只访问了 Android Automotive 项目中的 Car API,而不是 Android Auto 项目中的 API,所以我想知道这是否只是 Android Automotive 功能..我不能找到任何相关文档..

根据docs对于 Car,第一句话明确表示“Android Auto”。

Top level car API for embedded Android Auto deployments. This API works only for devices with PackageManager#FEATURE_AUTOMOTIVE

但是,如果我在桌面主机中检查该功能,它会告诉我它不受支持。

我正在使用这样的 Car API(与官方示例应用程序相同):

car = Car.createCar(carContext)

我错过了什么?

最佳答案

android.car 库仅适用于 Android Automotive 操作系统。这有点不明显,但您引用的评论确实证实了这一点 - “嵌入式 Android Auto”是 Android Automotive 操作系统。该文档评论是在 Automotive OS 正式名称出现之前写的。

目前无法在 Android Auto 上读取外部温度。请参阅Car Hardware APIs了解有关可在 Android Auto 上读取哪些硬件的详细信息。

关于android - 无法使用 Android Auto 'android.car' 库 - 引发 RuntimeException : Stub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76454276/

相关文章:

android - 谷歌地图 View 中令人不安的灰色框

java - 在后台线程上改造 2 回调 onResponse

java - acos 没有给出角度值?

java - 带有Retrofit 2.0 Android的多部分请求花费太多时间来请求

Android auto DHU - 无法从传输中读取 - 断开连接。退出

android auto - 没有回复的通知

android-x86 - 如何为 Android Automotive OS 构建 AOSP?

java - Android Auto - 后台服务与Activity之间的通信

android-auto - Android 汽车操作系统中的 TabTemplate 示例

android - 如何检测Android设备没有内置电池?