28

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

Tim OMalley
  • 423
  • 6
  • 14
  • 4
    Possible 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 Answers5

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);
}
jww
  • 97,681
  • 90
  • 411
  • 885
Herr K
  • 1,751
  • 3
  • 16
  • 24
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
  • 1
    That'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
  • 1
    I'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;
}
hoijui
  • 3,615
  • 2
  • 33
  • 41
Animesh
  • 1,765
  • 2
  • 22
  • 36
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