1

Can anyone help in how to install .apk file in the android device/emulator programmatically?

I have tried the below methods:

UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mDevice.executeShellCommand("adb shell pm install -t -r /data/local/tmp/com.example.xxxxx.xxxxxx"); 

But it is not working.

I am using UIAutomator for android native app automation testing, I need to install .apk file into android device/emulator before proceeding with my test scripts execution.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
mra419
  • 413
  • 4
  • 9
  • 23
  • any error messages? and can not see .apk file name in your codes. – navylover Sep 11 '18 at 11:08
  • I have used like this , mDevice.executeShellCommand("adb install -t -r /Users/xxxx/Desktop/app-debug.apk");, Also tried with package name as i mentioned in my question. – mra419 Sep 11 '18 at 11:17

3 Answers3

1

executeShellCommand runs inside your device. No need adb shell again.

UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mDevice.executeShellCommand("pm install -t -r /data/local/tmp/com.example.xxxxx.xxxxxx");
Rilwan
  • 2,251
  • 2
  • 19
  • 28
  • Thanks Rilwan, It is working. I have removed adb shell and tried. But before executing this i have pused .apk file to android device manually and then I have tried with above code. Can you please help me in how to execute adb push command through code to push my .apk file in my system to android device. I have tried with "Process p = Runtime.getRuntime().exec(cmd);" But getting this error "Permission denied error -13" – mra419 Sep 13 '18 at 07:22
  • Good it worked! The execShell is executing inside your phone. You cannot invoke a command which need to be run at your pc from your phone's shell. To programatically push a file from your system, you need to run `adb push` command in your PC. you can accept this a answer if the solution answered your question :) – Rilwan Sep 13 '18 at 16:12
0

You can use the code below to install an application from the command line

adb install example.apk
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
-1
Before execute `adb shell pm install`, run "adb root" command firstly.

if you rooted the device, this step could be omitted.

navylover
  • 12,383
  • 5
  • 28
  • 41
  • When i try this command in the terminal, i am getting error as - "adbd cannot run as root in production builds" – mra419 Sep 11 '18 at 11:42