読者です 読者をやめる 読者になる 読者になる

Intellij Ideaのgradleタスク実行でJavaHomeが違う。というエラー

トラブル事象

Intellij Idea(以降Idea)でgradelのRun設定を行い、タスクを実行しようとしたところ、以下のようなエラーとなった。

10:13:46: Executing external task 'gen'...
The newly created daemon process has a different context than expected.
It won't be possible to reconnect to this daemon. Context mismatch: 
Java home is different.
Wanted: DefaultDaemonContext[uid=null,javaHome=/Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home,daemonRegistryDir=/Users/xxxxx/.gradle/daemon,pid=9836,idleTimeout=null,daemonOpts=-XX:MaxPermSize=256m,-XX:+HeapDumpOnOutOfMemoryError,-Xmx1024m,-Dfile.encoding=UTF-8,-Duser.country=JP,-Duser.language=ja,-Duser.variant]
Actual: DefaultDaemonContext[uid=b46ae433-167f-4edf-83aa-8fcc74371dc1,javaHome=/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home,daemonRegistryDir=/Users/xxxxx/.gradle/daemon,pid=15833,idleTimeout=60000,daemonOpts=-XX:MaxPermSize=256m,-XX:+HeapDumpOnOutOfMemoryError,-Xmx1024m,-Dfile.encoding=UTF-8,-Duser.country=JP,-Duser.language=ja,-Duser.variant]

10:13:47: External task execution finished 'gen'.

解決策

gradle.propertiesファイルに以下のようにjava_homeを設定する。

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home
10:24:33: Executing external task 'gen'...
:gen

BUILD SUCCESSFUL

Total time: 3.411 secs
10:24:36: External task execution finished 'gen'.