{"id":3802,"date":"2019-05-04T19:32:48","date_gmt":"2019-05-04T19:32:48","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3802"},"modified":"2019-05-04T19:32:48","modified_gmt":"2019-05-04T19:32:48","slug":"gradle-groovy-a-simple-way-to-check-if-a-gradle-task-name-has-been-invoked-e-g-assemblerelease-for-android-developers","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/gradle-groovy-a-simple-way-to-check-if-a-gradle-task-name-has-been-invoked-e-g-assemblerelease-for-android-developers\/","title":{"rendered":"gradle\/groovy: A simple way to check if a gradle task name has been invoked (e.g. &#8220;assembleRelease&#8221; for Android developers)"},"content":{"rendered":"<p>If you google for this question, you&#8217;ll find a bunch of crap answers about creating tasks and checking the task graph, bullshit.<\/p>\n<p><strong>All you need to do is check if a parameter has been passed to gradle.<br \/>\n<\/strong><\/p>\n<p>Keep it simple and stupid:<\/p>\n<p>[pastacode lang=&#8221;java&#8221; manual=&#8221;boolean%20isAssembleRelease%20%3D%20gradle.startParameter.taskNames.contains(%22assembleRelease%22)&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n<p>(If you&#8217;re working with an android project, you can define that variable before the <code>android {<\/code> section starts)<\/p>\n<p>Then, if you need to do something different somewhere else down in your script, say, ask for a key alias and key password to sign your release (because they invoked <code>.\/gradlew assembleRelease<\/code> you do:<\/p>\n<p>[pastacode lang=&#8221;java&#8221; manual=&#8221;signingConfigs%20%7B%0A%20%20%20%20%20%20%20%20release%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(isAssembleRelease)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20your%20code%20here%20to%20ask%20for%20the%20key%20alias%20and%20password%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%7D&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you google for this question, you&#8217;ll find a bunch of crap answers about creating tasks and checking the task graph, bullshit. All you need to do is check if a parameter has been passed to gradle. Keep it simple and stupid: [pastacode lang=&#8221;java&#8221; manual=&#8221;boolean%20isAssembleRelease%20%3D%20gradle.startParameter.taskNames.contains(%22assembleRelease%22)&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/] (If you&#8217;re working with an android project, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3803,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[1434,1409,1412],"class_list":["post-3802","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","tag-android","tag-gradle","tag-groovy"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2019\/05\/simple.jpg?fit=1000%2C667&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-Zk","jetpack-related-posts":[{"id":3874,"url":"https:\/\/www.gubatron.com\/blog\/coding-solved-gradle-build-android-breaks-after-upgrading-a-dependency-with-nullpointerexception-thrown-at-programclass-constantpoolentryaccept\/","url_meta":{"origin":3802,"position":0},"title":"[CODING\/SOLVED] gradle build (android) breaks after upgrading a dependency with NullPointerException thrown at ProgramClass.constantPoolEntryAccept","author":"gubatron","date":"February 27, 2020","format":false,"excerpt":"You've just upgraded one of your Android project's dependencies and when you .\/gradlew assembleRelease the build process breaks. You invoke it again with --stacktrace to find the following exception: \u00a0 [pastacode lang=\"java\" manual=\"java.lang.NullPointerException%0Aat%20proguard.classfile.ProgramClass.constantPoolEntryAccept(ProgramClass.java%3A537)%0Aat%20proguard.shrink.UsageMarker.markConstant(UsageMarker.java%3A1246)%0Aat%20proguard.shrink.UsageMarker.visitRequiresInfo(UsageMarker.java%3A1040)%0Aat%20proguard.classfile.attribute.module.ModuleAttribute.requiresAccept(ModuleAttribute.java%3A138)%0Aat%20proguard.shrink.UsageMarker.visitModuleAttribute(UsageMarker.java%3A739)%0Aat%20proguard.classfile.attribute.module.ModuleAttribute.accept(ModuleAttribute.java%3A99)%0Aat%20proguard.classfile.ProgramClass.attributesAccept(ProgramClass.java%3A619)%0Aat%20proguard.shrink.UsageMarker.markProgramClassBody(UsageMarker.java%3A124)%0Aat%20proguard.shrink.UsageMarker.visitProgramClass(UsageMarker.java%3A94)%0Aat%20proguard.classfile.visitor.MultiClassVisitor.visitProgramClass(MultiClassVisitor.java%3A67)%0Aat%20proguard.classfile.visitor.MultiClassVisitor.visitProgramClass(MultiClassVisitor.java%3A67)%0Aat%20proguard.classfile.visitor.ClassNameFilter.visitProgramClass(ClassNameFilter.java%3A128)%0Aat%20proguard.classfile.ProgramClass.accept(ProgramClass.java%3A430)%0Aat%20proguard.classfile.ClassPool.classesAccept(ClassPool.java%3A124)%0Aat%20proguard.classfile.visitor.AllClassVisitor.visitClassPool(AllClassVisitor.java%3A45)%0Aat%20proguard.classfile.visitor.MultiClassPoolVisitor.visitClassPool(MultiClassPoolVisitor.java%3A85)%0Aat%20proguard.classfile.ClassPool.accept(ClassPool.java%3A110)%0Aat%20proguard.shrink.Shrinker.execute(Shrinker.java%3A90)%0Aat%20proguard.ProGuard.shrink(ProGuard.java%3A381)%0Aat%20proguard.ProGuard.execute(ProGuard.java%3A145)%0Aat%20proguard.ProGuard.main(ProGuard.java%3A572)\" message=\"\" highlight=\"\" provider=\"manual\"\/] This is a ProGuard bug, which my friend, has been solved by the ProGuard\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3321,"url":"https:\/\/www.gubatron.com\/blog\/solved-gradle-how-to-increase-the-java-compilers-available-heap-memory\/","url_meta":{"origin":3802,"position":1},"title":"[SOLVED] Gradle: How to increase the Java Compiler&#8217;s available Heap Memory","author":"gubatron","date":"July 29, 2014","format":false,"excerpt":"The documentation is not very clear on what all the available options are... after much Googling and many different attempts finally figured out how to raise the maximum heap of the compiler from within the gradle.build script. [pastacode lang=\"java\" manual=\"apply%20plugin%3A%20%E2%80%98java%E2%80%99%0A%0AcompileJava%20%7B%0A%2F%2Fraise%20heap%0Aoptions.fork%20%3D%20%E2%80%98true%E2%80%99%0Aoptions.forkOptions.with%20%7B%0AmemoryMaximumSize%20%3D%20%E2%80%9C2048m%E2%80%9D%0A%7D%0A%7D\" message=\"\" highlight=\"\" provider=\"manual\"\/] Update: So I've noticed this works great\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3315,"url":"https:\/\/www.gubatron.com\/blog\/gradle-how-to-add-a-list-of-local-jars-to-the-build-classpath\/","url_meta":{"origin":3802,"position":2},"title":"GRADLE: How to add a list of local .jar files to the build classpath","author":"gubatron","date":"July 29, 2014","format":false,"excerpt":"Sometimes you don't want\/cant use maven repos, all you have is a bunch of local jars on disk that you want to use as part of your compilation classpath, and the freaking gradle documentation is too vague. Here is an example: [pastacode lang=\"java\" manual=\"dependencies%20%7B%0Acompile%20files(%E2%80%98lib%2Fjars%2Fgettext-commons.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Flucene-3.5.0.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjaudiotagger.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fh2-1.3.164.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fmessages.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fslf4j-api-1.7.5.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fjaudiotagger.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fmetadata-extractor-2.6.2.jar%E2%80%99%0A)%0A%7D\" message=\"\" highlight=\"\" provider=\"manual\"\/]","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3331,"url":"https:\/\/www.gubatron.com\/blog\/gradle-how-to-copy-files-from-another-jar-into-your-resulting-output-jar\/","url_meta":{"origin":3802,"position":3},"title":"GRADLE: How to copy files from another .jar into your resulting output .jar","author":"gubatron","date":"July 29, 2014","format":false,"excerpt":"In our project we like to deliver a single jar as the final product, if you need to copy files that live on an existing jar into the Gradle's output jar, this example shows you how to do that (and more) [pastacode lang=\"java\" manual=\"jar%20%7B%0A%2F%2Fthis%20is%20how%20you%20change%20the%20name%20of%20the%20output%20jar%0AarchiveName%3D%E2%80%99frostwire.jar%E2%80%99%0A%0A%2F%2Fsome%20exclusion%20rules%20to%20keep%20your%20.jar%20clean%0Aexclude(%E2%80%98META-INF%2F*.SF%E2%80%99%2C%20%E2%80%98META-INF%2F*.DSA%E2%80%99%2C%20%E2%80%98META-INF%2F*.RSA%E2%80%99%2C%20%E2%80%98META-INF%2F*.MF%E2%80%99)%0A%0A%2F%2Fhere%20we%20grab%20all%20the%20.class%20files%20inside%20messages.jar%20and%20we%20put%20them%20in%20our%20resulting%20jar%0Afrom%20(zipTree(%E2%80%98lib%2Fjars%2Fmessages.jar%E2%80%99))%20%7B%0Ainclude%20%E2%80%98**%2F*.class%E2%80%99%0A%7D%0A%0A%2F%2Fhow%20to%20manipulate%20the%20jar%E2%80%99s%20manifest%0Amanifest%20%7B%0Aattributes%20%E2%80%98Main-Class%E2%80%99%3A%20%E2%80%98com.limegroup.gnutella.gui.Main%E2%80%99%0A%7D%0A%7D\" message=\"\" highlight=\"\" provider=\"manual\"\/]","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3336,"url":"https:\/\/www.gubatron.com\/blog\/gradle-how-to-specify-resources-from-different-folders-on-your-sourceset\/","url_meta":{"origin":3802,"position":4},"title":"GRADLE: How to specify resources from different folders on your sourceSet","author":"gubatron","date":"July 29, 2014","format":false,"excerpt":"Sometimes you need to have resources in your sourceset and these may come from different locations on disk, the official documentation is lacking in real world examples, or at least I just didn't understand it very well, but from reading forums I finally got it to work. In the example\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3699,"url":"https:\/\/www.gubatron.com\/blog\/how-to-run-your-kotlin-gradle-built-app-from-the-command-line\/","url_meta":{"origin":3802,"position":5},"title":"How to run your Kotlin gradle built app from the command line","author":"gubatron","date":"July 20, 2017","format":false,"excerpt":"So you build your Kotlin app, you went through the trouble of creating a build.gradle script that you build with gradle assemble this outputs a a \"build\/libs\/kotlin.jar\" .jar file, but you have no clue how to run your Kotlin code from the command line. Doing it by hand with \"java\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3802"}],"version-history":[{"count":1,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3802\/revisions"}],"predecessor-version":[{"id":3804,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3802\/revisions\/3804"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media\/3803"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}