Skip to content

Android - Animated.event useNativeDriver:true sets incorrect translate position when moving fast #50496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kocburak opened this issue Apr 4, 2025 · 18 comments
Labels
Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)

Comments

@kocburak
Copy link

kocburak commented Apr 4, 2025

Description

I have 2 transform translateY animation. When the useNativeDriver:false, there is no issue except performance. I can see the animation moves couple frames behind from native animation. When the useNativeDriver:true, while moving fast the component jumps around (usually to final position if there were no distruption to moementum scroll)

Steps to reproduce

  1. Install the application to you andorid using npx expo run:android --device while useNativeDriver: true
  2. Make a slide with momentum and while momentum exists stop and make small movement and remove you finger from screen. (see the video)

React Native Version

0.77.2

Affected Platforms

Runtime - Android

Output of npx @react-native-community/cli info

info Fetching system and libraries information...
System:
  OS: Windows 10 10.0.19045
  CPU: (16) x64 Intel(R) Core(TM) i7-10870H CPU @ 2.20GHz
  Memory: 17.67 GB / 31.84 GB
Binaries:
  Node:
    version: 22.14.0
    path: C:\Program Files\nodejs\node.EXE
  Yarn:
    version: 1.22.22
    path: ~\AppData\Roaming\npm\yarn.CMD
  npm:
    version: 11.2.0
    path: C:\Program Files\nodejs\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK:
    API Levels:
      - "22"
      - "33"
      - "33"
      - "33"
      - "34"
      - "35"
    Build Tools:
      - 30.0.2
      - 30.0.3
      - 33.0.1
      - 34.0.0
      - 35.0.0
      - 35.0.1
    System Images:
      - android-34 | Google APIs Intel x86_64 Atom
      - android-35 | AOSP ATD Intel x86_64 Atom
      - android-35 | Intel x86_64 Atom
      - android-35 | Google APIs Intel x86_64 Atom
    Android NDK: Not Found
  Windows SDK:
    AllowDevelopmentWithoutDevLicense: Enabled
    Versions:
      - 10.0.19041.0
      - 10.0.22621.0
IDEs:
  Android Studio: AI-242.23726.103.2422.13016713
  Visual Studio:
    - 17.13.35825.156 (Visual Studio Community 2022)
Languages:
  Java: 17.0.7
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli":
    installed: 15.1.3
    wanted: ^15.1.0
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.77.2
    wanted: ^0.77.2
  react-native-windows: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

info React Native v0.78.2 is now available (your project is running on v0.77.2).
info Changelog: https://github.com/facebook/react-native/releases/tag/v0.78.2
info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.77.2&to=0.78.2
info For more info, check out "https://reactnative.dev/docs/upgrading?os=windows".

Stacktrace or Logs

N/A

Reproducer

https://snack.expo.dev/@kocburak/usenativedriverbug

Screenshots and Videos

Screen_Recording_20250404_190621.mp4
@kocburak
Copy link
Author

kocburak commented Apr 5, 2025

I tried it on ios there is no issue.

@kocburak kocburak changed the title useNativeDriver:true sets incorrect translate position when moving fast Android - Animated.event useNativeDriver:true sets incorrect translate position when moving fast Apr 8, 2025
@kocburak
Copy link
Author

kocburak commented Apr 8, 2025

I looked the value usingdisclaimerTranslateY.addListener(value => { console.log(value) }) and the values were correct even though the placement on the screen was wrong. If you need more clarification about the topic I am willing to help.

@kocburak
Copy link
Author

kocburak commented Apr 8, 2025

I narrowed it down to: Animated.diffClamp function. When I remove this line there is no issue regarding the useNativeDriver:true

@kocburak
Copy link
Author

kocburak commented Apr 8, 2025

When I looked into DiffClampAnimatedNode.kt file I noticed val animatedNode = nativeAnimatedNodesManager.getNodeById(inputNodeTag) line. Is it possible that we are reading from wrong Tag ?

@kocburak
Copy link
Author

kocburak commented Apr 10, 2025

