If you want to directly get the package name of the current app in focus, use this adb command -
adb shell dumpsys window windows | grep -E 'mFocusedApp'| cut -d / -f 1 | cut -d " " -f 7
Extra info from the result of the adb command is removed using the cut command. Original solution from here.