Back to project page adb-idea.
The source code is released under:
Apache License
If you think the Android project adb-idea listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.developerphil.adbidea.adb; //from w w w. j a v a 2s. c o m import com.android.ddmlib.AdbCommandRejectedException; import com.android.ddmlib.IDevice; import com.android.ddmlib.ShellCommandUnresponsiveException; import com.android.ddmlib.TimeoutException; import com.developerphil.adbidea.adb.command.receiver.GenericReceiver; import org.jetbrains.android.facet.AndroidFacet; import java.io.IOException; import java.util.concurrent.TimeUnit; public class AdbUtil { public static boolean isAppInstalled(IDevice device, String packageName) throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException { GenericReceiver receiver = new GenericReceiver(); // "pm list packages com.my.package" will return one line per package installed that corresponds to this package. // if this list is empty, we know for sure that the app is not installed device.executeShellCommand("pm list packages " + packageName, receiver, 5L, TimeUnit.MINUTES); //TODO make sure that it is the exact package name and not a subset. // e.g. if our app is called com.example but there is another app called com.example.another.app, it will match and return a false positive return !receiver.getAdbOutputLines().isEmpty(); } /** * Computes the project's package while preserving backward compatibility between android studio 0.4.3 and 0.4.4 */ public static String computePackageName(AndroidFacet facet) { try { Object androidModuleInfo = facet.getClass().getMethod("getAndroidModuleInfo").invoke(facet); return (String) androidModuleInfo.getClass().getMethod("getPackage").invoke(androidModuleInfo); } catch (Exception e) { e.printStackTrace(); } return null; } }