How to use isAppInstalled(String bundleId) method?

Hi All.

I need to check whether application is already installed in device !!How to get the instance to the driver before or after connecting to Server before installation of application.

Something like:

if(driver.isAppInstalled(“ABC”) == false)
{
then connect to Appium Server(URL ,Capabilities);
}
else
{
install the application
}

You don’t mention what platform you are testing, but for iOS you could check for the installed app with something like:

ideviceinstaller -u <uuid> -l

And then search the results for the app/bundleId you are looking for. I believe you could do the same for Android with an adb command.

adb shell pm list packages

https://developer.android.com/tools/help/shell.html#pm :wink:

Thanks for the reply Wreed !!

I was trying to automate the Android application.So,after Appium Server connects to mobile and before application is launched at this Point of Time I need to check for the condition whether app is installed or not.

if(driver.isAppInstalled("ABC") != true){
 WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);}
else
{By.xpath("//android.widget.EditText[@content-desc='UsernameId']")).sendKeys("XXXXX");}

but the driver instance in the if condition is invalid because at that point of time it is null.So it is invalid!!

Need help on this.

Hi @Smaran,

So as @afwang said, just use the adb command at that point. You can just make a system call to ‘adb shell pm list packages’ and parse the result. Since the driver is invalid, don’t use it.

Thanks @wreed and @afwang !! I mean it!! :slightly_smiling:

I am stuck in same problem . I have tried in different ways but its not working . Can you share snippet of your code.

My code is in Ruby, which I doubt you are using (most around here use Java). Instead, why don’t you share what you’ve tried and tell what’s not working? Your description of the problem is decidedly lacking in details.

Hi , I have trying following code:-

public void setUp() throws MalformedURLException
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“deviceName”, “ZX1B32FFXF”);
capabilities.setCapability(“browserName”, “Android”);
capabilities.setCapability(“platformVersion”, “4.4.2”);
capabilities.setCapability(“platformName”, “Android”);
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
try{
if (driver.isAppInstalled(“com.hannainst.hannalab”))
{
System.out.println("isAppInstalled - "+driver.isAppInstalled(“com.hannainst.hannalab”));
System.out.println(“App is already installed”);
}else
{
capabilities.setCapability(“app”, “C:\Desktop\HannaLab-2016-09-07.apk”);
capabilities.setCapability(“appActivity”,“com.hannainst.hannalab.ManagerActivity”);
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 300);
wait.until(ExpectedConditions.elementToBeClickable(By.className(“android.widget.RelativeLayout”)));
}
} catch(Throwable e)
{
System.out.println(“Something wrong”);
}
}
}

Please help me.I am stuck

Ok, so here is where you are stuck. The driver is not instantiated and you want to check the device to see if the app is installed. So instead of using the above code you’ll need to make a system call to adb. Here is a tutorial on making a system call from Java:

Just sub in the commands that were given above (‘adb shell pm list packages’) and parse the results.

Thank you very much. I will try this.

Hi, I am trying as per your suggestion but unable to implement it properly.If possible please share code
I have got list of connected devices but unable to check installed app

Code like that:
AndroidDebugBridge.init(false);

    AndroidDebugBridge debugBridge = AndroidDebugBridge.createBridge("C:\\adb.exe", true);
    if (debugBridge == null) {
        System.err.println("Invalid ADB location.");
        System.exit(1);
    }

    AndroidDebugBridge.addDeviceChangeListener(new IDeviceChangeListener() {

        @Override
        public void deviceChanged(IDevice device, int arg1) {
          
        }

        @Override
        public void deviceConnected(IDevice device) {
            System.out.println(String.format("%s connected", device.getSerialNumber()));
        }

Sounds like you need to get the UDID (aka UUID) of the device you want to test, and then craft a command that gets all the installed apps on that device (and then parse that).

You want to see my code, and I’ve told you it won’t make much sense to you but here goes. This is the way we install an app. Note that adb is abstracted into it’s own class to make tests easier to write.

@adb = AppiumAndroid::LocalAdb.new apm.device_uuid
      log.info 'Pushing large app. This can take a while (90+ sec)'
      # Can't use Appium's push, results in broken pipe error
@adb.command "push #{settings.large_apk} #{DEVICE_APK}"

Hi, I am trying the following code but it is not working

       try{
          DesiredCapabilities capabilities = new DesiredCapabilities();
          capabilities.setCapability("deviceName", "vpk");
          capabilities.setCapability("browserName", "Android");
          capabilities.setCapability("platformVersion", "5.2");
          capabilities.setCapability("platformName", "Android");
          capabilities.setCapability("rotatable", true);
          capabilities.setCapability("appPackage", "com.hannainst.hannalab");
          capabilities.setCapability("appActivity","com.hannainst.hannalab.ManagerActivity");
          if(driver.isAppInstalled("com.hannainst.hannalab") == false)
          {
              System.out.println("App already present");
              
          }
          else
          {
          capabilities.setCapability("app", "C:\\HannaLab-2016-09-28.apk");
          }
          driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
          driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
          WebDriverWait wait = new WebDriverWait(driver, 300);
          wait.until(ExpectedConditions.elementToBeClickable(By.className("android.widget.RelativeLayout")));
          System.out.println("TC0 :-App Launched Successfully");
          } catch(Throwable e)
            {
                System.out.println("Test Case Failed");
                
            }
         }

Mate, @wreed already told you the issue you have in your code: you are using driver before you instantiate the driver object.

This line:

driver.isAppInstalled("com.hannainst.hannalab")

is being executed before this line:

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);