Appium unlock does not work on some devices [Android]

Hi all.
I have an issue with appium unlock on android. It works on 50% of devices. It tries to unlock but unsuccessfully.

My capabilities:

  DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability(APP, APP_PATH_LOCAL); //TODO LOCAL
//        capabilities.setCapability(APP, APP_PATH_REMOTE); //TODO REMOTE
        capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, F_APP_PACKAGE);
        capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, F_APP_ACTIVITY);
        capabilities.setCapability(DEVICE_NAME, deviceName);
        capabilities.setCapability(UDID, id);
        capabilities.setCapability(NEW_COMMAND_TIMEOUT, 20);
        capabilities.setCapability(PLATFORM_NAME, "ANDROID");
        androidDriver = new AndroidDriver(new URL(LOCAL), capabilities); //TODO LOCAL
//          androidDriver = new AndroidDriver(new URL(REMOTE), capabilities); //TODO REMOTE

TestNG logs:

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Screen did not unlock successfully, retrying (WARNING: The server did not provide any stacktrace information)

Appium logs:

[AndroidDriver] Starting Android session
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","wait-for-device"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","echo","ping"]
[debug] [Logcat] Starting logcat capture
[debug] [AndroidDriver] Pushing settings apk to device...
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","install","/home/igor/Node/node-v4.4.3-linux-x64/lib/node_modules/appium/node_modules/appium-android-driver/node_modules/io.appium.settings/bin/settings_apk-debug.apk"]
[debug] [AndroidDriver] Pushing unlock helper app to device...
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","install","/home/igor/Node/node-v4.4.3-linux-x64/lib/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-unlock/bin/unlock_apk-debug.apk"]
[ADB] Getting device platform version
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","getprop","ro.build.version.release"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[AndroidDriver] Unlocking screen
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","getprop","ro.build.version.sdk"]
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Screen is locked, trying to unlock
[debug] [ADB] Device API level: 21
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","am","start","-n","io.appium.unlock/.Unlock","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /home/igor/Android/Sdk/platform-tools/adb with args: ["-P",5037,"-s","4d009d021274404b","shell","dumpsys","window"]
[debug] [AndroidDriver] Shutting down Android driver
[AndroidDriver] Cannot shut down Android driver; it has already shut down
[MJSONWP] Encountered internal error running command: Error: Screen did not unlock successfully, retrying
    at Object.callee$1$0$ (lib/android-helpers.js:348:13)
    at tryCatch (/home/igor/Node/node-v4.4.3-linux-x64/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
    at GeneratorFunctionPrototype.invoke [as _invoke] (/home/igor/Node/node-v4.4.3-linux-x64/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
    at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/home/igor/Node/node-v4.4.3-linux-x64/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
    at GeneratorFunctionPrototype.invoke (/home/igor/Node/node-v4.4.3-linux-x64/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
    at run (/home/igor/Node/node-v4.4.3-linux-x64/lib/node_modules/appium/node_modules/babel-runtime/node_modules/core-js/library/modules/es6.promise.js:104:47)
    at /home/igor/Node/node-v4.4.3-linux-x64/lib/node_modules/appium/node_modules/babel-runtime/node_modules/core-js/library/modules/es6.promise.js:115:28
    at flush (/home/igor/Node/node-v4.4.3-linux-x64/lib/node_modules/appium/node_modules/babel-runtime/node_modules/core-js/library/modules/$.microtask.js:19:5)
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)
[HTTP] <-- POST /wd/hub/session 500 43019 ms - 181

I tried to unlock device programatically from adb using this method:

public void unlockDevice() throws Exception {
        Runtime.getRuntime().exec("adb shell input keyevent 26");
    }

But it do nothing

Maybe its possible to push power button using androidDriver before setting up capabilities?
androidDriver.pressKeyCode(26);
Or possible to run adb command from code using Java?
Or something else to unlock device?

I met same issue using API21, other teams are using API19, it can work

1 Like

@willosser You mentioned you were working on Android, did you see this issue? Do you have any advice? Thank you!

Workaround for API21—Go to settings, set screen lock=none, this can work for me.

I haven’t encountered this problem, but given your last comment, I suspect I know the reason why. We always set lock=none on the device as part of setup.

@willosser thank you for your reply, so how do you set lock=none when set up, manually or programmatically? If programmatically, could you please kindly advise more(a piece of code sample)?

@mashenka123, we set it automatically. When we start our test suite, we have a device configuration function we call to make certain the device is in the correct state. Similarly, upon exit, we make certain the device is unlocked.

As for the code sample, it’s rather large so I won’t post it here. We have the following:

A library that provides access to many adb commands, one of which allows us to launch applications. We use this to launch the lock screen settings screen. We bring that up and select None (or any of the other variations of none/slide) based on the mapping of devices we’ve tested (somewhere in the 150+ range)

If the screen is locked because a previous test has exited incorrectly, we have to unlock it manually.

@willosser Many thanks for your advice, I will try with adb commands then, but not sure if I can use adb commands to control real device without root operation, I know it work well on emulator, but not sure about the real devices, our company doesn’t allow to root test devices, could you please confirm? Thanks again!

@mashenka123, I don’t test with rooted devices.

You can use the following adb command to start the lock screen settings page

    adb shell am start -a android.app.action.SET_NEW_PASSWORD

Runtime.getRuntime().exec(“adb shell input keyevent 26”);
Runtime.getRuntime().exec(“adb shell input keyevent 82”);
It will unlock any device but it is not comfortable to do it via remote

BTW my issue resolved.
Appium’s 1.5.3 unlock now works correct on all devices.
Thx for appium devs