gradle

Topics related to gradle:

Getting started with gradle

Gradle is an open-source, general-purpose build tool. It is popular in the Java community and is the preferred build tool for Android.

Highlighted Gradle features

  • Declarative build scripts are code written in Groovy or Kotlin.
  • Lots of core and community plugins which use a flexible, convention-based approach
  • Incremental builds such that tasks whose dependencies who haven't changed aren't rerun.
  • Built-in dependency resolution for Maven and Ivy. Contributed plugins provide dependency resolution from other repositories such as npm.
  • First-class multi-project builds.
  • Integration with other build tools like Maven, Ant and others.
  • Build Scans that increase developers' the ability to collaborate on and optimize Gradle builds.

More information

If you want to learn more about Gradle features can look at the Overview part of the Gradle User Guide.

If you want to try Gradle can check out the guides here. You can walk through a Java quickstart guide, learn how use Gradle for the first time, and migrate from another build tool.

Gradle Plugins

Initializing Gradle

Terminology

  • Task - an atomic piece of work which a build performs. Tasks have inputs, outputs and task dependencies.
  • dependencies {} - Declares File or binary dependencies necessary to execute tasks. For example, org.slf4j:slf4j-api:1.7.21 is shorthand coordinates to a Maven dependency.
  • repositories {} - How Gradle finds files for external dependencies. Really, just a collection of files organized by group, name, and version. For example: jcenter() is a convenience method for maven { url 'http://jcenter.bintray.com/' } }, a Bintray Maven repository.

IntelliJ IDEA Task Customization

The three basic files of an IntelliJ project - the ipr, iws, and iml files - can be accessed as in gradle in the idea task through

project.ipr
module.iml
workspace.iws

using the .withXml lets you access the xml. Using the .asNode() on that turns it into a groovy xml node.

Ex:

project.ipr.withXml { provider ->
    def node = provider.asNode()

From there it's pretty simple - to modify gradle to configure IntelliJ projects for you, take the file as it starts, perform the actions you'd like gradle to take (inside IntelliJ), and then diff the new file with the old file. You should see what XML you'll need to customize the idea job. You'll also need to take note of where in the xml it's located.

One other thing to consider is that you don't want duplicate nodes within the IntelliJ files if you run the gradle idea multiple times. So, you'll want to search for the node you'd like to make and if it's not there, you can create and insert it.

Pitfalls:

Sometimes, when using == for string comparison in the find method, it fails. When testing and I find that to be the case, I use .contains.

When searching for nodes, not all nodes have the attribute you're using as a criteria, so be sure to check for null.

Dependencies

Gradle Wrapper

Gradle Performance

Gradle Init Scripts

Including Native Source - Experimental

Task dependencies

doLast

Note, that in a gradle 3.x more idiomatic way task definition: using explicit doLast{closure} notation instead "leftShift"(<<) operator preferable.(leftShift has been deprecated in a gradle 3.2 is scheduled to be removed in gradle 5.0.)

task oldStyle << {
    println 'Deprecated style task'
 }

is equivalent to:

task newStyle {
    doLast {
    println 'Deprecated style task'
    }
 }

Ordering tasks

Please note that mustRunAfter and shouldRunAfter are marked as "incubating" (as of Gradle 3.0) which means that these are experimental features and their behavior can be changed in future releases.

There are two ordering rules available:

  • mustRunAfter
  • shouldRunAfter

When you use the mustRunAfter ordering rule you specify that taskB must always run after taskA, whenever both taskA and taskB will be run.

The shouldRunAfter ordering rule is similar but less strict as it will be ignored in two situations:

  • if using that rule introduces an ordering cycle.
  • when using parallel execution and all dependencies of a task have been satisfied apart from the shouldRunAfter task, then this task will be run regardless of whether its shouldRunAfter dependencies have been run or not.

Using third party plugins

Auto Increment Version Number Using Gradle Script For Android Applications