Skip to content

Commit d7ff82e

Browse files
Abbondanzometa-codesync[bot]
authored andcommitted
Fix Activity Result launcher compilation (#58688)
Summary: Pull Request resolved: #58688 Different AndroidX artifacts expose `ActivityResultLauncher.contract` as either a Kotlin property or a Java `getContract()` method. A Kotlin subclass cannot override both source representations even though they have the same JVM signature. Route the contract accessor through a package-private Java superclass so both representations resolve to the same JVM method. Use the bridge in production and test launchers, verify that the registered contract is preserved, and allowlist this required Java compatibility source. Changelog: [Android][Fixed] - Fix Activity Result launcher compilation across AndroidX source variants Reviewed By: fkgozali Differential Revision: D121845437 fbshipit-source-id: 3d4e2c5726a8b39e5bb3863211fb7c88dd16e43a
1 parent 5c87ca9 commit d7ff82e

4 files changed

Lines changed: 35 additions & 5 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.activityresult;
9+
10+
import androidx.activity.result.ActivityResultLauncher;
11+
import androidx.activity.result.contract.ActivityResultContract;
12+
13+
abstract class ActivityResultLauncherCompat<I> extends ActivityResultLauncher<I> {
14+
private final ActivityResultContract<I, ?> contract;
15+
16+
ActivityResultLauncherCompat(ActivityResultContract<I, ?> contract) {
17+
this.contract = contract;
18+
}
19+
20+
@Override
21+
public ActivityResultContract<I, ?> getContract() {
22+
return contract;
23+
}
24+
}

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/activityresult/DeferredActivityResultLauncher.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import com.facebook.react.common.ReactConstants
2626
*/
2727
internal class DeferredActivityResultLauncher<I>(
2828
private val key: String,
29-
override val contract: ActivityResultContract<I, *>,
29+
contract: ActivityResultContract<I, *>,
3030
private val onUnregister: () -> Unit,
3131
private val onLaunchFailure: (RuntimeException) -> Unit = {},
32-
) : ActivityResultLauncher<I>() {
32+
) : ActivityResultLauncherCompat<I>(contract) {
3333

3434
private class PendingLaunch<I>(val input: I, val options: ActivityOptionsCompat?)
3535

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/activityresult/ReactActivityResultCallerImplTest.kt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ class ReactActivityResultCallerImplTest {
8181
caller = ReactActivityResultCallerImpl(reactContext)
8282
}
8383

84+
@Test
85+
fun launcherExposesRegisteredContract() {
86+
val contract = GetContent()
87+
val launcher = caller.registerForActivityResult(moduleA, contract) {}
88+
89+
assertThat(launcher.contract).isSameAs(contract)
90+
}
91+
8492
@Test
8593
fun twoOwnersMayRegisterTheSameStockContract() {
8694
caller.registerForActivityResult(moduleA, GetContent()) {}

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/activityresult/ReactActivityResultCallerThreadingTest.kt‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import android.app.Activity
1111
import android.content.ActivityNotFoundException
1212
import android.os.Bundle
1313
import android.os.Looper
14-
import androidx.activity.result.ActivityResultLauncher
1514
import androidx.activity.result.ActivityResultRegistry
1615
import androidx.activity.result.ActivityResultRegistryOwner
1716
import androidx.activity.result.contract.ActivityResultContract
@@ -65,8 +64,7 @@ class ReactActivityResultCallerThreadingTest {
6564
.orEmpty()
6665
}
6766

68-
private class ThrowingLauncher : ActivityResultLauncher<String>() {
69-
override val contract: ActivityResultContract<String, *> = GetContent()
67+
private class ThrowingLauncher : ActivityResultLauncherCompat<String>(GetContent()) {
7068
var launchCount = 0
7169

7270
override fun launch(input: String, options: ActivityOptionsCompat?) {

0 commit comments

Comments
 (0)