Test app in android phone using command line? - android

Android Studio is running quite slow in my laptop so Im planning use Android Studio just for building the app structure and use Sublime Text 3 for coding, but I want to test my app in my phone connected via USB using a command within the terminal. ADB maybe? or something else?

you can push the apk and install using adb install.
Of course, you still need to compile the new APK each time.

To run your tests from the command line use am instrument. For example to run all tests in your package:
$ adb shell am instrument -w com.example.foo/android.test.InstrumentationTestRunner
assuming your application package name is com.example.foo and is using InstrumentationTestRunner.
To verify your test package is installed, run
$ adb shell pm list instrumentation
EDIT
Or, if you just want to run the app once installed
$ adb shell am start -n com.example.foo/.MainActivity

Related

How to launch adb shell in Android Studio?

I am using Android Studio 2020.3.1.
I want to launch a adb shell from within Android Studio.
I have the Terminal tab at the very bottom of the IDE.
But I can only open "Local" terminals.
Any ideas where I can launch a "Remote" adb shell?
Within you local terminal, you can easily start an adb shell with the command adb shell
Locate adb if it's not already in your executable paths environment variable. The location largely dependent on the OS you use and where you install the Android SDK. In general it's in the ${ANDROID_SDK}/platform-tools/ directory.
Execute adb devices. This will list the connected adb capable devices. If you are not running any emulators and you only connect your phone then your phone would show up (if not then you may need to treat some permission steps depending on your operating system). Let's say the ID of your device is XYZ.
Execute adb -s XYZ shell and you'll be in a shell on your device.

How to install over existing Android app on device using adb?

During development, I use something like
$ adb -s 192.168.1.77:5555 uninstall com.myApp.app
$ adb -s 192.168.1.77:5555 install /path/to/android-debug.apk
to uninstall the existing app, then push and install the updated app to my device. However, this seems to erase my existing data associated with the app so that each time I run the updated app I need to input the new data again. However, simply running the install command gives:
[ 10%] /data/local/tmp/android-debug.apk
[100%] /data/local/tmp/android-debug.apk
pkg: /data/local/tmp/android-debug.apk
Failure [INSTALL_FAILED_ALREADY_EXISTS]
With iOS you can simply install your updated app over the existing app on the device.
What is the equivalent for Android preferably using adb?
Edit: This question is really similar to this one, but I think this should be kept because the way my question is phrased will help many people trying to find an answer that are coming from a different angle– the angle being of reinstalling/overwriting versus updating.
from adb --help
adb install [-lrtsdg] <file>
- push this package file to the device and install it
(-l: forward lock application)
(-r: replace existing application)
(-t: allow test packages)
(-s: install application on sdcard)
(-d: allow version code downgrade (debuggable packages only))
(-g: grant all runtime permissions)
Therefore you can just add -r option
$ adb -s 192.168.1.77:5555 install -r /path/to/android-debug.apk

android espresso: running test from command line

