How do I run android tests in the emulator using IntelliJ? - android

I'm trying to run the sample unit tests for the sample code that ships with the android SDK. Some of the tests run as standard unit tests, no problem. Other tests, such as com.example.android.apis.view.Focus2AndroidTest, use android classes, and therefore need to run in the emulator, that is a problem.
I can run applications just fine in my emulator. When I run tests, though, my emulator starts up, and then just sits there. The test never runs. All I see in the output window in IntelliJ is
Waiting for device.
/Users/rfzabick/android-sdk-mac_x86/tools/emulator -avd MyAvd0 -netspeed full -netdelay none
Device connected: emulator-5554
What am I doing wrong?
EDIT:
After #CrazyCoder's advice, I switched to android 4.0.3 (API 15). Here's what I got:
Testing started at 4:34 PM ...
Waiting for device.
/Users/rfzabick/android-sdk-mac_x86/tools/emulator -avd android4.0.3--api15 -netspeed full -netdelay none -wipe-data -no-boot-anim
Device connected: emulator-5554
Device is online: emulator-5554
Target device: emulator-5554 (android4.0.3--api15)
Uploading file
local path: /Users/rfzabick/IdeaProjects/ApiDemos/out/production/Tests/Tests.apk
remote path: /data/local/tmp/com.example.android.apis.tests
Installing com.example.android.apis.tests
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
Device is not ready. Waiting for 20 sec.
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
Device is not ready. Waiting for 20 sec.
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
Device disconnected: emulator-5554
pkg: /data/local/tmp/com.example.android.apis.tests
Running tests
Test running startedTest running failed: com.android.ddmlib.AdbCommandRejectedException: device not found
Empty test suite.
The only relevant thing I see in logcat is
01-21 16:36:22.047: WARN/ActivityManager(91): No content provider found for permission revoke: file:///data/local/tmp/com.example.android.apis.tests

Can you run normal apps in the emulator or USB device? Try to create the new emulator device and see if helps.
I've tried it with IDEA 11.0.1, 4.0.3 Android platform on Windows and it works fine:
Waiting for device.
Target device: emulator-5554 (ICS)
Uploading file
local path: D:\dev\android-sdk-windows\samples\android-15\ApiDemos\out\production\Tests\Tests.apk
remote path: /data/local/tmp/com.example.android.apis.tests
Installing com.example.android.apis.tests
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
pkg: /data/local/tmp/com.example.android.apis.tests
Success
Running tests
Test running startedFinish
You can also try to restart adb with adb kill-server and start the emulator manually from AVD manager.
Verify that there is no pre-installed ApiDemos application in the emulator, or the signatures will not match. Uninstall existing ApiDemos, then try to deploy and run ApiDemos from IntelliJ IDEA, then try to run the test configuration again.
UPDATE: We did some research and found the source of the problem. By default IntelliJ IDEA sets the dependency scope for the application module inside test module to Compile, so that all the production and test classes get compiled into the single Test.apk.
Instead, the scope must be set to Provided and we'll fix it in the next update. Right now you need to correct it manually as shown on the screenshot:
Rebuild the project and run the tests, again, this time 2 separate apk files will be deployed, one for the main app and the second one for the tests, then tests will run:
Waiting for device.
Target device: emulator-5554 (ICS)
Uploading file
local path: D:\dev\android-sdk-windows\samples\android-15\ApiDemos\out\production\Tests\Tests.apk
remote path: /data/local/tmp/com.example.android.apis.tests
Installing com.example.android.apis.tests
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
pkg: /data/local/tmp/com.example.android.apis.tests
Success
Uploading file
local path: D:\dev\android-sdk-windows\samples\android-15\ApiDemos\out\production\ApiDemos\ApiDemos.apk
remote path: /data/local/tmp/com.example.android.apis
Installing com.example.android.apis
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis"
pkg: /data/local/tmp/com.example.android.apis
Success
Running tests
Test running startedFinish

