I'm trying to read the Outside Temperature Sensor from cars running Android Auto and Android Automotive.
In order to read the SENSOR_TYPE_ENV_OUTSIDE_TEMPERATURE using the CarPropertyManager I need to include the android.car
library. However when running it on Android Auto (Desktop Head Unit) it keeps on throwing a runtimeexception
.
`java.lang.runtimeexception: stub! at android.car.car.createcar`
I tried adding the useLibrary 'android.car'
to the gradle file (as described here)
android {
compileSdkVersion 33
...
useLibrary 'android.car'
}
However when running it on Android Auto (Desktop Head Unit) I get the following error:
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/car/hardware/property/CarPropertyManager$CarPropertyEventCallback;
To fix the NoClassDefFoundError
I imported the lib from the SDK folder as follows (as described here):
val sdkDir: String = project.android.sdkDirectory.canonicalPath
val androidCarJar = "$sdkDir/platforms/android-33/optional/android.car.jar"
dependencies {
implementation(files(androidCarJar))
}
However when I try to run the app I get the follow error:
java.lang.runtimeexception: stub! at android.car.car.createcar
After searching for the error I found the following explanation 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.
Which makes sense however I tried running it both on an Emulator as well as inside a car.
Then I looked through the official samples for Android Auto and noticed that they only accessed the Car
API in the Android Automotive project but not in the Android Auto project so I'm wondering if this is an Android Automotive Feature only.. I can't find any documentation on that..
According to the docs for the Car
, the first sentence clearly says "Android Auto".
Top level car API for embedded Android Auto deployments. This API works only for devices with PackageManager#FEATURE_AUTOMOTIVE
However if I check for that Feature in the Desktop Head Unit it tells me that it's not supported.
I'm using the Car API like this (same as in the official sample app):
car = Car.createCar(carContext)
What am I missing?
The android.car
library is only for Android Automotive OS. It's a bit non-obvious, but the comment you referenced does confirm this - "embedded Android Auto" is Android Automotive OS. That doc comment was just written before there was an official name for Automotive OS.
There currently isn't a way to read outside temperature on Android Auto. See Car Hardware APIs for details on which hardware can be read from on Android Auto.