Appium - Create AppiumDriver with desiredcapabilities in Appium


public static AppiumDriver<AndroidElement>  getDriver(){
        if(driver == null)
        {
            try {
                setUp();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return driver;
    }

    public static void setUp() throws Exception {
            try {
                startAppiumServer();
                File classpathRoot = new File(System.getProperty("user.dir"));
                File appDir = new File("<Path of APK>"); //D:/app/build/outputs/apk/
                File app = new File(appDir, "app-debug.apk");
                capabilities = new DesiredCapabilities();
                capabilities.setCapability("deviceName", "Emulator-5554");
                capabilities.setCapability("platformVersion", "5.1.1");
                capabilities.setCapability("noReset", true); //Set to true if .apk need not be Installed for every test run
                capabilities.setCapability("fullReset", false); // Set to true if .apk needs to be installed on every test run
                capabilities.setCapability("app", app.getAbsolutePath());
                capabilities.setCapability("appPackage", "com.flipkart.mobility");
                capabilities.setCapability("elementScrollBehavior", true);
                driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            } catch (UnreachableBrowserException e) {
            }
    }