Android Studio build java.exe suspended second time around

Viewed 665

Using Android Studio 3.1.4 on Windows 64 Pro. When running any app, including a sample project, Android Studio gets stuck the second time around on build/run cycle.

In Windows task manager I see java.exe suspended where one of the process threads it says is waiting on network i/o.

I have to kill java.exe from the Windows task manager every time I build/run an app.

I tried all of the following with no help:

  • deleted .gradle in my user folder
  • deleted .AndroidStudio3.1 folder in my user folder
  • invalidate caches/restart
  • use offline gradle option
  • disable instant run

Any ideas why this is happening? Seems that the build process is deadlocking on itself. This only happens on the second build/run.

3 Answers

I am encountering the same issue and have tried re-installs, differing JVMs, and countless other tactics. As a temporary workaround I have modified the Gradle build to create a flag file which a separate Powershell script then polls for and if found kills any running Open JDK process. This is a kludge without doubt but helps keep me sane.

Update build.gradle(Module:app) to include the following:

android {
    compileSdkVersion 28
    ...
    }
    android.applicationVariants.all {
        ant.touch(file:"D:/Build.flg")
    }
}

Then run the following Powershell script

do {
    Write-Host "Checking for D:\Build.flg"
    if (Test-Path "D:\Build.flg") {
        Remove-Item "D:\Build.flg"
        Write-Host "Locating Java processes"
        $JavaProcessos = Get-Process java | select Id, processname, Description
        Foreach($Process in $JavaProcessos)
        {
            if($Process.Description -eq "OpenJDK Platform binary") {
                Write-Host "Stopping process " $Process.Id $Process.Description
                Start-Sleep -s 30
                Stop-Process $Process.Id
            }
        }   
    }
    Start-Sleep -s 10
}
While ($true)

Not pretty but functional.

I could not call the script directly from the build as it kills the JVM that Gradle itself is using at the time - thus the delay and flag approach.

I welcome suggestions to improve, until the root cause is addressed.

In the process of trying to fix the Android Studio alternate build hang, I damaged my Eclipse / Python install. Surprisingly, fixing Eclipse also seems to have eliminated the Android build problem. Came down to setting the right Java environment variables.

Press the āŠž Win key and search for [Edit the system environment variables].

Then add / update the following based on the location of your JDK installation.

CLASSPATH=.;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_191
JDK_HOME=%JAVA_HOME%
JRE_HOME=%JAVA_HOME%\jre

Finally, add the Java bin to the start of your path

Path=%JAVA_HOME%\bin;...

After this my Android Studio builds no longer hang and Eclipse is fully functional.

I've used Sysinternals ProcessExplorer. In the context menu you can just RESUME the java process and the build will work

Related