Conditional compiling for android

440 views
Skip to first unread message

sapna....@gmail.com

unread,
Aug 29, 2016, 2:37:33 PM8/29/16
to golang-nuts
As per the documentation in https://golang.org/pkg/go/build/ :

Using GOOS=android matches build tags and files as for GOOS=linux in addition to android tags and files.


So what if I want to compile a file on android but not for linux, because I have multiple targets , one of them being linux and the other one being android.


Any suggestions on this will be greatly appreciated, thanks !



andrey mirtchovski

unread,
Aug 29, 2016, 3:15:24 PM8/29/16
to sapna....@gmail.com, golang-nuts
you can do this for the pure linux stuff:

// +build !android,linux

Sapna Todwal

unread,
Aug 30, 2016, 2:46:53 PM8/30/16
to andrey mirtchovski, golang-nuts
I already tried this and it doesn't work . Also I need a way to enable certain code only for pure android and not linux. Any suggestions ??

Nigel Tao

unread,
Aug 31, 2016, 7:39:05 PM8/31/16
to Sapna Todwal, andrey mirtchovski, golang-nuts
On Wed, Aug 31, 2016 at 4:45 AM, Sapna Todwal <sapna....@gmail.com> wrote:
> I already tried this and it doesn't work .

That's odd. It should work. What are your filenames? What do your "//
+build" lines look like exactly?


> Also I need a way to enable
> certain code only for pure android and not linux. Any suggestions ??

// +build android

aashish...@gmail.com

unread,
Feb 1, 2018, 10:26:14 AM2/1/18
to golang-nuts
I have similar situation where I need to compile a package specifically for android and other specifically for linux.
I have included constraints mentioned above for pure linux and pure android compilation but still both files
are getting built and I get error.

GOOS=android
GOARCH=arm

Android specific files are named as *_android.go with build constraint "// +build android"
and 
Linux specific files are named as *_linux.go with build constraints "// +build !android,linux"

Thanks in advance.

Hyang-Ah Hana Kim

unread,
Feb 1, 2018, 12:08:39 PM2/1/18
to aashish...@gmail.com, golang-nuts
Strange. Can you share an example for repro?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
__

Dave Cheney

unread,
Feb 1, 2018, 2:40:31 PM2/1/18
to golang-nuts
The build system considers android and Linux to be the same for historical reasons.

Hyang-Ah Hana Kim

unread,
Feb 1, 2018, 3:56:28 PM2/1/18
to Dave Cheney, golang-nuts
Partly true.
Build tags like !android should prevent inclusion of the file.
Files named with _android suffix or with android buildtag should be included only for android.


On Thu, Feb 1, 2018 at 2:40 PM, Dave Cheney <da...@cheney.net> wrote:
The build system considers android and Linux to be the same for historical reasons.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
__

aashish...@gmail.com

unread,
Feb 2, 2018, 12:44:54 AM2/2/18
to golang-nuts
Thanks for getting back.

I finally got it to work after some experimentation on test project. 

Solution
Added tag android for android only file and named it with suffix _android
// +build android
And for linux only file added
// +build !android

Below env are set as mentioned earlier
GOOS=android
GOARCH=arm

And build command used is 
go build --tags android <main_package>


Build constraint line is added at the top of the file followed by a blank line.

They don't work if I add them after copyright header (block comment) with preceding and following blank line. 
May be only line comments are allowed above the build constraint line as mentioned on official page and not block comments.
Constraints may appear in any kind of source file (not just Go), but they must appear near the top of the file, preceded only by blank lines and other line comments. These rules mean that in Go files a build constraint must appear before the package clause.



On Friday, February 2, 2018 at 2:26:28 AM UTC+5:30, Hyang-Ah Hana Kim wrote:
Partly true.
Build tags like !android should prevent inclusion of the file.
Files named with _android suffix or with android buildtag should be included only for android.

On Thu, Feb 1, 2018 at 2:40 PM, Dave Cheney <da...@cheney.net> wrote:
The build system considers android and Linux to be the same for historical reasons.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
__
Reply all
Reply to author
Forward
0 new messages