diff --git a/lifecycle/lifecycle-viewmodel/build.gradle b/lifecycle/lifecycle-viewmodel/build.gradle index 30b57d8be7cbf..6b26ecbe86c84 100644 --- a/lifecycle/lifecycle-viewmodel/build.gradle +++ b/lifecycle/lifecycle-viewmodel/build.gradle @@ -23,9 +23,11 @@ */ import androidx.build.KmpPlatformsKt +import androidx.build.KotlinTarget import androidx.build.PlatformIdentifier import androidx.build.Publish import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode +import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType import org.jetbrains.kotlin.konan.target.Family plugins { @@ -33,12 +35,17 @@ plugins { id("com.android.library") } +def jsEnabled = KmpPlatformsKt.enableJs(project) +def wasmEnabled = KmpPlatformsKt.enableWasm(project) + androidXMultiplatform { android() desktop() mac() linux() ios() + js() + wasm() kotlin { explicitApi = ExplicitApiMode.Strict @@ -104,6 +111,10 @@ androidXMultiplatform { darwinMain.dependsOn(nativeMain) linuxMain.dependsOn(nativeMain) + if (jsEnabled || wasmEnabled) { + webMain.dependsOn(nonJvmMain) + } + targets.all { target -> if (target.platformType == KotlinPlatformType.native) { target.compilations["main"].defaultSourceSet { @@ -116,6 +127,13 @@ androidXMultiplatform { throw new GradleException("unknown native target ${target}") } } + } else if (target.platformType in [ + KotlinPlatformType.js, + KotlinPlatformType.wasm + ]) { + target.compilations["main"].defaultSourceSet { + dependsOn(webMain) + } } } } @@ -131,6 +149,7 @@ android { androidx { name = "Lifecycle ViewModel" publish = Publish.SNAPSHOT_AND_RELEASE + kotlinTarget = KotlinTarget.KOTLIN_1_9 inceptionYear = "2017" description = "Android Lifecycle ViewModel" metalavaK2UastEnabled = true diff --git a/lifecycle/lifecycle-viewmodel/src/commonMain/kotlin/androidx/lifecycle/viewmodel/internal/Lock.kt b/lifecycle/lifecycle-viewmodel/src/commonMain/kotlin/androidx/lifecycle/viewmodel/internal/Lock.kt index b342a1908074b..24b8438db270c 100644 --- a/lifecycle/lifecycle-viewmodel/src/commonMain/kotlin/androidx/lifecycle/viewmodel/internal/Lock.kt +++ b/lifecycle/lifecycle-viewmodel/src/commonMain/kotlin/androidx/lifecycle/viewmodel/internal/Lock.kt @@ -23,6 +23,7 @@ package androidx.lifecycle.viewmodel.internal * The implementation depends on the platform: * - On JVM/ART: uses JDK's synchronization. * - On Native: uses posix. + * - On JS/WASM: No-op as it's single thread environment. */ internal expect class Lock() { diff --git a/lifecycle/lifecycle-viewmodel/src/webMain/kotlin/androidx/lifecycle/viewmodel/internal/Lock.web.kt b/lifecycle/lifecycle-viewmodel/src/webMain/kotlin/androidx/lifecycle/viewmodel/internal/Lock.web.kt new file mode 100644 index 0000000000000..ca21a0338587f --- /dev/null +++ b/lifecycle/lifecycle-viewmodel/src/webMain/kotlin/androidx/lifecycle/viewmodel/internal/Lock.web.kt @@ -0,0 +1,21 @@ +/* + * 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. + */ + +package androidx.lifecycle.viewmodel.internal + +internal actual class Lock actual constructor() { + actual inline fun withLock(crossinline block: () -> T): T = block() +} diff --git a/lifecycle/lifecycle-viewmodel/src/webMain/kotlin/androidx/lifecycle/viewmodel/internal/ViewModelProviders.web.kt b/lifecycle/lifecycle-viewmodel/src/webMain/kotlin/androidx/lifecycle/viewmodel/internal/ViewModelProviders.web.kt new file mode 100644 index 0000000000000..e271ef392ec42 --- /dev/null +++ b/lifecycle/lifecycle-viewmodel/src/webMain/kotlin/androidx/lifecycle/viewmodel/internal/ViewModelProviders.web.kt @@ -0,0 +1,22 @@ +/* + * 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. + */ + +package androidx.lifecycle.viewmodel.internal + +import kotlin.reflect.KClass + +internal actual val KClass.canonicalName: String? + get() = simpleName // `qualifiedName` reflection API is not supported yet in JavaScript