I would like to check if the current operating system is linux or android during runtime with native C. Is it possible? Because I have to execute different code depending on the OS.
Asked
Active
Viewed 679 times
2
-
1Do realize that Android != Linux, and that .so binaries may be for different architectures (ARM vs x86). See: https://stackoverflow.com/q/33933896/295004 – Morrison Chang Feb 12 '18 at 08:29
-
Jeah, but in my case I can compile for x86 and ARM. In case of Linux I have to execute a little bit different code to run my application based on this source for android https://android.googlesource.com/platform/system/core/+/android-5.0.2_r3/toolbox/sendevent.c. If possible I would like to keep code for both version same and check during runtime with switch case with part of code has to be executed. – Shazter Feb 12 '18 at 08:40
-
Is your code running in a rooted Android environment or would be run via a Android app? If by app you could just set a variable indicating environment. – Morrison Chang Feb 12 '18 at 08:52
-
3*"Because I have to execute different code depending on the OS."* - You know at compile time. Use the `__ANDROID__`, `__linux` and `__linux__` preprocessor macros. Also see [How to detect compilation by android ndk in a C/C++ file?](https://stackoverflow.com/q/6374523/608639) – jww Feb 12 '18 at 09:18
-
Is rooted Android version. Looks like the common way is to check during compile time. I hoped to had only one binary for linux and android. – Shazter Feb 12 '18 at 09:50
1 Answers
2
To ensure program runs on Android you can check environment variables named ANDROID_ROOT
and ANDROID_DATA
. Usually they are present and refer to /system
and /data
folders.
C:
You can use getenv
function from stdlib.h
:
getenv("ANDROID_ROOT");
getenv("ANDROID_DATA");
CLI:
$ env | grep ANDROID_

Vladyslav Marchenko
- 581
- 5
- 10