Checking if your app is running on Horizon OS at runtime can be an alternative to creating a separate product flavor. Instead of eliminating unsupported dependencies from the build, you can check if you are running on Horizon OS at runtime and avoid calling into code that you know will not work. In this way, you can produce a single build that works on both Horizon OS and other Android platforms.
The best way to check if your code is running on Horizon OS is through the Horizon OS Support Library. This will ensure your app remains compatible with all Horizon OS releases.
Get the library
The Horizon OS Support Library is distributed through Maven Central and can be added to your Gradle build just like other dependencies:
Once the Horizon OS Support Library has been added as a dependency, use the HorizonOsDetector.isOnHorizonOs() method to determine if your app is running on Horizon OS. This method requires an Android Context as a parameter.
import horizonosx.os.HorizonOsDetector;
if (HorizonOsDetector.isOnHorizonOs(context)) {
// Execute code that only works on Horizon OS
} else {
// Execute code that does not work on Horizon OS
}
Use this check to guard calls to unsupported dependencies so they are never invoked when running on Horizon OS.
When to use runtime detection vs. product flavors
Approach
Best for
Runtime detection (this page)
Apps or libraries that need a single build artifact for both Horizon OS and other Android platforms.