blob: 5948bd43524b4776ee37718bbba86351ac5808f2 [file] [log] [blame]
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// A generic settings.gradle file for all playground projects that sets it up to use public
// artifacts and the playground-build.gradle build file.
// See README.md for details
def includeProject(name, filePath) {
settings.include(name)
def file
if (filePath instanceof String) {
file = new File(rootDir, filePath)
} else {
file = filePath
}
project(name).projectDir = file
}
/**
* Initializes the playground project to use public repositories as well as other internal projects
* that cannot be found in public repositories.
*
* @param settings The reference to the settings script
* @param relativePathToRoot The relative path of the project to the root AndroidX project
*/
def setupPlayground(settings, String relativePathToRoot) {
def projectDir = settings.rootProject.getProjectDir()
def supportRoot = new File(projectDir, relativePathToRoot).getCanonicalFile()
def buildFile = new File(supportRoot, "playground-common/playground-build.gradle")
def relativePathToBuild = projectDir.toPath().relativize(buildFile.toPath()).toString()
Properties playgroundProperties = new Properties()
File propertiesFile = new File(supportRoot, "playground-common/playground.properties")
propertiesFile.withInputStream {
playgroundProperties.load(it)
}
gradle.beforeProject { project ->
// load playground properties. These are not kept in the playground projects to prevent
// AndroidX build from reading them.
playgroundProperties.each {
project.ext[it.key] = it.value
}
}
settings.ext.supportRootDir = supportRoot
settings.rootProject.buildFileName = relativePathToBuild
settings.enableFeaturePreview("VERSION_CATALOGS")
settings.dependencyResolutionManagement {
versionCatalogs {
libs {
from(files("$supportRoot/gradle/libs.versions.toml"))
}
}
}
settings.includeProject(":lint-checks", new File(supportRoot, "lint-checks"))
settings.includeProject(":lint-checks:integration-tests",
new File(supportRoot, "lint-checks/integration-tests"))
settings.includeProject(":fakeannotations", new File(supportRoot, "fakeannotations"))
settings.includeProject(":internal-testutils-common",
new File(supportRoot, "testutils/testutils-common"))
settings.includeProject(":internal-testutils-gradle-plugin",
new File(supportRoot, "testutils/testutils-gradle-plugin"))
}
/**
* A convenience method to include projects from the main AndroidX build using a filter.
*
* @param filter This filter will be called with the project name (project path in gradle).
* If filter returns true, it will be included in the build.
*/
def selectProjectsFromAndroidX(filter) {
// Multiline matcher for anything of the form:
// includeProject(name, path, ...)
// where '...' is anything except the ')' character.
def includeProjectPattern = ~/(?m)^[\n\r\s]*includeProject\("(?<name>[a-z0-9-:]*)",[\n\r\s]*"(?<path>[a-z0-9-\/]+)[^)]+\)$/
def supportSettingsFile = new File(ext.supportRootDir, "settings.gradle")
def matcher = includeProjectPattern.matcher(supportSettingsFile.text)
while (matcher.find()) {
// check if is an include project line, if so, extract project gradle path and
// file system path and call the filter
def projectGradlePath = matcher.group("name")
def projectFilePath = matcher.group("path")
if (filter(projectGradlePath)) {
settings.includeProject(projectGradlePath,
new File(ext.supportRootDir, projectFilePath))
}
}
}
/**
* Checks if a project is necessary for playground projects that involve compose.
*/
def isNeededForComposePlayground(name) {
if (name == ":compose:lint:common") return true
if (name == ":compose:lint:internal-lint-checks") return true
if (name == ":compose:test-utils") return true
if (name == ":compose:lint:common-test") return true
if (name == ":test:screenshot:screenshot") return true
return false
}
// define functions that can be called by the main settings.gradle file
ext.includeProject = this.&includeProject
ext.selectProjectsFromAndroidX = this.&selectProjectsFromAndroidX
ext.isNeededForComposePlayground = this.&isNeededForComposePlayground
ext.setupPlayground = this.&setupPlayground
// validate JVM version to print an understandable error if it is not set to the
// required value (11)
def jvmVersion = System.getProperty("java.vm.specification.version")
if (jvmVersion != "11") {
def guidance;
if (startParameter.projectProperties.containsKey("android.injected.invoked.from.ide")) {
guidance = "Make sure to set the gradle JDK to JDK 11 in the project settings." +
"(File -> Other Settings -> Default Project Structure)"
} else {
guidance = "Make sure your JAVA_HOME environment variable points to Java 11 JDK."
}
throw new IllegalStateException("""
AndroidX build must be invoked with JDK 11.
$guidance
Current version: $jvmVersion
Current JAVA HOME: ${System.getProperty("java.home")}""".stripIndent());
}
// allow public repositories
System.setProperty("ALLOW_PUBLIC_REPOS", "true")
// specify out dir location
System.setProperty("CHECKOUT_ROOT", "${buildscript.sourceFile.parent}/..")