Build UiAutomator 2.0 from command line - android

I want to build, install and run the tests of a UiAutomator project through the command line.
In the previous version I was doing:
android create uitest-project -n <project_name> -p <project_path> -t <target_id>
ant build
to build
and then
adb push <jar_path> /data/local/tmp
to install and finally
adb shell uiautomator runtest <jar_name> -c <main_class>
However, right now I'm stuck in the building part.
The result is
-check-env:
[checkenv] Android SDK Tools Revision 24.1.2
[checkenv] Installed at C:\Android
-build-setup:
[getbuildtools] Using latest Build Tools: 22.0.0
[echo] Resolving Build Target for AndroidExplorerTester...
[getuitarget] Project Target: Android 5.0.1
[getuitarget] API level: 21
[echo] ----------
[echo] Creating output directories if needed...
-pre-compile:
compile:
-post-compile:
-dex:
[dex] input: <test_path>\bin\classes
[dex] Converting compiled files and external libraries into <test_path>\bin\classes.dex...
[dx] no classfiles specified
BUILD FAILED
C:\Android\tools\ant\uibuild.xml:198: null returned: 1
Total time: 1 second
I don't know if there is a better way to do it now since the new version of UiAutomator.
Note: I don't know if it matters but I was using Eclipse before and now I'm using IntelliJ (Android Studio if you prefer lol)

Here is one more way for those, who don't want to move to gradle and wish to stay with ant. Btw, main reason, why old way doesn't work, is moving of uiautomator starting from its 2.0 version from standalone test runner for jars to standard android 'am instrument' test runner for apps. This move has only one 'contra'. Now test projects should be bound to a definite target app (see workaround in the first step). So here is a plan.
First of all, you should have a target project, for which your test is designed. In fact it can be an empty app, which will not be shown at all, neither in apps menu, nor during testing. I've managed to create one in Eclipse, without creating any activity in wizard. To make ant's build.xml run:
android update project --target 0 --path %path_to_empty_app%
For more information about android tool see http: //developer. android. com/ tools/ projects/ projects-cmdline .html
Note: you may want to give necessary permissions to your target app, which will be spread to test app. Now test is not run with shell user permissions.
Second step is creating a test project. As I've mentioned, uiautomator is now integrated in standard android testing scheme. Thus, it uses a standard command for creating test apps:
android create test-project -m %path_to_target_app% -n %test_app_name% -p %path_to_test_app%
A usual app structure will be created in %path_to_test_app%, including ant's build.xml
For more information see http://developer.android.com/tools/testing/testing_otheride.html
Third: copy uiautomator classes jar to test app libs. The jar can be extracted from *.aar archive situated in SDK in \extras\android\m2repository\com\android\support\test\uiautomator\uiautomator-v18\2.1.0 or similar.
Fourth: put your test class *.java to test app src folder. Note the following changes in uiautomator here:
package is renamed from com.android.uiautomator to
android.support.test.uiautomator
UiAutomatorTestCase class is
left for compatibility, but is deprecated; extend your test class
from InstrumentationTestCase, to get UiDevice instance, use
UiDevice.getInstance(getInstrumentation())
Fifth: install and run your test. Simple way is the following:
cd %path_to_test_app%
:: Here 'ant instrument install' builds and installs both target and test apps.
ant instrument install
ant test
or the last line can be modified to
adb shell am instrument -w %target_app_full_name%.tests/android.test.InstrumentationTestRunner
For more information see http://developer.android.com/reference/android/test/InstrumentationTestRunner.html

