aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2024-06-05 00:08:03 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2024-06-05 00:08:03 +0000
commit86a54e23482b320e083f9a337814c7fe4f78e9a3 (patch)
tree99a45bd001c0a08b0e65d1d732fb8105c8c2a54e
parent85322893430c83b94256ba578d0fcb9c7971ec57 (diff)
parentcaeca0405fe14f0f164348e40b0138e132311ef1 (diff)
downloadsupport-androidx-lifecycle-release.tar.gz
Merge "Merge cherrypicks of ['android-review.googlesource.com/3114939'] into androidx-lifecycle-release." into androidx-lifecycle-releaseandroidx-lifecycle-release
-rw-r--r--lifecycle/lifecycle-runtime-compose/build.gradle4
-rw-r--r--lifecycle/lifecycle-runtime-compose/proguard-rules.pro11
-rw-r--r--lifecycle/lifecycle-runtime-compose/src/androidMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.android.kt65
-rw-r--r--lifecycle/lifecycle-runtime-compose/src/commonMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.kt10
-rw-r--r--lifecycle/lifecycle-runtime-compose/src/desktopMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.desktop.kt28
5 files changed, 111 insertions, 7 deletions
diff --git a/lifecycle/lifecycle-runtime-compose/build.gradle b/lifecycle/lifecycle-runtime-compose/build.gradle
index a327bfa3484..4d38706226c 100644
--- a/lifecycle/lifecycle-runtime-compose/build.gradle
+++ b/lifecycle/lifecycle-runtime-compose/build.gradle
@@ -79,5 +79,9 @@ androidx {
}
android {
+ buildTypes.configureEach {
+ consumerProguardFiles "proguard-rules.pro"
+ }
+
namespace "androidx.lifecycle.runtime.compose"
}
diff --git a/lifecycle/lifecycle-runtime-compose/proguard-rules.pro b/lifecycle/lifecycle-runtime-compose/proguard-rules.pro
new file mode 100644
index 00000000000..9f44c4b6e77
--- /dev/null
+++ b/lifecycle/lifecycle-runtime-compose/proguard-rules.pro
@@ -0,0 +1,11 @@
+# `androidx.lifecycle.compose.LocalLifecycleOwner` will reflectively lookup for
+# `androidx.compose.ui.platform.LocalLifecycleOwner` to ensure backward compatibility
+# when using Lifecycle 2.8+ with Compose 1.6.
+# We need to keep the getter if the code using this is included.
+#noinspection ShrinkerUnresolvedReference
+-if public class androidx.compose.ui.platform.AndroidCompositionLocals_androidKt {
+ public static androidx.compose.runtime.ProvidableCompositionLocal[] getLocalLifecycleOwner();
+}
+-keep public class androidx.compose.ui.platform.AndroidCompositionLocals_androidKt {
+ public static androidx.compose.runtime.ProvidableCompositionLocal[] getLocalLifecycleOwner();
+}
diff --git a/lifecycle/lifecycle-runtime-compose/src/androidMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.android.kt b/lifecycle/lifecycle-runtime-compose/src/androidMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.android.kt
new file mode 100644
index 00000000000..36cc0095c0b
--- /dev/null
+++ b/lifecycle/lifecycle-runtime-compose/src/androidMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.android.kt
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2024 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.
+ */
+
+@file:JvmName("LocalLifecycleOwnerKt")
+
+package androidx.lifecycle.compose
+
+import androidx.compose.runtime.ProvidableCompositionLocal
+import androidx.compose.runtime.staticCompositionLocalOf
+import androidx.lifecycle.LifecycleOwner
+
+/**
+ * The CompositionLocal containing the current [LifecycleOwner].
+ *
+ * **Important:** For backward compatibility with Compose 1.6.*, we will use reflection to access
+ * the [LocalLifecycleOwner] from the older `androidx.compose.ui.platform` package on Android
+ * targets. This will be cached for efficiency and has a included custom Proguard rule to prevent
+ * obfuscation issues.
+ *
+ * When using Compose 1.7.*, the [LocalLifecycleOwner] is directly accessed from the new package.
+ *
+ * Please note that backward compatibility reflection will be removed once Compose 1.7.* is stable.
+ * A Gradle dependency constraint will be put in place to ensure smooth migration for clients.
+ */
+@Suppress("CompositionLocalNaming")
+public actual val LocalLifecycleOwner: ProvidableCompositionLocal<LifecycleOwner> = run {
+ val compositionLocalFromComposeUi = runCatching {
+ // Use the `LifecycleOwner` class to find the `classLoader` from the `Application`.
+ val classLoader = LifecycleOwner::class.java.classLoader!!
+ // Top-level class name from Compose UI 1.6.* that holds the old `LocalLifecycleOwner`.
+ val className = "androidx.compose.ui.platform.AndroidCompositionLocals_androidKt"
+ // The Java getter used when accessing the `LocalLifecycleOwner` property in Kotlin.
+ val methodName = "getLocalLifecycleOwner"
+
+ val methodRef = classLoader.loadClass(className).getMethod(methodName)
+ if (methodRef.annotations.none { it is Deprecated }) {
+ // If the method IS NOT deprecated, we are running with Compose 1.6.*.
+ // We use reflection to access the older `LocalLifecycleOwner` from `compose-ui`.
+ @Suppress("UNCHECKED_CAST", "BanUncheckedReflection")
+ methodRef.invoke(null) as? ProvidableCompositionLocal<LifecycleOwner>
+ } else {
+ // If the method IS deprecated, we are running with Compose 1.7.*.
+ // The new `LocalLifecycleOwner` is available, no reflection needed.
+ null
+ }
+ }
+
+ return@run compositionLocalFromComposeUi.getOrNull()
+ ?: staticCompositionLocalOf<LifecycleOwner> {
+ error("CompositionLocal LocalLifecycleOwner not present")
+ }
+}
diff --git a/lifecycle/lifecycle-runtime-compose/src/commonMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.kt b/lifecycle/lifecycle-runtime-compose/src/commonMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.kt
index ac524bdad9c..435090ba57a 100644
--- a/lifecycle/lifecycle-runtime-compose/src/commonMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.kt
+++ b/lifecycle/lifecycle-runtime-compose/src/commonMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.kt
@@ -16,12 +16,8 @@
package androidx.lifecycle.compose
-import androidx.compose.runtime.staticCompositionLocalOf
+import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.lifecycle.LifecycleOwner
-/**
- * The CompositionLocal containing the current [LifecycleOwner].
- */
-val LocalLifecycleOwner = staticCompositionLocalOf<LifecycleOwner> {
- error("CompositionLocal LocalLifecycleOwner not present")
-}
+/** The CompositionLocal containing the current [LifecycleOwner]. */
+public expect val LocalLifecycleOwner: ProvidableCompositionLocal<LifecycleOwner>
diff --git a/lifecycle/lifecycle-runtime-compose/src/desktopMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.desktop.kt b/lifecycle/lifecycle-runtime-compose/src/desktopMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.desktop.kt
new file mode 100644
index 00000000000..1a1c7e5fd2e
--- /dev/null
+++ b/lifecycle/lifecycle-runtime-compose/src/desktopMain/kotlin/androidx/lifecycle/compose/LocalLifecycleOwner.desktop.kt
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2024 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.
+ */
+
+@file:JvmName("LocalLifecycleOwnerKt")
+
+package androidx.lifecycle.compose
+
+import androidx.compose.runtime.ProvidableCompositionLocal
+import androidx.compose.runtime.staticCompositionLocalOf
+import androidx.lifecycle.LifecycleOwner
+
+public actual val LocalLifecycleOwner: ProvidableCompositionLocal<LifecycleOwner> =
+ staticCompositionLocalOf {
+ error("CompositionLocal LocalLifecycleOwner not present")
+ }