You can run the instrumentation from ant, from the command line of from a script. In case you prefer the latter this post may be of help.

Running unit tests in emulator is not practical (basically takes too long). And since android jars are usefull only to compiling, it seems impossiblble to run them with unit tests. Good alternative is use of advanced mocking framework ( I personally prefer jMockit,
but there are other )
See example:
https://github.com/ko5tik/andject/blob/master/src/test/java/de/pribluda/android/andject/ViewInjectionTest.java
Here I test my class against derived android activity, and it has to call superclass methods
(this runs in maven, eclipse or IDEA on the spot )

Related

Can't get React Native to work on Ubuntu

I am trying to get React Native running on Ubuntu 16.04. I have used it with create-react-native-app and expo on my Windows machine with both the Android Studio virtual device and a tablet connected by USB. Now I have a new Ubuntu laptop and a Ubuntu dual boot on my PC and am trying to set it up there but have failed miserably.
Let's deal with the laptop. Following the instructions, I have installed Expo and Genymotion. Expo could never work right so now I'm just trying to get it going more directly. I have also installed Java 8 and watchman. I've installed and tried too many things to mention. I've put in about 20 hours so far.
Here is what happens when I try to start things.
If I plug in my tablet, I get:
kevin#kevin-Kudu:~/programming/rn4$ adb devices
adb server is out of date. killing...
* daemon started successfully *
List of devices attached
5205132d4f9011af device
(As a side note, it seems that my adb server is always out of date.) When I try to run the default code installed by react-native-cli by running react-native run-android, I get:
kevin#kevin-Kudu:~/programming/rn4$ react-native run-android
Scanning folders for symlinks in /home/kevin/programming/rn4/node_modules (9ms)
Starting JS server...
Building and installing the app on the device (cd android && ./gradlew installDebug)...
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
I've researched these errors and can't find a solution that works.
If I start an Android Studio virtual device (N6, API 23), it starts up. adb devices gets me:
kevin#kevin-Kudu:~/programming/rn4$ adb devices
adb server is out of date. killing...
* daemon started successfully *
List of devices attached
5205132d4f9011af device
When I try to run the app:
kevin#kevin-Kudu:~/programming/rn4$ react-native run-android
Scanning folders for symlinks in /home/kevin/programming/rn4/node_modules (9ms)
Starting JS server...
Building and installing the app on the device (cd android && ./gradlew installDebug)...
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
Again, I haven't been apple to find a solution.
When I try to do the same thing through genymotion and do adb devices, I get:
kevin#kevin-Kudu:~/programming/rn4$ adb devices
adb server is out of date. killing...
cannot bind 'tcp:5037'
ADB server didn't ACK
* failed to start daemon *
error:
I get the same result when I try to run the app.
Any ideas what I should try next? Please bear in mind that I am still fairly new to linux so please be specific. If you tell me, "Simply change the RX74 constant in the Fetzer config file" I'm not going to have any idea what you are talking about. (I've had bad experiences with linux advice that assumes that I know what every file on the system is and where to find it.)
Thanx in advance.
ADDENDUM 1 #Vigas Deep:
When I check which -a adb:
kevin#kevin-Kudu:~$ which -a adb
/usr/bin/adb
/usr/bin/adb
/home/kevin/Android/Sdk/platform-tools/adb
So I have more than one. I'll figure out why one is getting called twice, for now I just rename it, and now I get:
kevin#kevin-Kudu:~$ which -a adb
/home/kevin/Android/Sdk/platform-tools/adb
I fire up the Android Studio virtual device and check it:
kevin#kevin-Kudu:~$ adb devices
List of devices attached
emulator-5554 device
And when I try to run a RN starter app:
kevin#kevin-Kudu:~/programming/rn4$ react-native run-android
Scanning folders for symlinks in /home/kevin/programming/rn4/node_modules (18ms)
Starting JS server...
Building and installing the app on the device (cd android && ./gradlew installDebug)...
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
Interestingly, when I try to restart the adb server, I get:
kevin#kevin-Kudu:~/programming/rn4$ sudo adb kill-server && adb start-server
[sudo] password for kevin:
sudo: adb: command not found
Does the adb in the SDK not have those commands?
Just for kicks, I reboot and try pluggin in a tablet:
kevin#kevin-Kudu:~$ adb devices
List of devices attached
5205132d4f9011af device
(But for some reason only shows up when plugged into the USB2 port, very inconveniently located.)
When I try to run the RN project:
kevin#kevin-Kudu:~/programming/rn4$ react-native run-android
Scanning folders for symlinks in /home/kevin/programming/rn4/node_modules (21ms)
Starting JS server...
Building and installing the app on the device (cd android && ./gradlew installDebug)...
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
Sorry, that's all the testing I can do for now. Any other ideas?
ADDENDUM 2 #Vigas Deep:
"just to make sure that its using the adb located inside sdk; use this command for now: /home/kevin/Android/Sdk/platform-tools/adb devices"
OK, I get the same result whichever I use.
"and do not use sudo for adb command because we are not going to run react-native run-android as sudo."
OK. All the examples I saw used sudo to start and kill the adb server. Strangely, now it works without sudo but not with. Before, I think it was the opposite, but I haven't tried it without in a long time so I can't be sure.
"Also; are you using openJDK or oracleJDK ; how did you install the JDK?"
I thought I remembered going to the Oracle site, but when I do "java -version", I get:
kevin#kevin-Kudu:~/programming/rn4$ java -version
java version "1.7.0_95"
OpenJDK Runtime Environment (IcedTea 2.6.4) (7u95-2.6.4-3)
OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)
This would seem to indicate otherwise. Should I change it?
ADDENDUM 3 #Vigas Deep:
OK, I looked up how to remove openjdk and followed it. I found instructions about how to install the jdk8, just to be sure. I am getting the same error when I try to run the RN app. When I check java -version:
kevin#kevin-Kudu:~/programming/rn4$ java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
and
kevin#kevin-Kudu:~/programming/rn4$ readlink -f $(which java)
/usr/lib/jvm/java-8-oracle/jre/bin/java
When I check the jvm folder:
kevin#kevin-Kudu:/usr/lib/jvm$ ls
java-8-oracle
Just in case, here is my current $PATH:
kevin#kevin-Kudu:/usr/lib/jvm$ echo -e ${PATH//:/\\n}
/home/kevin/bin
/home/kevin/.local/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games
/home/kevin/bin
/usr/bin
/snap/bin
/usr/lib/jvm/java-8-oracle/bin
/usr/lib/jvm/java-8-oracle/db/bin
/usr/lib/jvm/java-8-oracle/jre/bin
/home/kevin/Android/Sdk/tools
/home/kevin/Android/Sdk/platform-tools
Any other ideas? Am I missing something? (highly likely)
ADDENDUM 4 #Vigas Deep:
OK, so completely removed Java, mainly following this.
I reinstalled it using this.
After some monkeying around with genymotion, I was able to get an app started on create-react-native-app that ran on genymotion.
Thank you #vigas-deep. If you're even in Oakland, I owe you a beer, or whatever your beverage of choice is. Thanx!
I have eight years of experience with Ubuntu and other Linux based OSes.
I think the problem is you are having multiple versions of ADB.
run the following command to know if that's right.
which -a adb
And if you have multiple versions of adb, just keep the adb version which is inside your android sdk and remove/rename others.
prefer rename just incase if you need those executables again.
I'd be happy to help more if needed.