Well I finally figured it out.
From the command line, in the main folder of the project (where you can find gradlew.bat file) run the following commands
build:
.\gradlew.bat assembleDebug
install on device:
.\gradlew.bat installDebug
(if you want Release version just replace Debug for Release, I didn't try it but the options exist and so I suppose they work)
run:
.\gradlew.bat connectedCheck
If you want to know other options you may have run
.\gradlew.bat tasks
Extra information
To do it programmatically (Java) use Runtime.getRuntime().exec(String command, String[] envp, File dir). For instance,
Runtime.getRuntime().exec(shell_command + " <path_to_test_folder>\gradlew.bat assembleDebug", null, new File(<path_to_test_project>));
Where shell_command depends on the operating system (the command for the command line):
- Windows: cmd /C
- Unix-based: /bin/sh -c

Related

Unable to load the main project's project.properties in android studio while creating a test project using terminal

I am trying to create a Test Project for a simple application project called 'SimpleMaths' in android studio. As per the android documentation for Testing from Other IDEs i used a command android create test-project -m <main_path> -n <project_name> -p <test_path> to create a test project. Specific to my project i have following project directory and command used:
Project Directory:
F:\Projects\SimpleMaths
command used to create SimpleMathsTest:
android create test-project -m ../main -n SimpleMathsTest -p F:\Projects\SimpleMaths\app\src\main
After this command is run:
In windows cmd:
C:\Program Files (x86)\Android\android-studio\sdk\tools>android create test-project -m ../main -n SimpleMathsTest -p F:\Projects\SimpleMaths\app\src\main
Found main project package: simplemaths.app
Found main project activity: .MainActivity
Error: Unable to load the main project's project.properties
In Android Studio's terminal
F:\Projects\SimpleMaths\app\src>android create test-project -m ../main -p main
Found main project package: simplemaths.app
Found main project activity: .MainActivity
Error: Unable to load the main project's project.properties
What could be the problem and how to generate such test projects with proper structure in android studio?
From Gradle Plugin user Guide-Testing
Building a test application is integrated into the application project.
So see the documentation above for more informations.
In fact, you don't have to create any test project under Android Studio. They are automatically "included" in you application project.
So what you have to do is to create a test folder, create your tests in and run them (sounds easy, but it took me more time then I thought it would because of all documetation you can find).
I'm very new in Andtoid testing so I can't respond to this:
What could be the problem
but here what I have done which worked for me:
Under my src/main/java/ I created
androidTest/java package.
Created my tests in.
in Run->Edit configurations I created an "Android Tests" configuration where I informed my Module and chose location of my tests.
In first time that didn't work, so I ran one of them manually (right click on it->run) and the reran my "Android Tests configuration" and it worked fine for me.

How to create Android App with Cordova 3.4

Since yesterday, I have some problems with Cordova and Android. I can not create an functional Android App. iOS works.
In detail: I use a Mac (MacOS X 10.8.5) and have installed ADT 22.6.2 (with API 17/18/19) and added the sdk-tools to my PATH.
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/adt/sdk/tools:/Applications /adt/sdk/platform-tools:/usr/local/MacGPG2/bin
My configuration:
$ node -v
v0.10.26
$ cordova -v
3.4.1-0.1.0
$ ant -version
Apache Ant(TM) version 1.8.2 compiled on June 20 2012
My attempt to create an Cordova App:
$ cordova create XY com.wb.XY XY
Creating a new cordova project with name "XY" and id "com.wb.XY" at location "/.../XY"
$ cd XY
XY wb$ cordova platform add ios
Creating ios project...
XY wb$ cordova platform add android
Creating android project...
Creating Cordova project for the Android platform:
Path: platforms/android
Package: com.wb.XY
Name: XY
Android target: android-19
Copying template files...
Running: android update project --subprojects --path "platforms/android" --target android-19 --library "CordovaLib"
Resolved location of library project to: /.../XY/platforms/android/CordovaLib
Updated and renamed default.properties to project.properties
Updated local.properties
No project name specified, using Activity name 'XY'.
If you wish to change it, edit the first line of build.xml.
Added file platforms/android/build.xml
Added file platforms/android/proguard-project.txt
Updated project.properties
Updated local.properties
No project name specified, using project folder name 'CordovaLib'.
If you wish to change it, edit the first line of build.xml.
Added file platforms/android/CordovaLib/build.xml
Added file platforms/android/CordovaLib/proguard-project.txt
Project successfully created.
XY wb$ cordova platform list
Installed platforms: android 3.4.0, ios 3.4.1
Available platforms: amazon-fireos, blackberry10, firefoxos
XY wb$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser.git
Fetching plugin "https://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser.git" via git clone
Installing "org.apache.cordova.inappbrowser" for android
Installing "org.apache.cordova.inappbrowser" for ios
XY wb$
What is going wrong? There is no directory „gen“.
If I try to import the project into ADT/Eclipse, I got the message:
Errors occurred during the build.
Errors running builder 'Android Resource Manager' on project 'XY'.
Errors running builder 'Android Pre Compiler' on project 'XY'.
Path must include project and resource name: /XY
and
/XY/gen already exists but is not a source folder. Convert to a source folder or rename it.
Eclipse generates a folder called „gen“, but it is empty.
EDIT: Here is what I tried based on the input given in answers and comments:
I have installed Node.js and Cordova 3.4:
$ node -v
v0.10.26
$ cordova -v
3.4.1-0.1.0
$ ant -version
Apache Ant(TM) version 1.8.2 compiled on June 20 2012
I have done this as you described with CLI.
The Android SDK is in the system path:
$ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/adt/sdk/tools:/Applications/adt/sdk/platform-tools:/usr/local/MacGPG2/bin
Everything seems to be all right. Do you see any mistake? Do I need Apache Ant?
If I try to add android (without sudo), I have got the output shown in my question. Trying this as admin (with sudo), the result is the same.
With
$ cordova emulate android
I get:
...
BUILD SUCCESSFUL
Total time: 5 seconds
WARNING : no emulator specified, defaulting to Android_Tablet
Waiting for emulator...
2014-04-26 14:16:53.848 emulator64-arm[84081:f07] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found. Did find:
/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
emulator64-arm: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.
Booting up emulator (this may take a while)........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
It takes a very long time . . . after 15 minutes I broke off. The log at Eclipse shows always returning the same messages.
But with
$ cordova run android
I am able to install and launch the app on my Android-device. But if I try to import the project into Eclipse, the file XY.java is shown with many errors.
Do you have any idea what could be the problem?
I am still able to work with my existing apps (iOS and Android), but I can not create a new one for Android.
I am sharing with you the steps to create android PhoneGap app with cordova 3.4 version :-
1) Download and install Node.js.
2) Run this command on your terminal :-
$ sudo npm install -g cordova
3) Then create your project using following command :-
$ cordova create hello com.example.hello HelloWorld
4) Then before adding any platform , run the following command....Replace the path of android sdk with your system path :-
$ export PATH=${PATH}:/Users/taruna/Documents/adt-bundle-mac-x86_64-20131030/sdk/platform-tools:/Users/taruna/Documents/adt-bundle-mac-x86_64-20131030/sdk/tools
5) Now add your platform using following command :-
$ sudo cordova platform add android
6) Now you can successfully run your project on emulator using following command :-
$ cordova emulate android
And its done now.
`

Integrating both Google Maps API and Nokia X HERE reference libraries for Google Maps API in one APK

I’m looking at the integrating Google Maps API and Nokia X HERE reference libraries for Google Maps API to one APK – docs here http://developer.nokia.com/resources/library/nokia-x/here-maps/one-apk-with-here-maps-and-google-maps.html
I’m following the steps in the video tutorial on the doc page and running into the following issues when trying to add the support library to the project. Any clue how to continue?
[2014-02-25 16:06:45 - Dex Loader] Unable to execute dex: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
[2014-02-25 16:06:45 - MapsDemo] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
Looks like you have support library added to the project twice. I think you have added it as an external jar and it's also in your projects /libs.
If you are using Eclipse, right click the project and navigate to Java Build Path -> Libraries and you might see android-support-v4.jar listed there twice: As an external jar and under Android Private Libraries. Remove the one that is not under Android Private Libraries.
Shameless plug: Running the Github demos that page mentions is a lot easier though:
# clone HERE samples
$ git clone https://github.com/nokia-developer/nokia-x-here-maps-samples
$ cd nokia-x-here-maps-samples
# init submodules
$ git submodule update –init
# launch emulator (you need to create one first)
$ emulator -avd AVD_for_Nokia_X
# build and install debug version of MapsV1OneApk
$ cd MapsV1OneApk
$ ./gradlew installDebug # OS X/Linux
$ gradlew installDebug # Windows
# build and install debug version of MapsV2OneApk
$ cd ..
$ cd MapsV2OneApk
$ ./gradlew installDebug # OS X/Linux
$ gradlew installDebug # Windows
NOTE You will need to modify AndroidManifest.xml for the above demos and insert working HERE/Google Maps keys in order to actually see the maps.
You could try checking which library is included multiple times. I would try first unchecking the android private libraries.

How to build a JAR file using ant for uiautomator test project

I have been trying to get my head around using uiautomator and I understand creating the test cases. I tried the first two steps of building the JAR according the the Android developer guide for UI Testing (http://developer.android.com/tools/testing/testing_ui.html) and that worked fine, but I am unsure on how to follow the next steps and building the JAR file with ant. Please can anyone help me with this issue.
Here are the steps that I have done so far:
<android-sdk>/tools/android create uitest-project -n <name> -t 1 -p <path>
set ANDROID_HOME=<path_to_your_sdk>
This is the step that I am stuck on:
ant build
The error I get when I use the command prompt is:
'ant' is not recognized as an internal or external command, operable program or batch file.
In the terminal run
which ant
It seems you do not have ant installed. If no location like /usr/bin/ant shows up after you run the command above, use this to install it:
sudo apt-get install ant
Try again after. It should work.
If you're using ADT, I recommend downloading Apache ant and placing it within the sdk/tools/ant folder. Then change your PATH variable to direct to the ant bin folder.
you have this error because you wrote "ant build" from incorrect path.
I did (this command generate build.xml, local.properties, project.properties files):
<android-sdk>/tools/android create uitest-project -n <name> -t 1 -p <path>
I changed in build.xml first line like:
<project name="name" default="build">
after that I run ant from eclipse. Build was success.
Have you installed ant? If not then do it from this site: http://ant.apache.org/manual/install.html
If yes then follow these steps:
Navigate to your project directory and means (go to bin of your project)
Using command window execute the function: ant build
Ensure that your default JAVA_HOME points to JDK installation, not the JRE, and check if your compiler is in the PATH. Try to type “javac” in the command prompt. If javac is not found, then you should put your bin directory in the PATH.
For example, in Windows:
SET PATH=c:/jdk1.5.0_07/bin;%PATH%
This site might help further: http://looksok.wordpress.com/2014/02/08/uiautomator-in-eclipse/
If you have ADT with Eclipse then you can find ant in the plugins directory of Eclipse.

running the NDK native-activity sample from a command-line IDE

similar questions have been asked:
How to run NDK samples?
Making Android NDK apps with NativeActivity?
I've got the command-line documentation down to the point where I can create an empty Android project, I do not know how to build-install-run the NDK examples, particularly the NativeActivity one, in a non-Eclipse build environment (Ubuntu 10.0.04).
So my questions are
does the NativeActivity example "just work" or do you have to do something besides just $adb -d shell "am start -a android.intent.action.MAIN -n com.example.nativeActivity"?
How do you build, install, run the NativeActivity example?
I can't answer about this specific example. But here's the general process for building and installing an Android application which uses the NDK.
cd to the root of its source code.
Run ndk-build. This builds the native code, and should result in some .so files being put into the libs directory.
android update project --path . --name something
ant debug (or similar). This will build the Java code and create an .apk. Crucially, the build process will pick up the .so files left within the libs directory and include them into the .apk.
adb install bin/name-of-project.apk
Then launch as normal using the Android GUI or using an am start command such as you give.

Categories

Resources