@react-native-bot why are you ignoring me :(
@cortinico Sorry for direct mention but even the bot ignored this issue 😄. Is there anything I should look into more?

@cortinico cortinico added Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. and removed Needs: Triage 🔍 labels Apr 11, 2025
@cortinico
Copy link
Contributor

I don't have an exact answer for this one. I believe this is related to an issue we had with Animated + transforms which is still unresolved. @cipolleschi do you have more context or a issue number to link here?

@kocburak
Copy link
Author

I found couple of issues but couldn't find any workaround.

#10174 : Suggest to use shouldComponentUpdate = () => false But I couldn't figure out how in my TypeScript with function components.
GeekyAnts/NativeBase#334 : No response
#10663 : This is a fix for iOS. I do not have an issue on iOS. Is this fix portable to Android ?

@cipolleschi
Copy link
Contributor

I don't have more context on this. @kocburak here you mentioned:

I narrowed it down to: Animated.diffClamp function. When I remove this line there is no issue regarding the useNativeDriver:true

What line are you referring to?

When I looked into DiffClampAnimatedNode.kt file I noticed val animatedNode = nativeAnimatedNodesManager.getNodeById(inputNodeTag) line. Is it possible that we are reading from wrong Tag ?

It could be but it is unlikely.

About: #50496 (comment)

@kocburak
Copy link
Author

kocburak commented Apr 14, 2025

In my Reproducer, all my animation logic is as follows:

//Buggy code.
const scrollY = useRef(new Animated.Value(0));

  const handleScroll = Animated.event(
    [{ nativeEvent: { contentOffset: { y: scrollY.current } } }],
    { useNativeDriver: true }
  );

  const minusScrollY = Animated.multiply(scrollY.current, -0.1);
   const stickyTranslateY = Animated.diffClamp(scrollY, -headerHeight, 0);

  let stickyStyles: StyleProp<ViewStyle> = {
    transform: [{ translateY: stickyTranslateY , }]
  }

This is the logic of 1 scroll. Bug still can be reproduce with this much. When I changed it to:

//Working code.
const scrollY = useRef(new Animated.Value(0));

  const handleScroll = Animated.event(
    [{ nativeEvent: { contentOffset: { y: scrollY.current } } }],
    { useNativeDriver: true }
  );

  const minusScrollY = Animated.multiply(scrollY.current, -0.1);
  //const stickyTranslateY = Animated.diffClamp(minusScrollY, -headerHeight, 0);  // <- commented out

  let stickyStyles: StyleProp<ViewStyle> = {
    transform: [{ translateY: minusScrollY }] // <- changed the argument from stickyTranslateY to minusScrollY 
  }

There is no animation bug (notice the comment on const minusScrollY = Animated.diffClamp(minusScrollY, -headerHeight, 0);. Also I tried to comment out multply and leave it out the Animated.diffClamp like this:

//Buggy code.
  const scrollY = useRef(new Animated.Value(0));

  const handleScroll = Animated.event(
    [{ nativeEvent: { contentOffset: { y: scrollY.current } } }],
    { useNativeDriver: true }
  );

  //const minusScrollY = Animated.multiply(scrollY.current, -0.1);   // <- commented out
  const stickyTranslateY = Animated.diffClamp(scrollY.curent, -headerHeight, 0); // <- changed the argument from minusScrollY to scrollY.curent

  let stickyStyles: StyleProp<ViewStyle> = {
    transform: [{ translateY: stickyTranslateY }] 
  }

The issue came back.

Edit: scrollY.current.interpolate can also be removed so i did in this post.

@kocburak
Copy link
Author

That is how I figured Animated.diffClamp causing the problem

@kocburak
Copy link
Author

@cortinico I saw this #50716 issue and tried #50496 (comment) with "newArchEnabled": false and no issue. Whatever the problem is, it is related to new Architecture of React Native.

I would like to continue to use new Arch. We are using JNI Turbo Module for some performance improvements. When I tried old arch on my app, it didn't like it 😄 (didn't compile at all).

@kocburak
Copy link
Author

I am trying to debug this. "DiffClampAnimatedNode.kt" has "prettyPrint" function. How can I enbale it so that I can see when the value changes ?

@sarthak-cars24
Copy link

I have been seeing the same issue after upgrading to React Native v0.76.8. Is their any workaround for this for the time being? Converting all off the codebase to use reanimated just because of this does not make sense, but it has become a release blocker for us.

@cipolleschi cipolleschi added the Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules) label Apr 24, 2025
@cipolleschi
Copy link
Contributor

We don't have any update on this currently, but thank to @kocburak for the thorough investigation.
There might be a bug in the Animated.diffClamp implementation, we need to further investigate it.

I am trying to debug this. "DiffClampAnimatedNode.kt" has "prettyPrint" function. How can I enbale it so that I can see when the value changes ?

@cortinico do you know how to enable it?

@cortinico In general, if you want to debug android, you need to build React Native from source as Android apps use a prebuild version of React Native, so modifying the files in the node_modules folder will not make any visible change in the executed code.

@cortinico
Copy link
Contributor

@cortinico do you know how to enable it?

It's already enabled. It prints on adb logcat here:

@kocburak
Copy link
Author

kocburak commented Apr 24, 2025

Hi @sarthak-cars24 Unfortunetly I couldn't find a solution. I tried to re do the same animation in reanimated. It worked but then I find out when I import the reanimated package, other pages that uses the safe-area-context package is broken. Whatever I tried the layout of the app is broken the moment I add the reanimated package. Good luck with yours.

