Helping out with Gradle builds for hacktoberfest
Tags: java, kotlin, android, hacktoberfest
Published: 2019-09-26 14:22:44

Using [https://gradle.org](Gradle) as your build tool? Read on. 

# #hacktoberfest

The thing you have to know about `OktoberFest`, is that in Germany, it takes place mostly in September. Don't ask me why. 

So I already contributed a pull-request for `#hacktoberfest`

{% github https://github.com/FultonBrowne/Ara-android/pull/44  %}

and a seconde one!

{% github https://github.com/FultonBrowne/Ara-Server/pull/10 %}

If you take a look, I did exactly the same thing. Let's unwrap it:

## I updated Gradle:

Always a good idea. You get more features, less bugs, more speed. 

`$ ./gradlew wrapper --gradle-version 5.6.2`
`$ ./gradlew tasks`


## I added the `build-scan` and `buildSrcVersions` Gradle plugins:

```groovy
// build.gradle

plugins {
    // :refreshVersions see https://github.com/jmfayard/buildSrcVersions/issues/77
    id("de.fayard.buildSrcVersions").version("0.6.1")
    id ("com.gradle.build-scan").version("2.4.2") 
}

buildSrcVersions {
  // Documented at https://github.com/jmfayard/buildSrcVersions/issues/53
}

buildScan {
    // ./gradlew --scan $TASKNAME 
    // see https://dev.to/jmfayard/the-one-gradle-trick-that-supersedes-all-the-others-5bpg

    termsOfServiceUrl = "https://gradle.com/terms-of-service"
    termsOfServiceAgree = "yes"
}
```


## I run `$ ./gradlew refreshVersions`


This automatically extracts the versions from the project dependencies, *and* allows to find newer available versions.

```properties
## gradle.properties
# user settings go here
# some.gradle.property=value

# Dependencies and Plugin versions with their available updates
# Generated by $ ./gradlew refreshVersions
# You can edit the rest of the file, it will be kept intact
# See https://github.com/jmfayard/buildSrcVersions/issues/77
plugin.com.github.ben-manes.versions=0.25.0
plugin.de.fayard.buildSrcVersions=0.6.1
version.com.android.tools.build..gradle=3.5.0
version.play-services-location=17.0.0
version.bottom-navigation-bar=2.1.0
version.lifecycle-extensions=2.0.0
#                # available=2.1.0
version.org.jetbrains.kotlin=1.3.31
#                # available=1.3.50
version.appcompat=1.1.0-rc01
#     # available=1.1.0
version.cardview=1.0.0
version.core-ktx=1.0.2
#    # available=1.1.0
# ....
```

Curious about this feature? 

Read the docs at [gradle :refreshVersions" generates gradle.properties with versions and available updates](https://github.com/jmfayard/buildSrcVersions/issues/77) 


- Note 1: I don't update the dependencies myself, that's the owner of the project who should do it!
- Note 2: I have to copy/paste [this boilerplate code to settings.gradle](https://github.com/FultonBrowne/Ara-android/blob/7e56db22cc7fda49359b3ca3e96d3c08ec1758bb/settings.gradle#L17-L34) so that the plugins pick up the right version


## I add the missing `./gradlew test` and `./gradlew install`

One of the meany ways Google messed up is that unlike basically every software projects on earth, Android project do not have by default the equivalent of `$ npm install` and `$ npm test`.

What makes it even dumber is that it's really trivial to add, a couple of lines to `./build.gradle`


```groovy
// build.gradle
tasks.create("install") {
    group = "custom"
    description = "Install the app"
    dependsOn(":app:installDebug")
}

tasks.create("test") {
    group = "custom"
    description = "Run the unit tests"
    dependsOn(":app:testDebugUnitTest")
}
tasks.create("hello") {
    group = "custom"
    description = "Empty Hello World task, useful to debug build problems"
}
```

Bonus: you know you should maintain a README but you are too lazy to do it? This part can now be self-documented!

```
$ ./gradlew tasks --group=custom

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Custom tasks
------------
install - Install the app
test - Run the unit tests
hello - Empty Hello World task, useful to debug build problems

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>
```


# I inspect the build with the Build Scan plugin

See my previous article:

{% link https://dev.to/jmfayard/the-one-gradle-trick-that-supersedes-all-the-others-5bpg %} 

Using this information, I could improve the build performance by just [adding a few lines to gradle.properties](https://github.com/FultonBrowne/Ara-android/blob/7e56db22cc7fda49359b3ca3e96d3c08ec1758bb/gradle.properties#L17-L25)

```
## gradle.properties

# See https://dev.to/jmfayard/configuring-gradle-with-gradle-properties-211k
org.gradle.caching=true
org.gradle.parallel=true
kotlin.code.style=official
studio.projectview=true
```

See my article on gradle.properties

{% link https://dev.to/jmfayard/configuring-gradle-with-gradle-properties-211k %}

# You can do it too!

The best thing about my niche is that what I did is totally generic and empowers the developer to do what she knows best!