This is a complex problem.
This ultimately comes down to how bones and their transforms are stored in the engine vs how blender does things and how blender handles keyframes. Things like Euler/Quaternion or the Basis Order of the keyframe vectors or even the keyframes being stored as relative or absolute and lastly the layout of the bones in the rig.
Matching all of these when transferring an animation will likely solve your issues
My solution was to save the animation and the rig as discrete files and treat them as a pair. One rig has many animations that only work on this particular rig. To share animations consider using some visual re-targeting method (easier said that done).