Error getting device API level and crashing ADB

Hi,
my test fails with “Error getting device API level” and crashing ADB. I use real devices (Xiaomi redmi 5 plus, Galaxy a7) with different android versions (7.1, 8.1) and problem occurs on both windows 7 and windows 10.

When i try to execute adb.exe -P 5037 -s 520080e95a9c1549 shell getprop ro.build.version.sdk in CMD as administrator, it returns the value and then crashes ADB (adb stop working). However, it does not crash when I open adb shell first and then execute getprop ro.build.version.sdk.

Appium 1.9.1
ADB version 1.0.40 --4986621
Everything up to date in SDK manager

Message: Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception:
System.InvalidOperationException: An unknown server-side error occurred while processing the command. Original error: Error getting device API level. Original error: Error executing adbExec. Original error: ‘Command ‘H:\SDK\platform-tools\adb.exe -P 5037 -s 520080e95a9c1549 shell getprop ro.build.version.sdk’ exited with code 3221226356’; Stderr: ‘’; Code: ‘3221226356’

[Appium] Welcome to Appium v1.9.1
[Appium] Non-default server args:
[Appium] address: 127.0.0.2
[Appium] Appium REST http interface listener started on 127.0.0.2:4723
[HTTP] --> POST /wd/hub/session
[HTTP] {“desiredCapabilities”:{“automationName”:“Appium”,“platformName”:“Android”,“platformVersion”:“7.1.1”,“deviceName”:“samsung”,“udid”:“07c92c189805”,“appPackage”:“com.android.vending”,“appActivity”:“com.android.vending.AssetBrowserActivity”,“noReset”:“true”,“noSign”:“true”}}
[MJSONWP] Calling AppiumDriver.createSession() with args: [{“automationName”:“Appium”,“platformName”:“Android”,“platformVersion”:“7.1.1”,“deviceName”:“samsung”,“udid”:“07c92c189805”,“appPackage”:“com.android.vending”,“appActivity”:“com.android.vending.AssetBrowserActivity”,“noReset”:“true”,“noSign”:“true”},null,null]
[BaseDriver] Event ‘newSessionRequested’ logged at 1541499471620 (11:17:51 GMT+0100 (Środkowoeuropejski czas stand.))
[Appium] Consider setting ‘automationName’ capability to ‘UiAutomator2’ on Android >= 6, since UIAutomator framework is not maintained anymore by the OS vendor.
[Appium] Creating new AndroidDriver (v4.1.1) session
[Appium] Capabilities:
[Appium] automationName: Appium
[Appium] platformName: Android
[Appium] platformVersion: 7.1.1
[Appium] deviceName: samsung
[Appium] udid: 07c92c189805
[Appium] appPackage: com.android.vending
[Appium] appActivity: com.android.vending.AssetBrowserActivity
[Appium] noReset: true
[Appium] noSign: true
[BaseDriver] Creating session with MJSONWP desired capabilities: {“automationName”:“Appium”,…
[BaseDriver] Capability ‘noReset’ changed from string to boolean. This may cause unexpected behavior
[BaseDriver] Capability ‘noSign’ changed from string to boolean. This may cause unexpected behavior
[BaseDriver] Session created with session id: a6bcb901-dbc8-4b17-b50a-92268d42787d
[AndroidDriver] Java version is: 1.8.0_192
[AndroidDriver] Retrieving device list
[ADB] Trying to find a connected android device
[ADB] Getting connected devices…
[ADB] 1 device(s) connected
[AndroidDriver] Using device: 07c92c189805
[ADB] Setting device id to 07c92c189805
[ADB] Running ‘H:\SDK\platform-tools\adb.exe -P 5037 -s 07c92c189805 shell getprop ro.build.version.sdk’
[ADB] Running ‘H:\SDK\platform-tools\adb.exe -P 5037 -s 07c92c189805 shell getprop ro.build.version.sdk’
[AndroidDriver] Shutting down Android driver
[AndroidDriver] Called deleteSession but bootstrap wasn’t active
[ADB] Running ‘H:\SDK\platform-tools\adb.exe -P 5037 -s 07c92c189805 shell am force-stop io.appium.unlock’
[ADB] Running ‘H:\SDK\platform-tools\adb.exe -P 5037 -s 07c92c189805 shell am force-stop io.appium.unlock’
[BaseDriver] Event ‘newSessionStarted’ logged at 1541499480328 (11:18:00 GMT+0100 (Środkowoeuropejski czas stand.))
[MJSONWP] Encountered internal error running command: Error: Error getting device API level. Original error: Error executing adbExec. Original error: ‘Command ‘H:\SDK\platform-tools\adb.exe -P 5037 -s 07c92c189805 shell getprop ro.build.version.sdk’ exited with code 3221226356’; Stderr: ‘’; Code: ‘3221226356’
[MJSONWP] at ADB.callee$0$0$ (C:\Users\dj25581\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-adb\lib\tools\adb-commands.js:75:13)
[MJSONWP] at tryCatch (C:\Users\dj25581\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
[MJSONWP] at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\dj25581\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22)
[MJSONWP] at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (C:\Users\dj25581\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21)
[MJSONWP] at GeneratorFunctionPrototype.invoke (C:\Users\dj25581\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
[MJSONWP] at
[HTTP] <-- POST /wd/hub/session 500 8710 ms - 397
[HTTP]

MY CODE
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;

namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{

    AppiumDriver<IWebElement> driver;

    [TestMethod]
    public void TestMethod1()
    {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.SetCapability("automationName", "Appium");
        cap.SetCapability("platformName", "Android");
        cap.SetCapability("platformVersion", "7.1.1");
        cap.SetCapability("deviceName", "xiaomi");
        cap.SetCapability("udid", "07c92c189805");
        cap.SetCapability("appPackage", "com.android.vending");
        cap.SetCapability("appActivity", "com.android.vending.AssetBrowserActivity");
        cap.SetCapability("noReset", "true");
        cap.SetCapability("noSign", "true");


        driver = new AndroidDriver<IWebElement>(new Uri("http://127.0.0.2:4723/wd/hub"), cap);

        Assert.IsTrue(true);

    }
}

}

OK, I kind of solved my problem by using older version of ADB:

Android Debug Bridge version 1.0.36
Revision 0e9850346394-android

It’s not the best solution but at least it works (does not crash anymore).

This is to confirm that the latest version of ADB (at the time of this writing) has some serious bugs.
Downgrade your adb manually to resolve this issue

In my case, upgrading Android SDK Platform-Tools to 29.0.1 (from 29.0.0) resolved the “Error getting device API level” crash.