About
Sometime it is useful for running gradlew in terminal directly. Therefore, here are problems I have solved when I am trying to do so.
Problem 1: Insufficient space for object heap
$ ./gradlew tasks check
...
-----------------------
Error occurred during initialization of VM
Could not reserve enough space for 1572864KB object heap
...
Solution
Define an environment variable in order to limit the heap size.
$ export _JAVA_OPTIONS=-Xmx512M
Reference
Problem 2: Invalid JDK installation
$ ./gradlew tasks check
.....
* What went wrong:
Execution failed for task ':app:compileReleaseJavaWithJavac'.
> Could not find tools.jar. Please check that C:\Program Files (x86)\Java\jre1.8.0_121 contains a valid JDK installation.
....
Solution
Explicitly set the JDK path through environment variable. For me, I simply reused the JRE from Android Studio.
$ export JAVA_HOME="C:\Program Files\Android\Android Studio\jre"
An example of successful run
$ ./gradlew tasks check
.....
BUILD SUCCESSFUL
Total time: 33.858 secs
Picked up _JAVA_OPTIONS: -Xmx512M
$ env | grep JAVA
_JAVA_OPTIONS=-Xmx512M
JAVA_HOME=C:\Program Files\Android\Android Studio\jre
Useful materials
An user guide on gradlew from android.