1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.playground
18 
19 import org.gradle.api.Plugin
20 import org.gradle.api.initialization.Settings
21 
22 class PlaygroundPlugin : Plugin<Settings> {
applynull23     override fun apply(settings: Settings) {
24         settings.apply(mapOf("plugin" to "playground-develocity-conventions"))
25         settings.apply(mapOf("plugin" to "com.android.settings"))
26         settings.extensions.create("playground", PlaygroundExtension::class.java, settings)
27         validateJvm(settings)
28     }
29 
validateJvmnull30     private fun validateJvm(settings: Settings) {
31         // validate JVM version to print an understandable error if it is not set to the
32         // required value (21)
33         val jvmVersion = System.getProperty("java.vm.specification.version")
34         check(jvmVersion == "21") {
35             """
36                 AndroidX build must be invoked with JDK 21.
37                 ${
38                     if (settings.gradle.startParameter.projectProperties.containsKey(
39                             "android.injected.invoked.from.ide"
40                         )
41                     ) {
42                         """
43                             Make sure to set the Gradle JDK to JDK 21 in the project settings.
44                             File -> Settings (on Mac Android Studio -> Preferences) ->
45                             Build, Execution, Deployment -> Build Tools ->
46                             Gradle -> Gradle JDK"
47                         """
48                         } else {
49                         "Make sure your JAVA_HOME environment variable points to Java 21 JDK."
50                     }
51                 }
52                 Current version: $jvmVersion
53                 Current JAVA HOME: ${System.getProperty("java.home")}
54             """.trimIndent()
55         }
56     }
57 }
58