Skip to content

Commit 52fd654

Browse files
azverkanfacebook-github-bot
authored andcommitted
Fix TextLayoutManager compilation against compile SDK 34 (facebook#56118)
Summary: TextLayoutManager.kt references Android 15 (API 35) symbols (`Build.VERSION_CODES.VANILLA_ICE_CREAM` and `StaticLayout.Builder.setUseBoundsForWidth`) that are unavailable when compiling against SDK level 34. This causes build failures for any Android target using `compile_sdk_version = 34`. Fix by defining `VERSION_CODE_VANILLA_ICE_CREAM = 35` in `AndroidVersion.kt` (following the existing `VERSION_CODE_BAKLAVA` pattern) and using reflection for `setUseBoundsForWidth`. Runtime behavior is unchanged — the version check guards ensure these code paths only execute on Android 15+ devices. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D95994030
1 parent b1e2e5a commit 52fd654

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/util/AndroidVersion.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import android.os.Build
1313
/** Helper class for checking Android version-related information. */
1414
internal object AndroidVersion {
1515

16+
/**
17+
* This is the version code for Android 15 (SDK Level 35). Internally at Meta this code is also
18+
* compiled against SDK 34, so we need to retain this constant instead of using
19+
* [Build.VERSION_CODES.VANILLA_ICE_CREAM] directly.
20+
*/
21+
internal const val VERSION_CODE_VANILLA_ICE_CREAM: Int = 35
22+
1623
/**
1724
* This is the version code for Android 16 (SDK Level 36). Delete it once we bump up the default
1825
* compile SDK version to 36.

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import com.facebook.react.uimanager.PixelUtil
3636
import com.facebook.react.uimanager.PixelUtil.dpToPx
3737
import com.facebook.react.uimanager.PixelUtil.pxToDp
3838
import com.facebook.react.uimanager.ReactAccessibilityDelegate
39+
import com.facebook.react.util.AndroidVersion.VERSION_CODE_VANILLA_ICE_CREAM
3940
import com.facebook.react.views.text.internal.span.CustomLetterSpacingSpan
4041
import com.facebook.react.views.text.internal.span.CustomLineHeightSpan
4142
import com.facebook.react.views.text.internal.span.CustomStyleSpan
@@ -105,6 +106,18 @@ internal object TextLayoutManager {
105106

106107
private val tagToSpannableCache = ConcurrentHashMap<Int, Spannable>()
107108

109+
// Lazily cached Method for StaticLayout.Builder.setUseBoundsForWidth (API 35+).
110+
// Reflection is needed because some internal targets compile against an SDK older than 35.
111+
private val setUseBoundsForWidthMethod: java.lang.reflect.Method? by lazy {
112+
try {
113+
StaticLayout.Builder::class
114+
.java
115+
.getMethod("setUseBoundsForWidth", Boolean::class.javaPrimitiveType)
116+
} catch (_: ReflectiveOperationException) {
117+
null
118+
}
119+
}
120+
108121
fun setCachedSpannableForTag(reactTag: Int, sp: Spannable): Unit {
109122
tagToSpannableCache[reactTag] = sp
110123
}
@@ -623,7 +636,7 @@ internal object TextLayoutManager {
623636

624637
// Pre-Android 15: Use existing advance-based logic
625638
if (
626-
Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM ||
639+
Build.VERSION.SDK_INT < VERSION_CODE_VANILLA_ICE_CREAM ||
627640
!ReactNativeFeatureFlags.fixTextClippingAndroid15useBoundsForWidth()
628641
) {
629642
val desiredWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt()
@@ -724,11 +737,14 @@ internal object TextLayoutManager {
724737
builder.setUseLineSpacingFromFallbacks(true)
725738
}
726739

740+
// setUseBoundsForWidth added in API 35 — use reflection to support internal targets that
741+
// compile against an SDK older than 35.
742+
// https://developer.android.com/reference/android/text/StaticLayout.Builder#setUseBoundsForWidth(boolean)
727743
if (
728-
Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM &&
744+
Build.VERSION.SDK_INT >= VERSION_CODE_VANILLA_ICE_CREAM &&
729745
ReactNativeFeatureFlags.fixTextClippingAndroid15useBoundsForWidth()
730746
) {
731-
builder.setUseBoundsForWidth(true)
747+
setUseBoundsForWidthMethod?.invoke(builder, true)
732748
}
733749

734750
return builder.build()

0 commit comments

Comments
 (0)