Skip to content

Commit

Permalink
fix(Android): Add backward compatibility for prefabs (#2088)
Browse files Browse the repository at this point in the history
## 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
#2082

---------

Co-authored-by: tboba <tymoteusz.boba@gmail.com>
  • Loading branch information
piaskowyk and tboba authored Apr 2, 2024
1 parent 56ffac1 commit f769cba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
50 changes: 42 additions & 8 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,38 @@ def reactNativeArchitectures() {
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

def safeAppExtGet(prop, fallback) {
def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
appProject?.ext?.has(prop) ? appProject.ext.get(prop) : fallback
}

def resolveReactNativeDirectory() {
def reactNativeLocation = safeAppExtGet("REACT_NATIVE_NODE_MODULES_DIR", null)
if (reactNativeLocation != null) {
return file(reactNativeLocation)
}

def reactNativeFromAppNodeModules = file("${projectDir}/../../react-native")
if (reactNativeFromAppNodeModules.exists()) {
return reactNativeFromAppNodeModules
}

def reactNativeFromProjectNodeModules = file("${rootProject.projectDir}/../node_modules/react-native")
if (reactNativeFromProjectNodeModules.exists()) {
return reactNativeFromProjectNodeModules
}

throw new GradleException(
"[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."
)
}

def reactNativeRootDir = resolveReactNativeDirectory()
def reactProperties = new Properties()
file("$reactNativeRootDir/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }
def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME")
def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.startsWith("0.0.0-") ? 1000 : REACT_NATIVE_VERSION.split("\\.")[1].toInteger()

android {
compileSdkVersion safeExtGet('compileSdkVersion', rnsDefaultCompileSdkVersion)
def agpVersion = Version.ANDROID_GRADLE_PLUGIN_VERSION
Expand Down Expand Up @@ -80,12 +112,14 @@ android {
}
}
}
buildFeatures {
prefab true
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
if (REACT_NATIVE_MINOR_VERSION >= 71) {
buildFeatures {
prefab true
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
lintOptions {
Expand Down Expand Up @@ -146,11 +180,11 @@ repositories {

dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'androidx.fragment:fragment:1.3.6'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'com.google.android.material:material:1.9.0'
implementation 'com.google.android.material:material:1.6.1'
implementation "androidx.core:core-ktx:1.8.0"

constraints {
Expand Down
4 changes: 3 additions & 1 deletion android/src/main/java/com/swmansion/rnscreens/ScreenStack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ class ScreenStack(context: Context?) : ScreenContainer(context) {

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

0 comments on commit f769cba

Please sign in to comment.