How to automate any android application using uiautomator2 driver?

I am trying to implement the uiautomator 2 into my project.
I tried to find the answer but didn’t succeed.
Can someone tell me all the changes required and the way how to set it up?

For java

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);

Documentation in: