Cannot run UIAutomator tests in latest version of Android SDK - android

I've upgraded to latest version of Android Studio (1.5) and SDK and now my UIAutomator tests, run from ant, don't work anymore (they used to, till Android Studio 1.0.x).
This is the script I use to run tests:
#echo off
cls
set ANDROID_HOME=C:\Users\xxxx\AppData\Local\Android\sdk
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_65
cmd /c ..\..\..\..\..\..\..\..\thirdparty\ant\bin\ant build
if ERRORLEVEL 1 goto end
:run
cmd /c adb push ..\..\..\..\..\..\..\app\libs\uiautomator.jar /data/local/tmp
cmd /c adb push .\bin\MyTestsUIAutomatorTest.jar /data/local/tmp
cmd /c adb shell uiautomator runtest MyTestsUIAutomatorTest.jar -c xxxxxx.haylugar.uiautomator.src.SettingsActivityUIAutomatorTest
cmd /c adb shell uiautomator runtest HayLugarUIAutomatorTest.jar -c xxxxxx.haylugar.uiautomator.src.ParkingListActivityUIAutomatorTest
cmd /c adb shell uiautomator runtest HayLugarUIAutomatorTest.jar -c xxxxx.haylugar.uiautomator.src.ParkingDetailActivityUIAutomatorTest
When building with ant, uiautomator.jar objects are not found:
src\ParkingDetailActivityUIAutomatorTest.java:3: error: package com.android.uiautomator.core does not exist
[javac] import com.android.uiautomator.core.UiObject;
Apparently, instructions have been changed according to this and this, but I followed them and they are missing something (I get an error when running tests saying that "no tests found to run"). Anyway, I just want to run my tests the same way I used to! I don't want to change configurations and waste time with the new nitty britty idea of the Android SDK team...

Fixed!
I needed to edit manually the build.xml to overwrite the compile and the dex targets of the uibuild.xml provided by the SDK to do the job correctly, copying the required libraries (uiautomator.jar and junit.jar) somewhere in my project structure:
<property name="jar.libs.dir" value="../../../../../../libs" />
<property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />
<path id="classpath">
<fileset dir="${jar.libs.absolute.dir}">
<include name="uiautomator.jar" />
<include name="junit-4.12.jar" />
</fileset>
</path>
<!-- version-tag: VERSION_TAG -->
<import file="${sdk.dir}/tools/ant/uibuild.xml" />
<!-- overwrite the compile target in uibuild.xml to include to external
jars -->
<target name="compile" depends="-build-setup, -pre-compile">
<javac encoding="${java.encoding}" source="${java.source}"
target="${java.target}" debug="true" extdirs="" includeantruntime="false"
destdir="${out.classes.absolute.dir}" bootclasspathref="project.target.class.path"
verbose="${verbose}" fork="${need.javac.fork}">
<src path="${source.absolute.dir}" />
<classpath refid="classpath" />
<compilerarg line="${java.compilerargs}" />
</javac>
</target>
<!-- overwrite the -dex target in uibuild.xml to include external jar files
into the target dex file. -->
<target name="-dex" depends="compile, -post-compile">
<dex executable="${dx}" output="${intermediate.dex.file}"
nolocals="#{nolocals}" verbose="${verbose}">
<path path="${out.classes.absolute.dir}" />
</dex>
</target>
</project>

Related

Use "android update" on cmd line to create a new project from existing code

In the documentation here
https://developer.android.com/tools/projects/projects-cmdline.html
it says that
'android update'
can be used to create a new project from existing code. The example however does not state how to do exactly that.
Given an existing project folder (basically containing a template android project), how do I create a new project with its own folder from cmd line?
Lennert, really i think 'android update' poor. I wanted copy an existing project, but renaming the package name, but i couldn't be able to do that, then I did that using script ant.
<project name="betorcs" basedir="." >
<property environment="env" />
<!-- project directories -->
<property name="project.dir" value="${basedir}/.." />
<property name="project.src" value="${project.dir}/src/main/java" />
<property name="project.res" value="${project.dir}/res" />
<!-- load configuration file -->
<loadproperties srcFile="${checklist.file}" />
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="libs/xmltask.jar"/>
<!-- altera o nome do packageName -->
<target name="-change-package">
<echo> Name the application package name on manifest.</echo>
<xmltask encoding="UTF-8" source="${project.dir}/AndroidManifest.xml" dest="${project.dir}/AndroidManifest.xml">
<copy path="/manifest/#package" property="packageName" />
<replace path="/manifest/#package" withText="${project.package}" />
</xmltask>
<echo> Refactory reference from '${packageName}' to '${project.package}'</echo>
<replace dir="${project.src}" value="${project.package}.R">
<include name="**/*.java" />
<replacefilter
token="${packageName}.R"
value="${project.package}.R" />
</replace>
<replace dir="${project.src}" value="${project.package}.BuildConfig">
<include name="**/*.java" />
<replacefilter
token="${packageName}.BuildConfig"
value="${project.package}.BuildConfig" />
</replace>
<echo> Rename resources from '${packageName}' to '${project.package}'</echo>
<replace dir="${project.res}" value="http://schemas.android.com/apk/res/${project.package}">
<include name="**/*.xml" />
<replacefilter
token="http://schemas.android.com/apk/res/${packageName}"
value="http://schemas.android.com/apk/res/${project.package}" />
</replace>
</target>
</project>
Action "update project":
Updates an Android project (must already have an AndroidManifest.xml).
Options:
-l --library : Directory of an Android library to add, relative to this
project's directory.
-p --path : The project's directory. [required]
-n --name : Project name.
-t --target : Target ID to set for the project.
-s --subprojects: Also updates any projects in sub-folders, such as test
projects.
Action "create project":
Creates a new Android project.
Options:
-n --name : Project name.
-t --target : Target ID of the new project. [required]
-p --path : The new project's directory. [required]
-k --package : Android package name for the application. [required]
-a --activity: Name of the default Activity that is created. [required]

Ant Build Failed

I am trying to build an Android Project using Apache Ant through Command Line but it is giving me Build Failed with the following error statements:
Java.io.IOException: Cannot run Program. "..\android_sdk\platform-tools\aapt.exe"
D:\MediaSync\build.xml:429: Execute failed: java.io.IOException: Cannot run program "..\android_sdk\platform-tools\aapt.exe"
I am new to this thing so i am not getting any idea how to get over this error. Searched alot about it but still scratching my head..
Anybody any idea about it then please tell me.
EDIT: Here are parts of my Buil.xml file
<!-- General SDK location -->
<property name="sdk-folder" value="D:/sdk" />
<!-- Preset tools location -->
<property name="android-platform-tools" value="${sdk-folder}/platform-tools"/>
<!-- Tools -->
<condition property="aapt" value="${android-platform-tools}/aapt.exe" else="${android-platform-tools}/aapt" >
<os family="windows"/>
</condition>
<!-- Generate the R.java file for this project's resources. -->
<target name="resource-src" depends="init, localization, replace-template-values">
<echo>Generating R.java / Manifest.java from the resources...</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-m" />
<arg value="-J" />
<arg value="${outdir-r}" />
<arg value="-M" />
<arg value="${outdir.manifest}/AndroidManifest.xml" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
</exec>
</target>
Make sure aapt.exe is installed at the correct path relative to your build.xml file (../android_sdk/platform-tools). That path is likely relative to your project, but not the build.xml.
I encountered the same problem on a 64-bit fedora platform and finally worked it out following this: http://blog.mx17.net/2012/10/25/android-on-ubuntu-ioexception-on-aapt/
It had failed because of trying to run a 32-bit program on an 64-bit architecture.
Maybe this would help.
Your error message says:
D:\MediaSync\build.xml:429: Execute failed: java.io.IOException: Cannot run program "..\android_sdk\platform-tools\aapt.exe"
while your aapt.exe is located in:
C:\android-sdk-windows\sdk\platform-tools\aapt.exe\
Obviously the build.xml tries to search D:\ for android_sdk\platform-tools\aapt.exe, which is the wrong path.
There should be a property for you to specify the path of aapt.exe, or it reads from system's PATH varibles.
To get a solution, post the lines around line 429 in build.xml. In line 429, there should be a <exec> task, be sure to include the whole content of that task (copy from <exec ...> to </exec>).
There is a bug in the intellij android studio and perhaps other IDEs that points to platform-tools for some executables/libs that now live in build-toos. A workaround for now is to make sim-links fof the necessary files.
So on linux/mac
platform-tools$ ln -s ../build-tools/android-version-blah/bin/appt appt
and
platform-tools$ ln -s ../build-tools/android-version-blah/lib lib
for Windows, install LinkExtensions and replace ln with mklink and read the help from typing mklink with no args. In Windows, the file would be appt.exe
A quick fix you can do is copy the aapt.exe and lib folder from the build-tools to the platform-tools folder.

