Gradle Daemon Connection Error: Could Not Connect to Daemon (Gradle 8.7, OpenJDK 17)

Hello,

Description:

I am working on a Flutter project on Window 10, using Docker Desktop, and using act-cli to build/deploy the APK. In practice, act-cli create a docker container and clone the source code then run gradle to build the apk (from the container itself) . However, when building the APK using Gradle, there is a serious issue related to the Gradle Daemon.

I provided a sample .workflow file below, the code sample is created by default template of Flutter

name: Bug

on:
  workflow_dispatch:


jobs:
  build:
    name: Build Flutter Project
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/cirruslabs/flutter:3.24.5
    steps:

        - name: Checkout flutter-hello-world
          uses: actions/checkout@v4
        - name: Flutter Doctor
          run: flutter doctor
        - name: Build SampleApplication
          run: flutter build apk --release -v

Then run the workflow by

act -W .github/workflows/workflow_file.yml

Observed Behavior:

Gradle fails to connect to the daemon, causing the APK build to terminate with an error.

| [  +10 ms] FAILURE: Build failed with an exception.
| [   +1 ms] * What went wrong:
| [        ] Could not connect to the Gradle daemon.
| [        ] Daemon uid: 5a0984cc-3ddf-479e-adef-4509f9c189e9 with diagnostics:
| [        ] Daemon pid: 293
| [        ]   log file: /root/.gradle/daemon/8.7/daemon-293.out.log
| [        ] ----- Last  20 lines from daemon log file - daemon-293.out.log -----
| [        ] 2024-12-15T09:20:51.848+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding IP addresses for network interface lo
| [        ] 2024-12-15T09:20:51.848+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Is this a loopback 
interface? true
| [        ] 2024-12-15T09:20:51.848+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding loopback address /0:0:0:0:0:0:0:1%lo
| [        ] 2024-12-15T09:20:51.848+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding loopback address /127.0.0.1
| [        ] 2024-12-15T09:20:51.850+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.TcpIncomingConnector] Listening on [1fba37b4-3d2d-409e-b3c1-9bcb5dd6ab18 port:38649, addresses:[localhost/127.0.0.1]].
| [        ] 2024-12-15T09:20:51.858+0000 [DEBUG] [org.gradle.launcher.daemon.server.Daemon] Daemon starting at: Sun Dec 15 09:20:51 GMT 2024, with address: [1fba37b4-3d2d-409e-b3c1-9bcb5dd6ab18 port:38649, addresses:[localhost/127.0.0.1]]
| [        ] 2024-12-15T09:20:51.858+0000 [INFO] [org.gradle.launcher.daemon.server.DaemonRegistryUpdater] Advertising the daemon address to the clients: [1fba37b4-3d2d-409e-b3c1-9bcb5dd6ab18 port:38649, addresses:[localhost/127.0.0.1]]
| [        ] 2024-12-15T09:20:51.859+0000 [DEBUG] [org.gradle.launcher.daemon.server.DaemonRegistryUpdater] Advertised daemon 
context: DefaultDaemonContext[uid=5a0984cc-3ddf-479e-adef-4509f9c189e9,javaHome=/usr/lib/jvm/java-17-openjdk-amd64,daemonRegistryDir=/root/.gradle/daemon,pid=293,idleTimeout=120000,priority=NORMAL,applyInstrumentationAgent=true,daemonOpts=--add-opens=java.base/java.util=ALL-UNNAMED,--add-opens=java.base/java.lang=ALL-UNNAMED,--add-opens=java.base/java.lang.invoke=ALL-UNNAMED,--add-opens=java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens=java.base/java.nio.charset=ALL-UNNAMED,--add-opens=java.base/java.net=ALL-UNNAMED,--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED,-Xmx4608M,-Dfile.encoding=UTF-8,-Duser.country=US,-Duser.language=en,-Duser.variant]
| [        ] 2024-12-15T09:20:51.859+0000 [DEBUG] [org.gradle.launcher.daemon.registry.PersistentDaemonRegistry] Storing daemon address: [1fba37b4-3d2d-409e-b3c1-9bcb5dd6ab18 port:38649, addresses:[localhost/127.0.0.1]], context: DefaultDaemonContext[uid=5a0984cc-3ddf-479e-adef-4509f9c189e9,javaHome=/usr/lib/jvm/java-17-openjdk-amd64,daemonRegistryDir=/root/.gradle/daemon,pid=293,idleTimeout=120000,priority=NORMAL,applyInstrumentationAgent=true,daemonOpts=--add-opens=java.base/java.util=ALL-UNNAMED,--add-opens=java.base/java.lang=ALL-UNNAMED,--add-opens=java.base/java.lang.invoke=ALL-UNNAMED,--add-opens=java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens=java.base/java.nio.charset=ALL-UNNAMED,--add-opens=java.base/java.net=ALL-UNNAMED,--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED,-Xmx4608M,-Dfile.encoding=UTF-8,-Duser.country=US,-Duser.language=en,-Duser.variant]
| [        ] 2024-12-15T09:20:51.865+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire exclusive lock on daemon addresses registry.
| [        ] 2024-12-15T09:20:51.868+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
| [   +1 ms] 2024-12-15T09:20:51.869+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon 
addresses registry.
| [        ] 2024-12-15T09:20:51.870+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire exclusive lock on daemon addresses registry.
| [        ] 2024-12-15T09:20:51.870+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
| [        ] 2024-12-15T09:20:51.872+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon 
addresses registry.
| [        ] 2024-12-15T09:20:51.872+0000 [LIFECYCLE] [org.gradle.launcher.daemon.server.Daemon] Daemon server started.       
| [        ] 2024-12-15T09:20:51.873+0000 [DEBUG] [org.gradle.launcher.daemon.bootstrap.DaemonStartupCommunication] Completed 
writing the daemon greeting. Closing streams...
| [        ] 2024-12-15T09:20:51.876+0000 [DEBUG] [org.gradle.launcher.daemon.server.Daemon] stopOnExpiration() called on daemon
| [        ] 2024-12-15T09:20:51.883+0000 [DEBUG] [org.gradle.launcher.daemon.server.Daemon] awaitExpiration() called on daemon
| [        ] 2024-12-15T09:20:51.884+0000 [DEBUG] [org.gradle.launcher.daemon.server.DaemonStateCoordinator] daemon is running. Sleeping until state changes.
| [        ] ----- End of the daemon log -----
| [        ] * Try:
| [        ] > Run with --debug option to get more log output.
| [        ] > Run with --scan to get full insights.
| [        ] > Get more help at https://help.gradle.org.
| [        ] * Exception is:
| [        ] org.gradle.launcher.daemon.client.DaemonConnectionException: Could not connect to the Gradle daemon.
| [        ] Daemon uid: 5a0984cc-3ddf-479e-adef-4509f9c189e9 with diagnostics:
| [        ] Daemon pid: 293
| [        ]   log file: /root/.gradle/daemon/8.7/daemon-293.out.log
| [        ] ----- Last  20 lines from daemon log file - daemon-293.out.log -----
| [        ] 2024-12-15T09:20:51.848+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding IP addresses for network interface lo
| [        ] 2024-12-15T09:20:51.848+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Is this a loopback 
interface? true
| [        ] 2024-12-15T09:20:51.848+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding loopback address /0:0:0:0:0:0:0:1%lo
| [        ] 2024-12-15T09:20:51.848+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding loopback address /127.0.0.1
| [        ] 2024-12-15T09:20:51.850+0000 [DEBUG] [org.gradle.internal.remote.internal.inet.TcpIncomingConnector] Listening on [1fba37b4-3d2d-409e-b3c1-9bcb5dd6ab18 port:38649, addresses:[localhost/127.0.0.1]].
| [        ] 2024-12-15T09:20:51.858+0000 [DEBUG] [org.gradle.launcher.daemon.server.Daemon] Daemon starting at: Sun Dec 15 09:20:51 GMT 2024, with address: [1fba37b4-3d2d-409e-b3c1-9bcb5dd6ab18 port:38649, addresses:[localhost/127.0.0.1]]
| [        ] 2024-12-15T09:20:51.858+0000 [INFO] [org.gradle.launcher.daemon.server.DaemonRegistryUpdater] Advertising the daemon address to the clients: [1fba37b4-3d2d-409e-b3c1-9bcb5dd6ab18 port:38649, addresses:[localhost/127.0.0.1]]
| [        ] 2024-12-15T09:20:51.859+0000 [DEBUG] [org.gradle.launcher.daemon.server.DaemonRegistryUpdater] Advertised daemon 
context: DefaultDaemonContext[uid=5a0984cc-3ddf-479e-adef-4509f9c189e9,javaHome=/usr/lib/jvm/java-17-openjdk-amd64,daemonRegistryDir=/root/.gradle/daemon,pid=293,idleTimeout=120000,priority=NORMAL,applyInstrumentationAgent=true,daemonOpts=--add-opens=java.base/java.util=ALL-UNNAMED,--add-opens=java.base/java.lang=ALL-UNNAMED,--add-opens=java.base/java.lang.invoke=ALL-UNNAMED,--add-opens=java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens=java.base/java.nio.charset=ALL-UNNAMED,--add-opens=java.base/java.net=ALL-UNNAMED,--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED,-Xmx4608M,-Dfile.encoding=UTF-8,-Duser.country=US,-Duser.language=en,-Duser.variant]
| [        ] 2024-12-15T09:20:51.859+0000 [DEBUG] [org.gradle.launcher.daemon.registry.PersistentDaemonRegistry] Storing daemon address: [1fba37b4-3d2d-409e-b3c1-9bcb5dd6ab18 port:38649, addresses:[localhost/127.0.0.1]], context: DefaultDaemonContext[uid=5a0984cc-3ddf-479e-adef-4509f9c189e9,javaHome=/usr/lib/jvm/java-17-openjdk-amd64,daemonRegistryDir=/root/.gradle/daemon,pid=293,idleTimeout=120000,priority=NORMAL,applyInstrumentationAgent=true,daemonOpts=--add-opens=java.base/java.util=ALL-UNNAMED,--add-opens=java.base/java.lang=ALL-UNNAMED,--add-opens=java.base/java.lang.invoke=ALL-UNNAMED,--add-opens=java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens=java.base/java.nio.charset=ALL-UNNAMED,--add-opens=java.base/java.net=ALL-UNNAMED,--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED,-Xmx4608M,-Dfile.encoding=UTF-8,-Duser.country=US,-Duser.language=en,-Duser.variant]
| [        ] 2024-12-15T09:20:51.865+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire exclusive lock on daemon addresses registry.
| [        ] 2024-12-15T09:20:51.868+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
| [        ] 2024-12-15T09:20:51.869+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon 
addresses registry.
| [        ] 2024-12-15T09:20:51.870+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire exclusive lock on daemon addresses registry.
| [        ] 2024-12-15T09:20:51.870+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
| [        ] 2024-12-15T09:20:51.872+0000 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon 
addresses registry.
| [        ] 2024-12-15T09:20:51.872+0000 [LIFECYCLE] [org.gradle.launcher.daemon.server.Daemon] Daemon server started.       
| [        ] 2024-12-15T09:20:51.873+0000 [DEBUG] [org.gradle.launcher.daemon.bootstrap.DaemonStartupCommunication] Completed 
writing the daemon greeting. Closing streams...
| [        ] 2024-12-15T09:20:51.876+0000 [DEBUG] [org.gradle.launcher.daemon.server.Daemon] stopOnExpiration() called on daemon
| [        ] 2024-12-15T09:20:51.883+0000 [DEBUG] [org.gradle.launcher.daemon.server.Daemon] awaitExpiration() called on daemon
| [        ] 2024-12-15T09:20:51.884+0000 [DEBUG] [org.gradle.launcher.daemon.server.DaemonStateCoordinator] daemon is running. Sleeping until state changes.
| [        ] ----- End of the daemon log -----
| [        ]    at org.gradle.launcher.daemon.client.DefaultDaemonConnector.connectToDaemonWithId(DefaultDaemonConnector.java:262)
| [        ]    at org.gradle.launcher.daemon.client.DefaultDaemonConnector.doStartDaemon(DefaultDaemonConnector.java:227)    
| [        ]    at org.gradle.launcher.daemon.client.DefaultDaemonConnector.startSingleUseDaemon(DefaultDaemonConnector.java:247)
| [        ]    at org.gradle.launcher.daemon.client.SingleUseDaemonClient.execute(SingleUseDaemonClient.java:51)
| [        ]    at org.gradle.launcher.daemon.client.SingleUseDaemonClient.execute(SingleUseDaemonClient.java:37)
| [        ]    at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:57)
| [        ]    at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:167)
| [        ]    at org.gradle.launcher.cli.DefaultCommandLineActionFactory$ParseAndBuildAction.execute(DefaultCommandLineActionFactory.java:259)
| [        ]    at org.gradle.launcher.cli.DefaultCommandLineActionFactory$ParseAndBuildAction.execute(DefaultCommandLineActionFactory.java:230)
| [        ]    at org.gradle.launcher.cli.DebugLoggerWarningAction.execute(DebugLoggerWarningAction.java:74)
| [        ]    at org.gradle.launcher.cli.DebugLoggerWarningAction.execute(DebugLoggerWarningAction.java:30)
| [        ]    at org.gradle.launcher.cli.WelcomeMessageAction.execute(WelcomeMessageAction.java:96)
| [        ]    at org.gradle.launcher.cli.WelcomeMessageAction.execute(WelcomeMessageAction.java:40)
| [        ]    at org.gradle.launcher.cli.NativeServicesInitializingAction.execute(NativeServicesInitializingAction.java:44) 
| [        ]    at org.gradle.launcher.cli.NativeServicesInitializingAction.execute(NativeServicesInitializingAction.java:26) 
| [        ]    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:41)
| [        ]    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:26)
| [        ]    at org.gradle.launcher.cli.DefaultCommandLineActionFactory$WithLogging.execute(DefaultCommandLineActionFactory.java:361)
| [        ]    at org.gradle.launcher.Main.doAction(Main.java:35)
| [        ]    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:50)
| [        ]    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| [        ]    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
| [        ]    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   
| [        ]    at java.base/java.lang.reflect.Method.invoke(Method.java:569)
| [        ]    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:60)
| [        ]    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:37)
| [        ]    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| [        ]    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
| [        ]    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   
| [        ]    at java.base/java.lang.reflect.Method.invoke(Method.java:569)
| [        ]    at org.gradle.launcher.GradleMain.main(GradleMain.java:34)
| [        ]    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| [        ]    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
| [        ]    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   
| [        ]    at java.base/java.lang.reflect.Method.invoke(Method.java:569)
| [        ]    at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:30)
| [        ]    at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
| [        ]    at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
| [        ] Caused by: org.gradle.internal.remote.internal.ConnectException: Could not connect to server [1fba37b4-3d2d-409e-b3c1-9bcb5dd6ab18 port:38649, addresses:[/127.0.0.1]]. Tried addresses: [/127.0.0.1].
| [        ]    at org.gradle.internal.remote.internal.inet.TcpOutgoingConnector.connect(TcpOutgoingConnector.java:67)        
| [        ]    at org.gradle.launcher.daemon.client.DefaultDaemonConnector.connectToDaemon(DefaultDaemonConnector.java:274)  
| [        ]    at org.gradle.launcher.daemon.client.DefaultDaemonConnector.connectToDaemonWithId(DefaultDaemonConnector.java:260)
| [        ]    ... 37 more
| [        ] Caused by: java.net.ConnectException: Connection refused
| [        ]    at java.base/sun.nio.ch.Net.pollConnect(Native Method)
| [        ]    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
| [        ]    at java.base/sun.nio.ch.SocketChannelImpl.finishTimedConnect(SocketChannelImpl.java:1148)
| [        ]    at java.base/sun.nio.ch.SocketChannelImpl.blockingConnect(SocketChannelImpl.java:1183)
| [        ]    at java.base/sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:98)
| [        ]    at org.gradle.internal.remote.internal.inet.TcpOutgoingConnector.tryConnect(TcpOutgoingConnector.java:81)     
| [        ]    at org.gradle.internal.remote.internal.inet.TcpOutgoingConnector.connect(TcpOutgoingConnector.java:54)        
| [        ]    ... 39 more
### Expected Behavior:

Gradle should connect to the daemon without issues, allowing the APK build to proceed successfully.


### Additional Context:
Here is the gradle.properties I have

android/gradle.properties
---
org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=2G -XX:+HeapDumpOnOutOfMemoryError
android.useAndroidX=true
android.enableJetifier=true
org.gradle.daemon=false
org.gradle.jvmargs=-Xmx4608M

android/app/gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl = https\://services.gradle.org/distributions/gradle-8.7-bin.zip
org.gradle.daemon=false
org.gradle.parallel=false
org.gradle.jvmargs=-Xmx4608M

Workaround:

Turn-off host network in the Docker Desktop seem to be the only solution for this problem albeit I don’t understand why?

Let me know if you’d like to refine this further.

The Gradle CLI talks with the Gradle Daemon (and the daemon with worker processes) via localhost connections. If the localhost connection does not work, Gradle cannot work.