I have an app that runs on several mobile devices running either Fedora or Android. To consolidate my codebase and distribution I would like to determine which OS I am on. I tried System.getProperty("os.name"), but that just returns "Linux". Is there something unique to Android in the System properties? Thanks
Asked
Active
Viewed 9,796 times
28
-
4Possible duplicate of [How do I programmatically determine operating system in Java?](https://stackoverflow.com/q/228477/608639) – jww Oct 04 '19 at 03:02
-
@jww the "os.name" property is `null` on Android, which most of those answers seem to depend on. The ones that don't rely on Apache Commons, which has no check for Android. – TheWanderer Dec 16 '20 at 01:07
5 Answers
32
There are several properties you could check. Candidates are:
- java.vendor.url --> http://www.android.com
- java.vm.name --> Dalvik (I don't know, which one Fedora is using...)
- java.vm.vendor --> The Android Project
- java.vendor --> The Android Project
Maybe you want to check by yourself?
Properties p = System.getProperties();
Enumeration keys = p.keys();
while(keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = (String) p.get(key);
System.out.println(key + " >>>> " + value);
}
-
3
-
2Hi, This is very helpful indeed. Is there any official word from the Android folks as to which of these will remain invariant? Thanks. – Animesh Jul 29 '11 at 09:55
-
4
-
-
5From Android 5 onward, java.runtime.name returns null. So, it is not reliable. – Ahmet Noyan Kızıltan Nov 19 '14 at 22:13
7
I do not know Android but if you do not find some unique system property you can sometimes identify the system if some specific class exists there. So you can do the following:
boolean isAndroid() {
try {
Class.forName("the class name");
return true;
} catch(ClassNotFoundException e) {
return false;
}
}

AlexR
- 114,158
- 16
- 130
- 208
-
1+1 you could look for a class within the android packages. Activity for instance. – Vincent Mimoun-Prat Dec 23 '10 at 14:49
-
1That's also a good idea, but the source may or may not have android.jar. – Tim OMalley Jan 21 '11 at 15:01
-
This would not work when using a test tool like Robolectrics. – Rafael Winterhalter Jan 20 '17 at 13:22
-
1I'm using this approach with the `android.os.Build` class - which is the class that Android applications are expected to check for different Android platform configurations, see https://developer.android.com/reference/android/os/Build. So if your application wants to know - not only whether its Android - but also what kind of Android you are running under, then you'd actually want to get a class ref to `Build`. – Guss Oct 21 '18 at 08:35
-
Alternatively, you could look for a class that the Android platform does not provide, like `java.lang.instrument.ClassFileTransformer` (since Java 5). – neuralmer Oct 02 '20 at 15:18
3
Here is some code that I wrote using the information from this page, in case you want to copy-paste:
private static YLogger ylogger;
public static YLogger getLogger() {
if (ylogger == null){
// need to find a new logger. Let's check if we have Android running
if (System.getProperty("java.vm.name").equalsIgnoreCase("Dalvik")){
ylogger = new AndroidLogger();
ylogger.d("YLoggerFactory", "Instantiating Android-based logger");
} else {
// fallback option, system logger.
ylogger = new SystemLogger();
ylogger.d("YLoggerFactory", "Instantiating System-based logger");
}
}
return ylogger;
}
-
2
-
Kind of hack but this might help `boolean isAndroid; try { Class.forName("android.app.Activity"); isAndroid = true; } catch (ClassNotFoundException e) { isAndroid = false; }` – jasxir Oct 15 '15 at 09:03
-
how do you compile this on the backend (without Android libs)? It will fail because of missing AndroidLogger class – Luka Bradeško Jan 19 '19 at 20:49
2
The list of defined system properties is here: https://developer.android.com/reference/java/lang/System#getProperties()
I'm using
boolean android = "The Android Project".equals(System.getProperty("java.specification.vendor"));

Alex - GlassEditor.com
- 14,957
- 5
- 49
- 49
-
this seems like the most stable and quite clear and verbose solution. on a normal/Java.Oracle JVM, this same property returns `"Oracle Corporation"`. – hoijui Sep 24 '18 at 12:22
-
Java was taken over from SUN by Oracle while Java 6 was the latest version, so at least till Java 5, this would have been a different value. – hoijui Sep 24 '18 at 12:30
0
I use this in my processing sketch to determine in which mode I'm running i.e. where I'm running it.
enum Mode {
java, android
}
Mode getMode() {
return System.getProperty("java.runtime.name").equals("Android Runtime") ? Mode.android : Mode.java;
}
if (getMode() == Mode.java){
// do something
// eg: do something that android can't handle
} else {
// do android stuff
// eg: scale the sketch by 2 to improve visibility
}

Phani Rithvij
- 4,030
- 3
- 25
- 60
-
-
-
It shouldn't work in Java in general. == only compares if the objects are the exact same. equals() will do a deep comparison of the char arrays. – Peter Quiring Jan 16 '20 at 15:13
-
It might work by chance if the compiler is de-duplicating strings because both objects would be the same. – Peter Quiring Jan 16 '20 at 15:17
-