Android built apk with ant

I want to compile a cm9 theme using ant so I have this code given in the sample theme :
<?xml version="1.0" encoding="UTF-8"?>
<project name="TemplateTheme" default="help">
<!-- ADD YOUR ANDROID SDK PATH TO THIS FILE -->
<loadproperties srcFile="local.properties" />
<!-- The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant specific build properties.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="ant.properties" />
<!-- DON'T TOUCH THIS ONE -->
<loadproperties srcFile="default.properties" />
<!-- ADD YOUR KEYSTORE PROPERTIES HERE -->
<loadproperties srcFile="key.properties" />
<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project'"
unless="sdk.dir"
/>
<!-- Import the actual build file.
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.
***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />
<target name="-package-resources">
<echo>Optimizing theme resources</echo>
<exec executable="./prepare-resources" failonerror="true" />
<echo>Packaging theme resources</echo>
<exec executable="./aapt" failonerror="true">
<arg value="package" />
<arg value="-f" />
<arg value="-M" />
<arg path="AndroidManifest.xml" />
<arg value="-I" />
<arg path="${android.jar}" />
<arg value="-F" />
<arg path="${out.absolute.dir}/${resource.package.file.name}" />
<arg value="-S" />
<arg path="res-final" />
<arg value="-x" />
<arg value="10" />
</exec>
<echo>Cleaning up resources</echo>
<exec executable="./prepare-resources" failonerror="true">
<arg value="clean" />
</exec>
</target>
</project>
but when I execute I get the error :
cannnot run program ".\prepare-resources":Create Process Error=193, %1 is not a valid win32 Application
prepare-resources:
#!/bin/sh
if [ "$*" == "clean" ]; then
rm -rf res-final/
else
rm -rf res-final/
rsync -a res/ res-final/
echo "Themeing quasi-clone packages..."
rsync res-final/xml/com_cyanogenmod_trebuchet.xml res-final/xml/com_anddoes_launcher.xml
rsync res-final/xml/com_cyanogenmod_trebuchet.xml res-final/xml/com_teslacoilsw_launcher.xml
echo "Running pngcrush..."
for png in `find res-final/ -type f -name \*.png`; do
pngcrush -q $png $png.out 2>/dev/null >/dev/null && mv $png.out $png
done
fi
prepare-resources" is a shell script, not an executable. To be able to execute it, install cygwin, a UNIX-like environment that includes lots of software including shells capable of executing the script.
This page describes what packages you need and how to setup yourself.

Hudson Android multi-configuration test project can't find test results - "No test report files were found. Configuration error?"