Jenkins running on headless linux aws with xvfb getting error for the connectedAndroidTest espresso

Giving Error on jenkins while running android espresso test.
My server is headless linux aws, and i am using xvfb to run android emulator virtually and want to run test cases.
i have already create emulator with command line
android create avd --force -n nexus4-emulator2 -t "Google Inc.:Google APIs:18" --abi default/armeabi-v7a -s "768x1280" --device "Nexus 4" -c 128M
this is creating emulator, first it was giving permission error, but after adding access permission it get resolved.
now some of logs on jenkins is like below
[android] Starting Android emulator
$ /opt/android-sdk-linux//platform-tools/adb -s emulator-5788
wait-for-device shell getprop init.svc.bootanim [android] Emulator
reported that the startup process is 'running'
[android] Attempting to unlock emulator screen
:Chynge:preBuild UP-TO-DATE
Installing APK 'app-debug.apk' on 'nexus4-emulator(AVD) - 4.3.1' for
Chynge:debug Installed on 1 device.
:app:connectedDebugAndroidTest
com.app_positive.LoginTest > loginTest[nexus4-emulator(AVD) - 4.3.1]
[31mFAILED [0m android.support.test.espresso.NoMatchingViewException:
No views in hierarchy found matching: (with id: com.app:id/btn_login
and with text: is "Login with E-Mail" and is displayed on the screen
to the user)
:app:connectedDebugAndroidTest FAILED
What went wrong: Execution failed for task ':app:connectedDebugAndroidTest'.
There were failing tests. See the report at: file:///data/jenkins/workspace/android-ci-tests/client/android/app/build/reports/androidTests/connected/index.html
[Gradle] - Launching build.
Build step 'Invoke Gradle script' changed build result to FAILURE Xvfb
stopping Finished: FAILURE
![Emulator Configuration][see here]![Gradle task configuration][see here] I haven't added command to create emulator or to open it, but Jenkins provides android emulator plugin that do all things like creating emulator, wait to open, launch emulator, i just have to add configuration for it.
finally i got answer, i tried to create emulator on my windows machine with same configuration on Jenkins,and install app on it , it was showing google play service update pop up, and that's the reason, runner unable to find view on screen, as pop up don't allow to run the app, query of creating emulator is like
path upto android_sdk_tools>> android create avd --force -n emualator_29 -t android-23 --abi google_apis/armeabi-v7a -s "768x1280" --device "Nexus 4" -c 128M
just by taking new version 23, some ideal screen resolution, and one more only armeabi-v7a runs on headless machine...it works for me..thank you..
I haven't used ever Jenkins, but I used Travis CI for building my Github projects. I know that can after installing plugin read Travis configuration files with .yml extensions.
Here's my configuration of Android emulator
- echo no | android create avd --force -n test -t $ANDROID_TARGET --abi armeabi-v7a --sdcard 200M
- emulator -avd test -no-audio -no-window &
- android-wait-for-emulator
- sleep 10
- adb shell settings put global window_animation_scale 0 &
- adb shell settings put global transition_animation_scale 0 &
- adb shell settings put global animator_duration_scale 0 &
- adb shell input keyevent 82 &
From: https://github.com/piotrek1543/LocalWeather/blob/master/.travis.yml
As you can see after creating an emulator instance (first line), I'm executing android-wait-for-emulator as loading the virtual device may take more than few minutes and Espresso test may start before emulator woulb in idle. Also remember to turn off animation and unlock the screen (tha last line).
Try to write similar script.
EDIT: As you're using headless server uncheck this options:
show emulator window
Running on headless build machines
If you have build slaves which are
headless (e.g. Linux servers that don't have a graphical user
interface), you can still run an Android Emulator even although, by
default, the emulator does require a graphical environment.
Just untick the "Show emulator window" configuration option in your
job configuration. This is the equivalent of using the emulator's
"-no-window" command-line option.
From: https://wiki.jenkins-ci.org/display/JENKINS/Android+Emulator+Plugin
use emulator snapshots
Similarly, snapshot support does not fully function for Android 4.0
until SDK Tools r15. An initial snapshot can be created, but
subsequently loading from that snapshot will crash the emulator
immediately. Earlier Android versions are not affected, i.e. you can
still use snapshots with Android 3.2 and earlier. Upgrading to SDK
Tools r15+ should fix this.
As a workaround, you can also uncheck "Use emulator snapshots" in any
jobs where you are seeing problems.
From: https://wiki.jenkins-ci.org/display/JENKINS/Android+Emulator+Plugin
The same solution is described on that screen:
From http://blog.zuehlke.com/en/configure-your-android-project-on-jenkins/
EDIT2: Read this article: https://www.cloudbees.com/blog/continuous-integration-mobile-apps-jenkins-android-builds and notice:
Notice that you can add some additional scripts to your emulator, try to use mine without first line.
Hope it will help

Error when launching .apk on android studio emulator

I am building an app with ai2 but I have spend hours trying to get their emulator to work, so I have exported myapp.apk and I am trying to launch it on android studio emulator.
I have followed several tuto included the answer proposed by Tarek on that thread But I am getting and error on cmd.
Here is what I tried:
run android studio as administrator, run the emulator via the AVD manager, waited until it's started.
move myapp.apk in the sdk/platform-tools folder
created a run.bat file in this folder and wrote CMD in it5. copy your desired apk to the same folder
double click on run.bat and wrote: adb install r- "myapp.apk"
Here I am getting an error on CMD: error device not found -waiting for device-. I don't understand because my emulator is running.
I have also tried to launch the emulator without starting android studio with this command emulator -avd <avd_name> [<options>] but I get this message "the system cannot find the file specified".
The error message appears because adb cannot connect to the device although it is running. This is not supposed to happen, but it does from time to time.
You can always check if adb can see the emulator by running adb devices, which should result in a list of devices it can see. If this list is empty, the device is not found. You can resolve this by adb kill-server, waiting a couple of seconds and then running adb start-server. Try running adb devices again, and the device should be listed, and the installation should succeed.
Starting an emulator can be done by running emulator #<device-name> [options]. The device name can be found by running android list avd. This will result in a list of devices of which the <device-name> will also be listed.

Android Studio error in Linux as Could Not access Package manager

Device connected: emulator-5554
Device is online: emulator-5554
Target device: AVD_for_Nexus_4 [emulator-5554]
Uploading file
local path: /home/verma/AndroidStudioProjects/Icandy1/App/build/apk/App-debug-unaligned.apk
remote path: /data/local/tmp/com.google.icandy1.app
Installing com.google.icandy1.app
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.google.icandy1.app"
Error: Could not access the Package Manager. Is the system running?
I got the above errors. Could anyone please solve this issue from Android Studio.
This is bug https://code.google.com/p/android/issues/detail?id=62113. From the bug report:
Our theory is that the IDE is trying to install the APK while the
emulator is still starting up, before it's ready to have things
installed in it. If that theory is correct, we need to have the IDE do
better detection of what state the emulator is in and have it wait
until it's ready; in the meantime, try working around it by launching
the emulator by hand, letting it get to the lockscreen and unlocking
it, and then leaving it running while you iteratively build and launch
apps from the IDE.

android studio - cannot access the package manager

When trying to run my program on an emulator I get this error:
Device connected: emulator-5554
Device is online: emulator-5554
Target device: avd1 [emulator-5554]
Uploading file
local path: C:\Users\peter\AndroidStudioProjects\MyApplicationProject\MyApplication\build\apk\MyApplication-debug-unaligned.apk
remote path: /data/local/tmp/com.bat.myapplication
Installing com.bat.myapplication
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.bat.myapplication"
Error: Could not access the Package Manager. Is the system running?
any ideas? my paths, javahome etc, seem right....
Just wait until emulator be loaded completely, once it finished then you could simply run your application.
I had the same issue once.
First run only the emulator (it takes several minutes), and once it's loaded (the UI will change), then you run your program.
As other have said, this error occurs because the emulator is still in the process of launching. An attempt to access the package manager, for the device, at this time causes an error.
It's just a simple timing issue. Here are the steps to avoid this error:
Wait until the emulator 'lock screen' is showing.
Run the 'app' again (^R in most IDE's).
Choose the running device (Should be the same emulator).
App should install without error.

Categories

Resources