Gradle - Detect if build is started by IDEA or commandline

471 views
Skip to first unread message

Tomáš Procházka

unread,
Sep 22, 2014, 6:00:56 AM9/22/14
to adt...@googlegroups.com
It is possible to detect in the gradde script if build was started from the IDE or from the commandline?

Artem Zinnatullin

unread,
Sep 22, 2014, 6:03:44 AM9/22/14
to adt...@googlegroups.com
If you don't need common solution, you can use params "-P" and pass some param which you can detect in build script from IDE run settings (IDEA, Android Studio can pass params to the build script)

понедельник, 22 сентября 2014 г., 14:00:56 UTC+4 пользователь Tomáš Procházka написал:

Tomáš Procházka

unread,
Sep 22, 2014, 2:26:23 PM9/22/14
to adt...@googlegroups.com
I hope, that AS put some -P or anything else when run the build process. 
I need something universal which will work of everyone, without special setup.

Dne pondělí, 22. září 2014 12:03:44 UTC+2 Artem Zinnatullin napsal(a):

Tomáš Procházka

unread,
Sep 22, 2014, 3:43:34 PM9/22/14
to adt...@googlegroups.com
I need it because I have special method App.isIDEBuild() in my app that allow me to setup special debug stuff, like prefill login screen with passwords, skip some screens to speed up development. But if app is builded from command line (on CI server), it must work as usual. So it is much better to use App.isIDEBuild() than change code itself and forgot remove something.

I'm doing it now with maven and now I'm looking for gradle solutions.

Mark Murphy

unread,
Sep 22, 2014, 4:00:37 PM9/22/14
to adt...@googlegroups.com
On Mon, Sep 22, 2014, at 15:43, Tomáš Procházka wrote:
> I need it because I have special method App.isIDEBuild() in my app that
> allow me to setup special debug stuff, like prefill login screen with
> passwords, skip some screens to speed up development. But if app is
> builded
> from command line (on CI server), it must work as usual. So it is much
> better to use App.isIDEBuild() than change code itself and forgot remove
> something.

I would differentiate based on build type, either using
BuildConfig.DEBUG (if I didn't need the CI server to make debug builds)
or a custom BuildConfig field. Basically, you anoint one build type as
being the "IDE build", and use other build types on the CI server.

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 6.0: Android
Studio-ified!

Xavier Ducrohet

unread,
Sep 22, 2014, 4:22:49 PM9/22/14
to adt...@googlegroups.com
I agree with Mark, this is what build types are for. You can have different BuildConfig constants depending on the build type you are building.

That said you can in fact detect if the build was done from the IDE. We inject  a property called android.injected.invoked.from.ide when building from the IDE


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



--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Artem Zinnatullin

unread,
Sep 22, 2014, 4:42:47 PM9/22/14
to adt...@googlegroups.com
We use build flavors + build types for this purpose, for expample, we have production and test flavors which defines custom backend base URL and other things.

As result, we have build matrix:
1) testFlavor-DEBUG.apk
2) testFlavor-RELEASE.apk
3) prodFlavor-DEBUG.apk
4) prodFlavor-RELEASE.apk

In IDE (Android Studio) you can select product flavor and build type

Tomáš Procházka

unread,
Sep 23, 2014, 2:41:24 AM9/23/14
to adt...@googlegroups.com
Thanks.
We also use build type debug/release, it control logging, enable Google Analytics, etc.
But for example crittercism we have enabled on booth debug and release build, but we want to have it of in IDE build, to do not flood statistic with dev crashes.


Dne pondělí, 22. září 2014 22:22:49 UTC+2 Xavier Ducrohet napsal(a):

Artem Zinnatullin

unread,
Sep 23, 2014, 3:04:47 AM9/23/14
to adt...@googlegroups.com
How we solved same problem:

We have two Google Analytics trackers for one project. 

Via product flavors we can pass different classes, different resources and buildConfigFields via build.gradle productFlavors closure, so for test builds (which developers use for debug purposes) we set different g_analytics.xml (with testTrackingId), so we don't worry about flooding product flavor statistics.

Just try product flavors as addition to build types, I think they will satisfy 99% of your needs

понедельник, 22 сентября 2014 г., 14:00:56 UTC+4 пользователь Tomáš Procházka написал:
It is possible to detect in the gradde script if build was started from the IDE or from the commandline?

вторник, 23 сентября 2014 г., 10:41:24 UTC+4 пользователь Tomáš Procházka написал:

Mark Murphy

unread,
Sep 23, 2014, 5:50:55 AM9/23/14
to adt...@googlegroups.com
On Tue, Sep 23, 2014, at 02:41, Tomáš Procházka wrote:
> We also use build type debug/release, it control logging, enable Google
> Analytics, etc.
> But for example crittercism we have enabled on booth debug and release
> build, but we want to have it of in IDE build, to do not flood statistic
> with dev crashes.

Then use three build types: debug,
somethingThatIsNotDebugButIsNotReleaseEither, and release. The middle
one would be whatever you consider the CI server debug build to be.

Tomáš Procházka

unread,
Sep 23, 2014, 10:58:57 AM9/23/14
to adt...@googlegroups.com
I used android.injected.invoked.from.ide and it works great, here is my code https://gist.github.com/tprochazka/36517db854f9046f0b9c


Dne pondělí, 22. září 2014 22:22:49 UTC+2 Xavier Ducrohet napsal(a):
I agree with Mark, this is what build types are for. You can have different BuildConfig constants depending on the build type you are building.
Reply all
Reply to author
Forward
0 new messages