Skip to content

Commit 884cc7f

Browse files
piaskowyktboba
authored andcommitted
fix(Android): Add backward compatibility for prefabs (software-mansion#2088)
## Description This PR fixes backward compatibility with React Native versions older than 0.71. The issue was caused by using prefabs in the screen transition animation feature. I added a check that makes this feature available since React Native 0.71 Tested against RN versions: - [x] 0.68 - [x] 0.69 - [x] 0.70 - [x] 0.71 - [x] 0.73 Fixes software-mansion#2082 --------- Co-authored-by: tboba <[email protected]>
1 parent bc57291 commit 884cc7f

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

android/build.gradle

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,38 @@ def reactNativeArchitectures() {
4646
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
4747
}
4848

49+
def safeAppExtGet(prop, fallback) {
50+
def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
51+
appProject?.ext?.has(prop) ? appProject.ext.get(prop) : fallback
52+
}
53+
54+
def resolveReactNativeDirectory() {
55+
def reactNativeLocation = safeAppExtGet("REACT_NATIVE_NODE_MODULES_DIR", null)
56+
if (reactNativeLocation != null) {
57+
return file(reactNativeLocation)
58+
}
59+
60+
def reactNativeFromAppNodeModules = file("${projectDir}/../../react-native")
61+
if (reactNativeFromAppNodeModules.exists()) {
62+
return reactNativeFromAppNodeModules
63+
}
64+
65+
def reactNativeFromProjectNodeModules = file("${rootProject.projectDir}/../node_modules/react-native")
66+
if (reactNativeFromProjectNodeModules.exists()) {
67+
return reactNativeFromProjectNodeModules
68+
}
69+
70+
throw new GradleException(
71+
"[RNScreens] Unable to resolve react-native location in node_modules. You should project extension property (in `app/build.gradle`) `REACT_NATIVE_NODE_MODULES_DIR` with path to react-native."
72+
)
73+
}
74+
75+
def reactNativeRootDir = resolveReactNativeDirectory()
76+
def reactProperties = new Properties()
77+
file("$reactNativeRootDir/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }
78+
def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME")
79+
def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.startsWith("0.0.0-") ? 1000 : REACT_NATIVE_VERSION.split("\\.")[1].toInteger()
80+
4981
android {
5082
compileSdkVersion safeExtGet('compileSdkVersion', rnsDefaultCompileSdkVersion)
5183
def agpVersion = Version.ANDROID_GRADLE_PLUGIN_VERSION
@@ -80,12 +112,14 @@ android {
80112
}
81113
}
82114
}
83-
buildFeatures {
84-
prefab true
85-
}
86-
externalNativeBuild {
87-
cmake {
88-
path "CMakeLists.txt"
115+
if (REACT_NATIVE_MINOR_VERSION >= 71) {
116+
buildFeatures {
117+
prefab true
118+
}
119+
externalNativeBuild {
120+
cmake {
121+
path "CMakeLists.txt"
122+
}
89123
}
90124
}
91125
lintOptions {
@@ -146,11 +180,11 @@ repositories {
146180

147181
dependencies {
148182
implementation 'com.facebook.react:react-native:+'
149-
implementation 'androidx.appcompat:appcompat:1.5.0'
183+
implementation 'androidx.appcompat:appcompat:1.4.2'
150184
implementation 'androidx.fragment:fragment:1.3.6'
151185
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
152186
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
153-
implementation 'com.google.android.material:material:1.9.0'
187+
implementation 'com.google.android.material:material:1.6.1'
154188
implementation "androidx.core:core-ktx:1.8.0"
155189

156190
constraints {

android/src/main/java/com/swmansion/rnscreens/ScreenStack.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ class ScreenStack(context: Context?) : ScreenContainer(context) {
337337

338338
private fun needsDrawReordering(fragmentWrapper: ScreenFragmentWrapper): Boolean =
339339
// On Android sdk 33 and above the animation is different and requires draw reordering.
340-
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ||
340+
// For React Native 0.70 and lower versions, `Build.VERSION_CODES.TIRAMISU` is not defined yet.
341+
// Hence, we're comparing numerical version here.
342+
Build.VERSION.SDK_INT >= 33 ||
341343
fragmentWrapper.screen.stackAnimation === StackAnimation.SLIDE_FROM_BOTTOM ||
342344
fragmentWrapper.screen.stackAnimation === StackAnimation.FADE_FROM_BOTTOM ||
343345
fragmentWrapper.screen.stackAnimation === StackAnimation.IOS

0 commit comments

Comments
 (0)