Throw exception when attempting to launch Studio from non-GUI session

Fixes: 232769947
Test: ./studiow m
Change-Id: Ic4ec32f6b63f64c46af992280ff5f3453ea74af5
diff --git a/buildSrc/private/src/main/kotlin/androidx/build/studio/StudioTask.kt b/buildSrc/private/src/main/kotlin/androidx/build/studio/StudioTask.kt
index 8ba84b8..7af2dd9 100644
--- a/buildSrc/private/src/main/kotlin/androidx/build/studio/StudioTask.kt
+++ b/buildSrc/private/src/main/kotlin/androidx/build/studio/StudioTask.kt
@@ -46,6 +46,7 @@
     // TODO: support -y and --update-only options? Can use @Option for this
     @TaskAction
     fun studiow() {
+        validateEnvironment()
         install()
         launch()
     }
@@ -129,6 +130,22 @@
     }
 
     /**
+     * Ensure that we can launch Studio without issue.
+     */
+    private fun validateEnvironment() {
+        if (!System.getenv().containsKey("DISPLAY")) {
+            throw GradleException(
+                """
+                Studio must be run from a graphical session.
+
+                Could not read DISPLAY environment variable.  If you are using SSH into a remote
+                machine, consider using either ssh -X or switching to Chrome Remote Desktop.
+                """.trimIndent()
+            )
+        }
+    }
+
+    /**
      * Install Studio and removes any old installation files if they exist.
      */
     private fun install() {