1import kotlin.Triple
2import org.apache.tools.ant.taskdefs.condition.Os
3
4plugins {
5    id("org.jetbrains.gradle.plugin.idea-ext") version "1.1.7"
6}
7
8final boolean IS_ARM_MAC = Os.isFamily(Os.FAMILY_MAC) &&
9        System.getProperty("os.arch") == "aarch64"
10
11allprojects {
12    ext {
13        ANDROID_TOP = "${new File("${rootDir}", ANDROID_RELATIVE_TOP).canonicalPath}"
14        SYS_UI_DIR = "${ANDROID_TOP}/frameworks/base/packages/SystemUI"
15        SETTINGS_DIR = "${ANDROID_TOP}/frameworks/base/packages/SettingsLib"
16        GOOGLE_SYS_UI_DIR = "${ANDROID_TOP}/vendor/unbundled_google/packages/SystemUIGoogle/"
17        MANAGED_PROVISIONING_DIR = "${ANDROID_TOP}/packages/apps/ManagedProvisioning/"
18        // We need to explicitly request x86_64 binaries on M1 Macs since protoc compiler
19        // for ARM doesn't exist for current protobuf version.
20        PROTO_ARCH_SUFFIX = IS_ARM_MAC ? ":osx-x86_64" : ""
21
22        // Whether we should compile SystemUI with Compose enabled or not.
23        USE_COMPOSE = true
24    }
25}
26
27final String GRADLE_BUILD_ROOT = "${ANDROID_TOP}/out/gradle/build"
28buildDir = "${GRADLE_BUILD_ROOT}/${rootProject.name}/build"
29
30gradle.beforeProject {
31    rootProject.subprojects {
32        buildDir = "${GRADLE_BUILD_ROOT}/${rootProject.name}/${project.name}/build"
33    }
34}
35
36tasks.register('updateSdkSources', SdkSourceUpdaterTask) {
37    androidRoot = new File("${ANDROID_TOP}")
38}
39
40idea {
41    project {
42        settings {
43            taskTriggers {
44                afterSync tasks.named("updateSdkSources")
45            }
46        }
47    }
48}
49
50final Map<String, Triple> LIB_VERSION_MAP = new RepoDependencyMapper()
51        .mapPath("${ANDROID_TOP}/prebuilts/sdk/current/androidx/m2repository")
52        .mapPath("${ANDROID_TOP}/prebuilts/sdk/current/androidx-legacy/m2repository")
53        .mapPath("${ANDROID_TOP}/prebuilts/sdk/current/extras/material-design-x")
54        .mapPath("${ANDROID_TOP}/prebuilts/misc/common/androidx-test")
55        .getMap()
56
57allprojects {
58    configurations.configureEach {
59        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
60            // Override any transitive dependency to also use the local version
61            Triple targetOverride = LIB_VERSION_MAP.get(details.requested.module.toString())
62            if (targetOverride != null) {
63                if (targetOverride.second != null) {
64                    details.useTarget group: targetOverride.first, name: targetOverride.second, version: targetOverride.third
65                } else {
66                    details.useVersion(targetOverride.third)
67                }
68            }
69        }
70    }
71}
72
73Properties properties = new Properties()
74properties.load(project.rootProject.file('studiow.properties').newDataInputStream())
75// String like UdcDevForTree456b
76rootProject.ext.set("compileSdkPreviewString", properties.getProperty('compile.sdk.preview'))
77// String like android-UdcDevForTree456b
78rootProject.ext.set("compileSdkVersionString", properties.getProperty('compile.sdk.version'))
79
80// Check env var to see if the gradle script was launched using the studiow script. Launching
81// Android Studio and then opening the Gradle project afterwards IS UNSUPPORTED. Android Studio
82// needs to be lunched using studiow so that it can validate settings and update the build
83// environment.
84if (System.getenv('STUDIO_LAUNCHED_WITH_WRAPPER') == null) {
85    throw new Exception("Android Studio for SystemUI must be launched using " +
86            "the studiow script found in \$ANDROID_BUILD_TOP/" +
87            "vendor/unbundled_google/packages/SystemUIGoogle/studio-dev/studiow.")
88}
89
90subprojects {
91    afterEvaluate { project ->
92        if (project.hasProperty("android")) {
93            android {
94                // Settings compileSdkVersion also sets compileSdkPreview. No need to set both
95                compileSdkVersion(compileSdkVersionString)
96                buildToolsVersion BUILD_TOOLS_VERSION
97                defaultConfig {
98                    minSdkPreview TARGET_SDK
99                }
100                compileOptions {
101                    sourceCompatibility JavaVersion.VERSION_17
102                    targetCompatibility JavaVersion.VERSION_17
103                }
104
105                if (android.hasProperty("kotlinOptions")) {
106                    kotlinOptions {
107                        jvmTarget = "17"
108                        freeCompilerArgs = ["-Xjvm-default=all"]
109                    }
110                }
111
112                // Disable abortOnError everywhere. Otherwise, the :build task will fail for 100%
113                // of our projects
114                lint {
115                    abortOnError false
116                }
117            }
118        }
119
120        /**
121         * TODO(b/269759002): Replace this workaround with DSL like "disableCompileSdk" if available
122         * AndroidX uses the same workaround in their internal build by setting androidx.useMaxDepVersions
123         *
124         * Workaround to fix the following error:
125         *
126         * 3 issues were found when checking AAR metadata:
127         *
128         *   1.  Dependency 'androidx.window:window:1.1.0-alpha06' requires libraries and applications that
129         *       depend on it to compile against codename "UpsideDownCake" of the
130         *       Android APIs.
131         *
132         *       :ComposeGallery is currently compiled against android-UpsideDownCakeForSysUiStudioRev456b2dc4.
133         *
134         *       Recommended action: Use a different version of dependency 'androidx.window:window:1.1.0-alpha06',
135         *       or set compileSdkPreview to "UpsideDownCake" in your build.gradle
136         *       file if you intend to experiment with that preview SDK.
137         */
138//        tasks.withType(CheckAarMetadataTask).configureEach({ task ->
139//            task.enabled = false
140//        })
141    }
142}
143
144tasks.named('wrapper') {
145    // Delete gradlew.bat because Windows builds are not supported
146    doLast {
147        delete "${projectDir}/gradlew.bat"
148    }
149}
150