Search code examples
javaandroidandroid-studiojava-9processhandle

Cannot resolve symbol 'ProcessHandle' on Android Studio


For some strange reason, I cannot make ProcessHandle be resolved in my Android Studio environment:

Android Studio Koala | 2024.1.1 Patch 1
Build #AI-241.18034.62.2411.12071903, built on July 11, 2024
Runtime version: 17.0.11+0--11852314 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 5120M
Cores: 8
Registry:
  ide.instant.shutdown=false
Non-Bundled Plugins:
  PlantUML integration (7.10.1-IJ2023.2)
  com.intellij.marketplace (241.17890.43)
  Lombook Plugin (241.18034.62)
  com.cmgapps.intellij.proguard-retrace-unscambler (1.9.1)
  Docker (241.18034.82)
  SequenceDiagram (3.0.12)
  com.kn.diagrams.generator.generator (2022.2.0)

As soon as I add a reference to ProcessHandle (along with the required import statement):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.lang.ProcessHandle;

I get this error:

Cannot resolve symbol 'ProcessHandle'

"Cannot resolve symbol ProcessHandle" as viewed on IDE

Knowing that 'ProcessHandle' has only been introduced to Java since Java 9 and that I had a Java 8 installed on my system, I first resorted to uninstalling any remnant of Java 8, hoping that this would remove any conflict which I haven't been able to spot by inspecting Settings and Project Structure.

That didn't help.

So, I started trying all sorts of steps based on my SO research (e.g. java.lang.ProcessHandle - compilation error , Intellij Cannot resolve symbol on import ) :

  1. Verified Java Version in Android Studio (AS): Ensured that Android Studio is correctly configured to use Java 17 (via Project Structure settings).

  2. I even made sure the %JAVA_HOME% points to the same location as AS Settings: C:\Program Files\Android\Android Studio\jbr: enter image description here

  3. Checked Module Settings: I made sure that the module settings in my project are set to use Java 17 as well.

Module Settings using Java 17

  1. I even placed in the code System.out.println("JUnit5's Java Runtime Version: " + System.getProperty("java.version")); that actually shows in runtime that actual Java version used is 17.
  2. Invalidated Caches and Restart.

None of these measures helped. Any idea what I could have missed?


Solution

  • The Android documentation for the java.lang package does not include ProcessHandle as an interface in that package. ProcessHandle is simply not available on Android. See also this section which says:

    Which Java APIs can I use in my Java or Kotlin source code?

    An Android application can use some of the APIs defined in a JDK, but not all of them. The Android SDK defines implementations of many Java library functions as part of its available APIs.

    Wikipedia describes some more differences.


    Presumably, ProcessHandle is not available because Android apps are sandboxed and are designed to only have limited access to the OS. Android has its own APIs for dealing with processes, such as android.os.Process and android.app.ActivityManager.RunningAppProcessInfo.