Alex Rivera | Logout

Can't build and run an android test project created using "ant create test-project" when tested project has jars in libs directory

Asked 2010-03-18T17:32:35.553
28

I have a module that builds an app called MyApp. I have another that builds some testcases for that app, called MyAppTests. They both build their own APKs, and they both work fine from within my IDE. I'd like to build them using ant so that I can take advantage of continuous integration.

Building the app module works fine. I'm having difficulty getting the Test module to compile and run.

Using Christopher's tip from a previous question, I used android create test-project -p MyAppTests -m ../MyApp -n MyAppTests to create the necessary build files to build and run my test project. This seems to work great (once I remove an unnecessary test case that it constructed for me and revert my AndroidManifest.xml to the one I was using before it got replaced by android create), but I have two problems.

The first problem: The project doesn't compile because it's missing libraries.

$ ant run-tests
Buildfile: build.xml
    [setup] Project Target: Google APIs
    [setup] Vendor: Google Inc.
    [setup] Platform Version: 1.6
    [setup] API level: 4
    [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions.

-install-tested-project:
    [setup] Project Target: Google APIs
    [setup] Vendor: Google Inc.
    [setup] Platform Version: 1.6
    [setup] API level: 4
    [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions.

-compile-tested-if-test:

-dirs:
     [echo] Creating output directories if needed...

-resource-src:
     [echo] Generating R.java / Manifest.java from the resources...

-aidl:
     [echo] Compiling aidl files into Java classes...

compile:
    [javac] Compiling 1 source file to /Users/mike/Projects/myapp/android/MyApp/bin/classes

-dex:
     [echo] Converting compiled files and ext
Edit
Report

4 Answers

13

The problem is that there's a bug in the android ant build scripts that don't include the tested project's libs directory when compiling the tester project. If you try to get around this by copying the libs to the tester project's libs dir, you'll run into class verification problems at run time like I did, as pointed out by fadden.

The solution is to tweak the compile target originally in android's android_test_rules.xml to add <fileset dir="${tested.project.absolute.dir}/libs" includes="*.jar" /> to the <classpath> directive.

Here's the revised compile target. By adding it to the build.xml in your TESTER project, it will take precedence over the one in android_test_rules.xml:

<!-- override "compile" target in platform android_rules.xml to include tested app's external libraries -->
<target name="compile" depends="-resource-src, -aidl"
            description="Compiles project's .java files into .class files">
    <!-- If android rules are used for a test project, its classpath should include
         tested project's location -->
    <condition property="extensible.classpath"
                       value="${tested.project.absolute.dir}/bin/classes" else=".">
        <isset property="tested.project.absolute.dir" />
    </condition>
    <javac encoding="ascii" target="1.5" debug="true" extdirs=""
            destdir="${out.classes.absolute.dir}"
            bootclasspathref="android.target.classpath"
            verbose="${verbose}" classpath="${extensible.classpath}">
        <src path="${source.absolute.dir}" />
        <src path="${gen.absolute.dir}" />
        <classpath>
            <fileset dir="${tested.project.absolute.dir}/libs" includes="*.jar" />
            <fileset dir="${external.libs.absolute.dir}" includes="*.jar" />
        </classpath>
    </javac>
</
answered 2010-05-12T20:50:22.557
8

I had a similar problem, using Intellij IDEA (11.1.1). Whily my app would build and deploy to the device just fine, my test-app would spew tons of dex errors when I tried to run it: "Class ref in pre-verified class resolved to unexpected implementation"...

app
   test-app
common-libs

app depends on common-libs (and exports common-libs)
test-app depends on app

This post helped me figure out that the problem was duplicate class files in the app and test app .dex files, which I then manually verified. It turns out that to exclude the app classes from the test-app, in the module settings for test-app, I needed to change the scope of it's dependency, app, from 'compile', to 'provided'.

answered 2012-04-04T13:51:43.117
0

Have you tried to include the .class files on the test apk, instead of generating new .class files from your original project? This solved the problem in my case.

answered 2010-04-15T20:13:45.270
0

The scope of the project to be tested should be compile and that of the libraries should be provided.

For more, read here

answered 2013-04-01T09:49:30.543

Your Answer