quamotion / madb

SharpAdbClient is a .NET library that allows .NET applications to communicate with Android devices. It provides a .NET implementation of the adb protocol, giving more flexibility to the developer than launching an adb.exe process and parsing the console output.
Apache License 2.0
361 stars 133 forks source link

AdbClient.Instance.Install() fails if apk is already installed #123

Open the1337moderator opened 6 years ago

the1337moderator commented 6 years ago

When attempting to use AdbClient.Instance.Install() with an APK that is already installed on the target device, a SharpAdbClient.Exceptions.AdbException is received.

Possible solution: Check if already installed, have a bool flag as function parameter to determine if it should be skipped or should be uninstalled/installed

Calling code:

foreach (FileInfo apkFile in new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "Resources\\apk")).GetFiles("*.apk").ToList())
{
    Console.Write($"Installing Package:{apkFile.Name}");
    using (FileStream fs = new FileStream(apkFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        try
        {
            AdbClient.Instance.Install(device, fs);  // Exception thrown if already installed.
            Console.WriteLine("     Done.");
        }
        catch (SharpAdbClient.Exceptions.AdbException ex) { Console.WriteLine("     Failed" + ex.Message); }
    }
}

Exception data:

SharpAdbClient.Exceptions.AdbException HResult=0x80131500 Message=Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install com.adobe.reader without first uninstalling.] Source=SharpAdbClient StackTrace: at SharpAdbClient.AdbClient.Install(DeviceData device, Stream apk, String[] arguments) at TabletPrep.Program.Main(String[] args) in \source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 102

kluivert-queiroz commented 4 years ago

If someone still suffering with something similar, the solution is pass -r parameter to the requestBuilder inside AdbClient class.

This will make the app reinstall if needed, it's defined in ADB documentation.

Example: AdbClient.Instance.install(device, stream, new string[] {"-r"};