I have android test project that i've integrated into the Hudson. I have created a free-style software project with next settings :
1. Source Code Management - custom workspace
2. Run an Android emulator during build :
* Android OS Version: 4.0.3
* Screen density: 240
* Screen resolution: WVGA
* Device locale: en_US
* SD card size: 16M
3. Invoke Ant:
Target: all clean emma debug install test
Build File: workspace/Project/build.xml
4. Post-build Actions
* Archive the artifacts: **/*test-TEST.xml
* Publish JUnit test result report: **/*test-TEST.xml
* Record Emma coverage report: **/coverage.xml
* Record fingerprints of files to track usage: **/*test-TEST.xml
Here's build.xml file part that covers my ant target:
<!-- version-tag: custom -->
<target name="test" depends="-test-project-check"
description="Runs tests from the package defined in test.package property">
<property name="tested.project.absolute.dir" location="${tested.project.dir}" />
<property name="test.runner" value="android.test.InstrumentationTestRunner" />
<!-- Application package of the tested project extracted from its manifest file -->
<xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
expression="/manifest/#package" output="tested.manifest.package" />
<xpath input="AndroidManifest.xml"
expression="/manifest/#package" output="manifest.package" />
<property name="emma.dump.file"
value="/data/data/${tested.manifest.package}/coverage.ec" />
<if condition="${emma.enabled}">
<then>
<echo>WARNING: Code Coverage is currently only supported on the emulator and rooted devices.</echo>
<run-tests-helper emma.enabled="true">
<extra-instrument-args>
<arg value="-e" />
<arg value="coverageFile" />
<arg value="${emma.dump.file}" />
</extra-instrument-args>
</run-tests-helper>
<echo>Downloading coverage file into project directory...</echo>
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}" />
<arg value="pull" />
<arg value="${emma.dump.file}" />
<arg value="coverage.ec" />
</exec>
<echo>Extracting coverage report...</echo>
<emma>
<report sourcepath="${tested.project.absolute.dir}/${source.dir}"
verbosity="${verbosity}">
<!-- TODO: report.dir or something like should be introduced if necessary -->
<infileset dir=".">
<include name="coverage.ec" />
<include name="coverage.em" />
</infileset>
<!-- TODO: reports in other, indicated by user formats -->
<html outfile="${basedir}/coverage/coverage.html" />
<xml outfile="${basedir}/coverage/coverage.xml" />
</report>
</emma>
<echo>Cleaning up temporary files...</echo>
<delete file="coverage.ec" />
<delete file="coverage.em" />
<echo>Saving the report file in ${basedir}/coverage/coverage.html</echo>
</then>
<else>
<run-tests-helper />
</else>
</if>
<mkdir dir="${basedir}/junit-results" />
<echo>Base dir is ${basedir}</echo>
<exec executable="${adb}" failonerror="true" dir="${basedir}/junit-results">
<arg line="${adb.device.arg}" />
<arg value="pull" />
<arg value="/data/data/${tested.manifest.package}/files/" />
</exec>
</target>
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
Now, this project works fine and i get both emma coverage and test results, but when i configured multi-configuration project with the same options, except for android emulator i set different variables with os, density and screen resolution, i got stuck with one error -
test:
[echo] WARNING: Code Coverage is currently only supported on the emulator and rooted devices.
[echo] Running tests ...
[exec]
[exec] com.mobclix.android.externaltestharness.test.UnitActivityTest:.
[exec] Test results for PolideaInstrumentationTestRunner=.
[exec] Time: 25.122
[exec]
[exec] OK (1 test)
[exec]
[exec]
[exec] Generated code coverage data to /data/data/com.mobclix.android.externaltestharness/coverage.ec
[echo] Downloading coverage file into project directory...
[exec] 355 KB/s (2043 bytes in 0.005s)
[echo] Extracting coverage report...
[echo] Cleaning up temporary files...
[delete] Deleting: /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage.ec
[delete] Deleting: /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage.em
[echo] Saving the report file in /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage/coverage.html
[echo] Base dir is /Users/biercoff/Work/Workspace/HarnessExternalTest
[exec] pull: building file list...
[exec] pull: /data/data/com.mobclix.android.externaltestharness/files/com.mobclix.android.externaltestharness.test-TEST.xml -> ./com.mobclix.android.externaltestharness.test-TEST.xml
[exec] 1 file pulled. 0 files skipped.
[exec] 40 KB/s (499 bytes in 0.012s)
BUILD SUCCESSFUL
Total time: 1 minute 20 seconds
$ /Users/biercoff/.hudson/tools/android-sdk/platform-tools/adb disconnect localhost:53830
[android] Stopping Android emulator
[android] Archiving emulator log
$ /Users/biercoff/.hudson/tools/android-sdk/platform-tools/adb kill-server
Recording fingerprints
Recording test results
No test report files were found. Configuration error?
Archiving artifacts
Description set:
Emma: looking for coverage reports in the provided path: /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage/coverage.xml
Emma: found 1 report files:
/Users/biercoff/Work/Workspace/HarnessExternalTest/coverage/coverage.xml
Emma: stored 1 report files in the build folder: /Users/biercoff/.hudson/jobs/MulitTestHarnessProject/configurations/axis-density/240/axis-os/4.0.3/axis-resolution/WVGA/builds/2012-05-18_17-26-05/emma
Emma: Coverage: Classes 21/39 (54%). Methods 40/128 (31%). Blocks 833/2928 (28%). Lines 154.5/528 (29%).
[DEBUG] Skipping watched dependency update for build: MulitTestHarnessProject/density=240,os=4.0.3,resolution
For emma coverage i had to change path in Hudson into /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage/coverage.xml to make it work,
but when i tried to do the same thing for Junit test results, Hudson refused to "see" it. File is in the project folder under junit-results folder, but everytime i run new build, i get the same No test report files were found. Configuration error? error message. Tried many variations, but no luck. I'm missing probably something really tiny. Could somebody help me, please?
Hudson searches for jUnit test results relative to its workspace. And you can't specify an absolute path (I've been using Hudson/Jenkins for more than a year and still not sure why not). So in your case it is looking only at things that are deeper in the directory level than the workspace. Now, for a multi-congiguration project the workspace does not start at the top level, but deeper - it includes axes names (e.g.: workspace_root/AXIS1_NAME/AXIS1_VAL...). But your tests are located up the directory tree from that workspace. You have to copy them in a build step into ${WORKSPACE} and then Hudson will be able to publish them.

How to pass arguments to aapt when building android apk?

I'm building an apk using ant on Linux (not using Eclipse), and I'm trying to find an easy way to switch between compressing assets and not, in order to keep my huge database uncompressed for < 2.3. As per this article:
http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-apps/
I see that it's possible to do this by specifying something like aapt -0 db, but I can't find information on how to edit the way ant interacts with aapt. Or do I have to run aapt by itself? What do I do?
It's Android's build.xml (you'll find this under your SDK, tools and ant) that executes AAPT when building.
Find your build.xml and replace the "-package-resources" target with the target below. I'm pretty sure (not able to test it at the moment) you'd be able to swith compression on and off with either "-Dcompression.disabled=set" when build with ANT or "compression.disabled=set" in your .properties-file.
<target name="-package-resources" depends="-crunch">
<!-- only package resources if *not* a library project -->
<do-only-if-not-library elseText="Library project: do not package resources..." >
<aapt executable="${aapt}"
command="package"
versioncode="${version.code}"
versionname="${version.name}"
debug="${build.is.packaging.debug}"
manifest="AndroidManifest.xml"
assets="${asset.absolute.dir}"
androidjar="${android.jar}"
apkfolder="${out.absolute.dir}"
nocrunch="${build.packaging.nocrunch}"
resourcefilename="${resource.package.file.name}"
resourcefilter="${aapt.resource.filter}"
projectLibrariesResName="project.libraries.res"
projectLibrariesPackageName="project.libraries.package"
previousBuildType="${build.last.target}"
buildType="${build.target}">
<res path="${out.res.absolute.dir}" />
<res path="${resource.absolute.dir}" />
<if>
<condition>
<isset property="compression.disabled" />
</condition>
<then>
<nocompress/>
</then>
</if>
</aapt>
</do-only-if-not-library>
</target>

Categories

Resources