Platform define: ANDROID or __ANDROID__?

7,628 views
Skip to first unread message

Peter

unread,
Sep 2, 2010, 8:37:13 AM9/2/10
to android-ndk
Hi,
in our multi-platform code (using CMake), I somehow only see

#ifdef __ANDROID__
#endif

but shouldn't it be

#ifdef ANDROID
#endif

as I saw from the flags defined by the original NDK. What should we
rely on in future, is ANDROID somewhat official?

Peter

Damien Lespiau

unread,
Sep 2, 2010, 12:47:35 PM9/2/10
to andro...@googlegroups.com

With the 2.2 NDK, If you use ndk-build with V=1 you can see the actual
gcc command line that defines -DANDROID.

Hopefully it does not change between the NDK versions :p

--
Damien

Angus Lees

unread,
Sep 2, 2010, 11:42:09 PM9/2/10
to andro...@googlegroups.com
 On the other hand, the Android toolchain patches define __ANDROID__ without needing any explicit -D arguments.

I have no idea which one is "official", but I would personally prefer it if everything switched to __ANDROID__, and I even go as far as patching the few ANDROID places in my copy of the bionic headers to use __ANDROID__ exclusively ;)

 - Gus

Peter

unread,
Sep 3, 2010, 3:21:24 AM9/3/10
to android-ndk

> I have no idea which one is "official", but I would personally prefer it if
> everything switched to __ANDROID__, and I even go as far as patching the few
> ANDROID places in my copy of the bionic headers to use __ANDROID__
> exclusively ;)

Hum, I think we will keep both, ANDROID and __ANDROID__ in the Cmake
file for the Android build for time being. But of course it would be
nicer if we would agree on one, because the project is big and these
defines are spread.

Peter

David Turner

unread,
Sep 3, 2010, 12:35:41 PM9/3/10
to andro...@googlegroups.com
__ANDROID__ should be the official one, but some ANDROID crept in the system.
I'm not sure it's totally necessary to rely on ANDROID, we may remove that from future NDK releases.

__ANDROID__ shall always be defined by the toolchain, without a need for special flags though, so please rely on that instead in your code.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Peter

unread,
Sep 6, 2010, 3:05:23 AM9/6/10
to android-ndk
So it's the other way around than i thought.
Because a file compiled with the official NDK toolchain only defines -
DANDROID, not -D__ANDROID__. Or am I missing the __ANDROID__ just
because you don't see it in the commandline, it's defined in the GCC
internally as already Angus mentioned?
I actually never checked that, will test it with a hello world :)

David Turner

unread,
Sep 6, 2010, 4:08:08 AM9/6/10
to andro...@googlegroups.com
Yes, __ANDROID__ is defined by GCC

Peter

unread,
Sep 6, 2010, 8:18:19 AM9/6/10
to android-ndk
Well, I tested it and now I'm confused, because without any flags
defined this _doesn't_ fail:

#if defined(__ANDROID__)
bla i = 5;
#endif


And if I print the build-in definitions using:
echo | arm-eabi-g++ -E -dM -

there is nothing defining __ANDROID__ (using NDK r4b).

So it's necessary to define it in the CMakeLists.txt?

alan

unread,
Sep 6, 2010, 11:34:29 AM9/6/10
to android-ndk
I've just updated all our files to use __ANDROID__ instead of ANDROID
and am having the same problem as the above user that __ANDROID__
doesn't seem to be defined (I am using the crystax build if that makes
any difference)

Doug Schaefer

unread,
Sep 6, 2010, 11:51:48 AM9/6/10
to andro...@googlegroups.com
On Mon, Sep 6, 2010 at 11:34 AM, alan <al...@birtles.org.uk> wrote:
> I've just updated all our files to use __ANDROID__ instead of ANDROID
> and am having the same problem as the above user that __ANDROID__
> doesn't seem to be defined (I am using the crystax build if that makes
> any difference)

It doesn't seemed to be defined in the official NDK either...

I ran into this when I was playing with OGRE and same result. I had to
define __ANDROID__ myself.

> On Sep 6, 1:18 pm, Peter <peterholtw...@gmail.com> wrote:
>> Well, I tested it and now I'm confused, because without any flags
>> defined this _doesn't_ fail:
>>
>> #if defined(__ANDROID__)
>> bla i = 5;
>> #endif
>>
>> And if I print the build-in definitions using:
>> echo | arm-eabi-g++ -E -dM -
>>
>> there is nothing defining __ANDROID__ (using NDK r4b).
>>
>> So it's necessary to define it in the CMakeLists.txt?
>>
>> On 6 Sep., 10:08, David Turner <di...@android.com> wrote:
>>
>> > Yes, __ANDROID__ is defined by GCC
>>
>>
>

Tim Mensch

unread,
Sep 6, 2010, 3:35:03 PM9/6/10
to andro...@googlegroups.com, Peter
On 9/6/2010 6:18 AM, Peter wrote:
> Well, I tested it and now I'm confused, because without any flags
> defined this _doesn't_ fail:
>
> #if defined(__ANDROID__)
> bla i = 5;
> #endif
I can verify this; as a result of the earlier messages in this thread, I
changed to __ANDROID__ for my #define flag, but I had to change my
-DANDROID to -D__ANDROID__ to get it to work, as Peter described above.