I have a test.espresso package with all the test classes.
I am trying to run a single test class from the command line, however it ends up running all the test classes.
adb shell am instrument -w \
com.demo.app.test/android.support.test.runner.AndroidJUnitRunner
How do I just run a single test class. I want to use bamboo(which is like jenkins) to run all the test classes individually in separate jobs.
This worked for me (the change is in bold:
adb shell am instrument -w-e class full.path.and.TestClassName\ com.demo.app.test/android.support.test.runner.AndroidJUnitRunner
Based on: https://developer.android.com/studio/test/command-line.html#AMOptionsSyntax (look under options for "class").
If you're using gradle, then there is gradle task you can directly use to achieve it. It would be something like this:
./gradlew connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
To run on specific flavor:
./gradlew connectedMyAppFlavorDebugAndroidTest
It does everything for you, right from building the app, installing on the connected device, run the tests and finally uninstall the app.
If you're not sure about the exact gradle task you need to execute the tests, run the following to get the all available gradle tasks:
./gradlew tasks
You'll get the list of all the tasks with the short description.
To run via command line
Start device. I use Genymotion so I would do
gmtool admin start DeviceName
Install via command line
For ADB
for ADB is should be exactly what the console outputs from Android studio when you start .
$ adb push /Users/x/x-android/app/build/outputs/apk/x-debug.apk
$ adb shell pm install -r "/data/local/tmp/com.x"
$ adb push /x/x/x-android/app/build/outputs/apk/x-debug-androidTest.apk /data/local/tmp/com.x.test
$ adb shell pm install -r "/data/local/tmp/com.x.test"
For Genymotion it is
gmtool device install ~/Desktop/x.apk
gmtool device install ~/Desktop/x-androidTest.apk
For Genymotion connect Genymotion to ADB
gmtool device adbconnect
Start your tests. This is for both ADB and Geny
adb shell am instrument -w -r -e debug false -e class com.x.MyTest com.x.test/android.support.test.runner.AndroidJUnitRunner
I'm adding also how to run it from Gradle here.
./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.MyClassTest#myFunctionTest

force android tests to run on different emulators from command line batch file

How can I run my android junit/robotium tests from the command line on every single emulator? I want to make sure my tests run on many android OS versions and many screen resolutions.
I'd like to write a batch file that runs from the windows command line to run my test suite over and over again on each emulator I have installed.
To run from the command line I can do this:
adb shell am instrument -w
com.myapp.client.test/android.test.InstrumentationTestRunner
but that just runs it on the default emulator. How can I force that command to run on all of the emulators I have setup?
Ideally, the batch file would looking something like:
launch emulator1
run tests
close emulator1
launch emulator2
run tests
close emulator2
...
I don't know how to do the launch and close part.
Thanks
EDIT: Found solutions. Below is my batch file
set PORTRAIT=medium
set LANDSCAPE=large
:: launch emulator
emulator -avd android2.2
:: wait for emulator to load
adb wait-for-device
:: install apps?
:: run tests in portrait
adb shell am instrument -w -e size %PORTRAIT% com.myapp.client.test/android.test.InstrumentationTestRunner
:: run tests in landscape
adb shell am instrument -w -e size %LANDSCAPE% com.myapp.client.test/android.test.InstrumentationTestRunner
:: pull screenshots
adb pull /sdcard/ c:\
:: close/kill emulator (android bug here, so must use windows taskkill)
taskkill /IM emulator-arm.exe
I would really recommend you looking to use something like Jenkins to handle this for you. You can use the android emulator plugin to build up a whole matrix of API versions/Screen size to have your tests run against.

How to run Android instrumentation tests from the command line (in Kubuntu)?

We are able to run instrumentation tests of Android from the command line on Windows by launching:
adb shell
am instrument -w <package.test>/android.test.InstrumentationTestRunner
This gives us good results.
Using the same architecture, we are unable to run the same in Kubuntu.
We have the same setup in Kubuntu.
Can someone let us know, if there are packages with same name.. Then what package will the adb shell point?
How will the emulator connect with adb shell from cmd line?
DO we need to do any changes to do so in Kubuntu ?
You need to explain what errors you are seeing.
If you have the same setup under Kubuntu, i.e. the Android SDK is installed, with tools like adb accessible in your path, then everything should work fine.
In response to your individual points (and these answers are the same on Windows, Mac or Linux):
It is not possible to have more than one Android package installed on a device or emulator with the same package name.
You can connect to the emulator — the same as for any device — by calling adb shell, e.g.:
adb -d shell if you have a single USB-attached device
adb -e shell if you have a single emulator running
adb -s emulator-5554 shell to specify a particular emulator (or device serial number)
You don't need to change anything between operating systems. The difference would be with setting up a device, as you need to modify udev rules on Linux, and install the USB driver on Windows

Categories

Resources