A few approaches depending on your setup:
ADB + UIAutomator2 — Android Debug Bridge lets you tap, swipe, type, and read UI elements from command line. UIAutomator2 is the standard framework on top of it. With root access you have even more control. You can drive devices from your PC over USB or WiFi. This is what most mobile farming setups use at scale.
Appium — wraps UIAutomator2 with a clean API you can script in Python, JS, or Java. More structured than raw ADB, better for multi-device control and reliable element selectors. Good starting point since there's solid documentation.
Auto.js / Hamibot — JavaScript-based automation that runs directly on-device, no PC required. Good for simpler tasks. Some apps detect these through accessibility service detection, though root helps bypass that.
OpenCV + ADB — for apps that obfuscate their UI tree, you screenshot the screen, do pattern matching with OpenCV, then tap at matched coordinates. More fragile but works when element selectors fail.
Since you have root access, you can also use Frida for hooking into app internals — useful for bypassing detection or manipulating app state directly. More advanced but very powerful.
For SM apps specifically: Instagram and TikTok actively detect automation through timing patterns and device fingerprints. Start with UIAutomator2, add random delays between actions, and make sure your device fingerprints are varied if you're running multiple devices.
The ADB + UIAutomator2 + Python combo is the practical starting point. Plenty of tutorials on GitHub for this setup.