Hi @cipolleschi, I will try it. This bug looks like a race condition between the layers of architecture. Probably one of the layer does not update properly. At least thats what I think. But I am very new in react native so I don't have my hopes up

@kocburak
Copy link
Author

@cipolleschi I failed spectacularly. I added the lines to android/settings.gradle:

includeBuild('../node_modules/react-native') {
    dependencySubstitution {
        substitute(module("com.facebook.react:react-android")).using(project(":packages:react-native:ReactAndroid"))
        substitute(module("com.facebook.react:react-native")).using(project(":packages:react-native:ReactAndroid"))
        substitute(module("com.facebook.react:hermes-android")).using(project(":packages:react-native:ReactAndroid:hermes-engine"))
        substitute(module("com.facebook.react:hermes-engine")).using(project(":packages:react-native:ReactAndroid:hermes-engine"))
    }
}

It gave the following error:

PS D:\GitHub\AwesomeProject> npm run android

> AwesomeProject@0.0.1 android
> react-native run-android    

info Installing the app...

> Task :react-native:packages:react-native:ReactAndroid:hermes-engine:configureBuildForHermes FAILED

[Incubating] Problems report is available at: file:///D:/GitHub/AwesomeProject/android/build/reports/problems/problems-report.html

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.13/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
38 actionable tasks: 3 executed, 35 up-to-date

info 💡 Tip: Make sure that you have set up your development environment correctly, by running npx react-native doctor. To read more about doctor command visit: https://github.com/react-native-community/cli/blob/main/packages/cli-doctor/README.md#doctor

CMake Error at CMakeLists.txt:64 (project):
  Running

   'nmake' '-?'

  failed with:

   Sistem belirtilen dosyayı bulamıyor


CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':react-native:packages:react-native:ReactAndroid:hermes-engine:configureBuildForHermes'.
> Process 'command 'cmd'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 2m 29s
error Failed to install the app. Command failed with exit code 1: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
CMake Error at CMakeLists.txt:64 (project): Running 'nmake' '-?' failed with: Sistem belirtilen dosyayı bulamıyor CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':react-native:packages:react-native:ReactAndroid:hermes-engine:configureBuildForHermes'. > Process 'command 'cmd'' finished with non-zero exit value 1 * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 2m 29s.
info Run CLI with --verbose flag for more details.

I read the lines saying "nmake System could not findt it". So I tried added this PATH to System Variables (I am on Windows 10): C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x64 and then it gave the following error where I couldn't figure out a solution.

PS D:\GitHub\AwesomeProject> npm run android

> AwesomeProject@0.0.1 android
> react-native run-android    

info Installing the app...

> Task :react-native:packages:react-native:ReactAndroid:hermes-engine:configureBuildForHermes FAILED

[Incubating] Problems report is available at: file:///D:/GitHub/AwesomeProject/android/build/reports/problems/problems-report.html

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.13/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
54 actionable tasks: 5 executed, 49 up-to-date

info 💡 Tip: Make sure that you have set up your development environment correctly, by running npx react-native doctor. To read more about doctor command visit: https://github.com/react-native-community/cli/blob/main/packages/cli-doctor/README.md#doctor

CMake Error at C:/Users/MONSTER/AppData/Local/Android/Sdk/cmake/3.22.1/share/cmake-3.22/Modules/CMakeTestCCompiler.cmake:69 (message):
  The C compiler

    "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: D:/GitHub/AwesomeProject/node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/CMakeFiles/CMakeTmp

    Run Build Command(s):nmake -f Makefile /nologo cmTC_7fe01\fast &&   "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x64\nmake.exe"  -f CMakeFiles\cmTC_7fe01.dir\build.make /nologo -L                  CMakeFiles\cmTC_7fe01.dir\build
    Building C object CMakeFiles/cmTC_7fe01.dir/testCCompiler.c.obj
        C:\Users\MONSTER\AppData\Local\Android\Sdk\cmake\3.22.1\bin\cmake.exe -E cmake_cl_compile_depends --dep-file=CMakeFiles\cmTC_7fe01.dir\testCCompiler.c.obj.d --working-dir=D:\GitHub\AwesomeProject\node_modules\react-native\ReactAndroid\hermes-engine\build\hermes\CMakeFiles\CMakeTmp --filter-prefix="Note: including file: " -- C:\PROGRA~1\MICROS~3\2022\COMMUN~1\VC\Tools\MSVC\1443~1.348\bin\Hostx64\x64\cl.exe @C:\Users\MONSTER\AppData\Local\Temp\nm924E.tmp
    testCCompiler.c
    Linking C executable cmTC_7fe01.exe
        C:\Users\MONSTER\AppData\Local\Android\Sdk\cmake\3.22.1\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_7fe01.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\PROGRA~1\MICROS~3\2022\COMMUN~1\VC\Tools\MSVC\1443~1.348\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\cmTC_7fe01.dir\objects1.rsp @C:\Users\MONSTER\AppData\Local\Temp\nm929D.tmp
    RC Pass 1: command "rc /fo CMakeFiles\cmTC_7fe01.dir/manifest.res CMakeFiles\cmTC_7fe01.dir/manifest.rc" failed (exit code 0) with the following output:
    Sistem belirtilen dosyayı bulamıyorNMAKE : fatal error U1077: 'C:\Users\MONSTER\AppData\Local\Android\Sdk\cmake\3.22.1\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_7fe01.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\PROGRA~1\MICROS~3\2022\COMMUN~1\VC\Tools\MSVC\1443~1.348\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\cmTC_7fe01.dir\objects1.rsp @C:\Users\MONSTER\AppData\Local\Temp\nm929D.tmp' : return code '0xffffffff'
    Stop.
    NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x64\nmake.exe"  -f CMakeFiles\cmTC_7fe01.dir\build.make /nologo -L                  CMakeFiles\cmTC_7fe01.dir\build' : return code '0x2'
    Stop.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:64 (project)



FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':react-native:packages:react-native:ReactAndroid:hermes-engine:configureBuildForHermes'.
> Process 'command 'cmd'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 46s
error Failed to install the app. Command failed with exit code 1: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
CMake Error at C:/Users/MONSTER/AppData/Local/Android/Sdk/cmake/3.22.1/share/cmake-3.22/Modules/CMakeTestCCompiler.cmake:69 (message): The C compiler "C:/Program Files/Microsoft Visual 
Studio/2022/Community/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe" is not able to compile a simple test program. It fails with the following output: Change Dir: D:/GitHub/AwesomeProject/node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/CMakeFiles/CMakeTmp Run Build Command(s):nmake -f Makefile /nologo cmTC_7fe01\fast && "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x64\nmake.exe" -f CMakeFiles\cmTC_7fe01.dir\build.make /nologo -L CMakeFiles\cmTC_7fe01.dir\build Building C object CMakeFiles/cmTC_7fe01.dir/testCCompiler.c.obj C:\Users\MONSTER\AppData\Local\Android\Sdk\cmake\3.22.1\bin\cmake.exe -E cmake_cl_compile_depends --dep-file=CMakeFiles\cmTC_7fe01.dir\testCCompiler.c.obj.d --working-dir=D:\GitHub\AwesomeProject\node_modules\react-native\ReactAndroid\hermes-engine\build\hermes\CMakeFiles\CMakeTmp --filter-prefix="Note: including file: " -- C:\PROGRA~1\MICROS~3\2022\COMMUN~1\VC\Tools\MSVC\1443~1.348\bin\Hostx64\x64\cl.exe @C:\Users\MONSTER\AppData\Local\Temp\nm924E.tmp testCCompiler.c Linking C executable cmTC_7fe01.exe C:\Users\MONSTER\AppData\Local\Android\Sdk\cmake\3.22.1\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_7fe01.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\PROGRA~1\MICROS~3\2022\COMMUN~1\VC\Tools\MSVC\1443~1.348\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\cmTC_7fe01.dir\objects1.rsp @C:\Users\MONSTER\AppData\Local\Temp\nm929D.tmp RC Pass 1: command "rc /fo CMakeFiles\cmTC_7fe01.dir/manifest.res CMakeFiles\cmTC_7fe01.dir/manifest.rc" failed (exit code 0) with the following output: Sistem belirtilen dosyayı bulamıyorNMAKE : fatal error U1077: 'C:\Users\MONSTER\AppData\Local\Android\Sdk\cmake\3.22.1\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_7fe01.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\PROGRA~1\MICROS~3\2022\COMMUN~1\VC\Tools\MSVC\1443~1.348\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\cmTC_7fe01.dir\objects1.rsp @C:\Users\MONSTER\AppData\Local\Temp\nm929D.tmp' : return code '0xffffffff' Stop. NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x64\nmake.exe" -f CMakeFiles\cmTC_7fe01.dir\build.make /nologo -L CMakeFiles\cmTC_7fe01.dir\build' : return code '0x2' Stop. CMake will not be able to correctly generate this project.
Call Stack (most recent call first): CMakeLists.txt:64 (project) FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':react-native:packages:react-native:ReactAndroid:hermes-engine:configureBuildForHermes'. > Process 'command 'cmd'' finished with non-zero exit value 1 * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 46s.
info Run CLI with --verbose flag for more details.

Do you have any comment on this ? What am I doing wrong ?

@cortinico
Copy link
Contributor

Do you have any comment on this ? What am I doing wrong ?

That's because build from source doesn't work well with your Windows setup. For build form source on Android, we recommend to use either Linux or Mac machines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)
Projects
None yet
Development

No branches or pull requests

4 participants