Add SystemUI build flag for Compose code (1/3)
This CL adds a Soong build flag that enables Compose code when building
SystemUI, or anything depending on SystemUI-core. The flag is disabled
by default, and will be first used to measure the impact of Compose on
our apk and system health. It will be turned on for one/many/all targets
once agreed with the Android System Health & Performance team.
To turn this flag on, one simply have to
`export SYSTEMUI_USE_COMPOSE=true` before compiling.
This flag is implemented such that:
- When it is enabled, SystemUI-core depends on the SystemUIComposeCore
library and compiles the source files of SystemUIComposeFeatures. In
an ideal world, SystemUI-core would also just depend on
SystemUIComposeFeatures instead of recompiling its sources, but given
that the code in SystemUIComposeFeatures depends on code in
SystemUI-core, this would lead to a cyclic dependency. Therefore,
when this flag is enabled, we compile those files together.
SystemUIComposeFeatures also contains a ComposeFacade object that is
the bridge between SystemUI-core code and SystemUIComposeFeatures
code.
- When it is disabled, we only add another ComposeFacade object with
the same API as the ComposeFacade object in SystemUIComposeFeatures,
which throws when its functions are called (except for
ComposeFacade#isComposeAvailable(), which returns false).
This way, any code in SystemUI-core can check
ComposeFacade#isComposeAvailable() first then call other functions to
indirectly access Compose code. See http://ag/20759151 for an example.
Bug: 242304109
Test: Builds
Change-Id: Ibd895a73041c163816e779273ec0b6588aabef76
diff --git a/packages/SystemUI/Android.bp b/packages/SystemUI/Android.bp
index 25fb24a..9515aa5 100644
--- a/packages/SystemUI/Android.bp
+++ b/packages/SystemUI/Android.bp
@@ -31,6 +31,51 @@
],
}
+// Opt-in configuration for code depending on Jetpack Compose.
+soong_config_module_type {
+ name: "systemui_compose_java_defaults",
+ module_type: "java_defaults",
+ config_namespace: "ANDROID",
+ bool_variables: ["SYSTEMUI_USE_COMPOSE"],
+ properties: [
+ "srcs",
+ "static_libs",
+ ],
+}
+
+systemui_compose_java_defaults {
+ name: "SystemUI_compose_defaults",
+ soong_config_variables: {
+ SYSTEMUI_USE_COMPOSE: {
+ // Because files in compose/features/ depend on SystemUI
+ // code, we compile those files when compiling SystemUI-core.
+ // We also compile the ComposeFacade in
+ // compose/facade/enabled/.
+ srcs: [
+ "compose/features/src/**/*.kt",
+ "compose/facade/enabled/src/**/*.kt",
+ ],
+
+ // The dependencies needed by SystemUIComposeFeatures,
+ // except for SystemUI-core.
+ // Copied from compose/features/Android.bp.
+ static_libs: [
+ "SystemUIComposeCore",
+
+ "androidx.compose.runtime_runtime",
+ "androidx.compose.material3_material3",
+ ],
+
+ // By default, Compose is disabled and we compile the ComposeFacade
+ // in compose/facade/disabled/.
+ conditions_default: {
+ srcs: ["compose/facade/disabled/src/**/*.kt"],
+ static_libs: [],
+ },
+ },
+ },
+}
+
java_library {
name: "SystemUI-proto",
@@ -68,6 +113,9 @@
android_library {
name: "SystemUI-core",
+ defaults: [
+ "SystemUI_compose_defaults",
+ ],
srcs: [
"src/**/*.kt",
"src/**/*.java",
@@ -227,6 +275,9 @@
android_library {
name: "SystemUI-tests",
+ defaults: [
+ "SystemUI_compose_defaults",
+ ],
manifest: "tests/AndroidManifest-base.xml",
additional_manifests: ["tests/AndroidManifest.xml"],
srcs: [
diff --git a/packages/SystemUI/compose/facade/disabled/src/com/android/systemui/compose/ComposeFacade.kt b/packages/SystemUI/compose/facade/disabled/src/com/android/systemui/compose/ComposeFacade.kt
new file mode 100644
index 0000000..b335907
--- /dev/null
+++ b/packages/SystemUI/compose/facade/disabled/src/com/android/systemui/compose/ComposeFacade.kt
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2022 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.
+ *
+ */
+
+package com.android.systemui.compose
+
+/** The Compose facade, when Compose is *not* available. */
+object ComposeFacade : BaseComposeFacade {
+ override fun isComposeAvailable(): Boolean = false
+
+ private fun throwComposeUnavailableError() {
+ error(
+ "Compose is not available. Make sure to check isComposeAvailable() before calling any" +
+ " other function on ComposeFacade."
+ )
+ }
+}
diff --git a/packages/SystemUI/compose/facade/enabled/src/com/android/systemui/compose/ComposeFacade.kt b/packages/SystemUI/compose/facade/enabled/src/com/android/systemui/compose/ComposeFacade.kt
new file mode 100644
index 0000000..d950057
--- /dev/null
+++ b/packages/SystemUI/compose/facade/enabled/src/com/android/systemui/compose/ComposeFacade.kt
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2022 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.
+ */
+
+package com.android.systemui.compose
+
+/** The Compose facade, when Compose is available. */
+object ComposeFacade : BaseComposeFacade {
+ override fun isComposeAvailable(): Boolean = true
+}
diff --git a/packages/SystemUI/src/com/android/systemui/compose/BaseComposeFacade.kt b/packages/SystemUI/src/com/android/systemui/compose/BaseComposeFacade.kt
new file mode 100644
index 0000000..859867a
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/compose/BaseComposeFacade.kt
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2022 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.
+ *
+ */
+
+package com.android.systemui.compose
+
+/**
+ * A facade to interact with Compose, when it is available.
+ *
+ * You should access this facade by calling the static methods on
+ * [com.android.systemui.compose.ComposeFacade] directly.
+ */
+interface BaseComposeFacade {
+ /**
+ * Whether Compose is currently available. This function should be checked before calling any
+ * other functions on this facade.
+ *
+ * This value will never change at runtime.
+ */
+ fun isComposeAvailable(): Boolean
+}