ActivityManager#isUserAMonkey()
developer.android.com
developer.android.com
There's also isUserAGoat [2] which is more of an easter egg - initially (and now) it always returns false, but certain versions return true if Goat Simulator is installed [3].
[1] https://developer.android.com/studio/test/monkey
[2] https://developer.android.com/reference/android/os/UserManag...()
[3] https://android.googlesource.com/platform/frameworks/base/+/...