To be fair, I'm using the Crystax build of the NDK, which may
potentially be missing __ANDROID__ ?

Tim


Angus Lees

unread,
Sep 6, 2010, 4:52:10 PM9/6/10
to andro...@googlegroups.com, Peter
Oh, sorry for the misinformation :(

I've been using custom-built toolchains for so long I have very little idea of what the NDK gcc actually does...  I can confirm that the new gcc-4.5 "arm-none-linux-androideabi" target declares __ANDROID__ internally (see http://gcc.gnu.org/onlinedocs/gcc/GNU_002fLinux-Options.html for example).

 - Gus

Doug Schaefer

unread,
Sep 6, 2010, 5:46:03 PM9/6/10
to andro...@googlegroups.com

Wow, that's stunning news that Android support has reached the gcc
mainline. That should mean Android support in the CodeSourcery
toolchain can't be far behind.

Mind you, it's too bad that we're scrambling around trying to find
alternative toolchains that support the features we really need.

Tim in Boulder

unread,
Sep 6, 2010, 6:14:08 PM9/6/10
to android-ndk
On Sep 6, 2:08 am, David Turner <di...@android.com> wrote:
> Yes, __ANDROID__ is defined by GCC

Not in android-ndk-r4b. I added this to hello-jni.c:

#ifndef __ANDROID__
#error
#endif

And then built:

$ /f/Devel/android-ndk-r4b/ndk-build
Gdbserver : [arm-eabi-4.4.0] /cygdrive/c/Users/tim/projects/
android/jni_std/libs/armeabi/gdbserver
Gdbsetup : /cygdrive/c/Users/tim/projects/android/jni_std/libs/
armeabi/gdb.setup
Gdbsetup : + source directory /cygdrive/c/Users/tim/projects/
android/jni_std/jni
Compile thumb : hello-jni <= /cygdrive/c/Users/tim/projects/android/
jni_std/jni/hello-jni.c
/cygdrive/c/Users/tim/projects/android/jni_std/jni/hello-jni.c:21:2:
error: #error
make: *** [/cygdrive/c/Users/tim/projects/android/jni_std/obj/local/
armeabi/objs/hello-jni/hello-jni.o] Error 1

So it does not seem to be defined by the current tool chain, unless by
"defined by GCC" you mean it's defined by some internal build of GCC
that doesn't come with the NDK. Angus just pointed out that it's
defined in GCC 4.5.0, for example.

Tim

David Turner

unread,
Sep 7, 2010, 8:29:55 AM9/7/10
to andro...@googlegroups.com
Sorry guys, seems to be a bug in the toolchain. I'll look for it.

I would recommend defining __ANDROID__ explicitely for now, it should be the only thing that NDK users should be testing again.

Thanks a lot for your time, sorry for the misinformation.

- David


--
Message has been deleted

Jeffrey Walton

unread,
Feb 23, 2013, 6:58:46 AM2/23/13
to andro...@googlegroups.com
On Sat, Feb 23, 2013 at 4:10 AM, mani krishnan <drmanik...@gmail.com> wrote:
To see what is being defined for the platfrom, a useful command is:

cpp -dM < /dev/null | sort

Do it under the crossed environment.

Jeff

Joshua Zhu

unread,
Sep 12, 2017, 12:41:01 PM9/12/17
to android-ndk
Hi all

I want to know does this method still works on the latest Android platform? Or if there is new method to get the platform information?

Thanks a lot!

在 2010年9月7日星期二 UTC+8下午8:29:55,David Turner写道:

Dan Albert

unread,
Sep 12, 2017, 2:13:22 PM9/12/17
to android-ndk
All Android targets get __ANDROID__ (the compiler defines this). ANDROID should not be used. That's defined by some build systems, and does not mean anything useful in any of the build systems I know. In AOSP, ANDROID it confusingly means "this is AOSP". It's even defined for Windows targets!

If you more specifically want to know that the code is being built with the NDK (as opposed to system code in the AOSP build system), r16 adds __NDK__ (and a handful of other things like __NDK_MAJOR__ and whatnot). Prior to r16 there wasn't a reliable way to detect this.

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.

Alex Cohn

unread,
Sep 13, 2017, 10:08:10 AM9/13/17
to android-ndk
On Tuesday, September 12, 2017 at 9:13:22 PM UTC+3, Dan Albert wrote:
All Android targets get __ANDROID__ (the compiler defines this). ANDROID should not be used. That's defined by some build systems, and does not mean anything useful in any of the build systems I know. In AOSP, ANDROID it confusingly means "this is AOSP". It's even defined for Windows targets!

If you more specifically want to know that the code is being built with the NDK (as opposed to system code in the AOSP build system), r16 adds __NDK__ (and a handful of other things like __NDK_MAJOR__ and whatnot). Prior to r16 there wasn't a reliable way to detect this.

and there is also __ANDROID_API__ which is defined on recent releases of NDK, and let you know which platform you are targeting, e.g. 

#if __ANDROID_API__ >= 21 
...
#ednfif

BR,
Alex

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

Joshua Zhu

unread,
Oct 23, 2017, 10:08:13 AM10/23/17
to andro...@googlegroups.com
Useful and thank you all~

Best Regards,
Zhuyuanxuan

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
Reply all
Reply to author
Forward
0 new messages