Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

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

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 external libraries into /Users/mike/Projects/myapp/android/MyApp/bin/classes.dex...
     [echo]          

-package-resources:
     [echo] Packaging resources
 [aaptexec] Creating full resource package...

-package-debug-sign:
[apkbuilder] Creating MyApp-debug-unaligned.apk and signing it with a debug key...
[apkbuilder] Using keystore: /Users/mike/.android/debug.keystore

debug:
     [echo] Running zip align on final apk...
     [echo] Debug Package: /Users/mike/Projects/myapp/android/MyApp/bin/MyApp-debug.apk

install:
     [echo] Installing /Users/mike/Projects/myapp/android/MyApp/bin/MyApp-debug.apk onto default emulator or device...
     [exec] 1567 KB/s (288354 bytes in 0.179s)
     [exec]     pkg: /data/local/tmp/MyApp-debug.apk
     [exec] Success

-compile-tested-if-test:

-dirs:
     [echo] Creating output directories if needed...
    [mkdir] Created dir: /Users/mike/Projects/myapp/android/MyAppTests/gen
    [mkdir] Created dir: /Users/mike/Projects/myapp/android/MyAppTests/bin
    [mkdir] Created dir: /Users/mike/Projects/myapp/android/MyAppTests/bin/classes

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

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

compile:
    [javac] Compiling 5 source files to /Users/mike/Projects/myapp/android/MyAppTests/bin/classes
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/GsonTest.java:4: package roboguice.test does not exist
    [javac] import roboguice.test.RoboUnitTestCase;
    [javac]                      ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/GsonTest.java:8: package com.google.gson does not exist
    [javac] import com.google.gson.JsonElement;
    [javac]                       ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/GsonTest.java:9: package com.google.gson does not exist
    [javac] import com.google.gson.JsonParser;
    [javac]                       ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/GsonTest.java:11: cannot find symbol
    [javac] symbol: class RoboUnitTestCase
    [javac] public class GsonTest extends RoboUnitTestCase<MyApplication> {
    [javac]                               ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:6: package roboguice.test does not exist
    [javac] import roboguice.test.RoboUnitTestCase;
    [javac]                      ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:7: package roboguice.util does not exist
    [javac] import roboguice.util.RoboLooperThread;
    [javac]                      ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:11: package com.google.gson does not exist
    [javac] import com.google.gson.JsonObject;
    [javac]                       ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:15: cannot find symbol
    [javac] symbol: class RoboUnitTestCase
    [javac] public class HttpTest extends RoboUnitTestCase<MyApplication> {
    [javac]                               ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/LinksTest.java:4: package roboguice.test does not exist
    [javac] import roboguice.test.RoboUnitTestCase;
    [javac]                      ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/LinksTest.java:12: cannot find symbol
    [javac] symbol: class RoboUnitTestCase
    [javac] public class LinksTest extends RoboUnitTestCase<MyApplication> {
    [javac]                                ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:4: package roboguice.test does not exist
    [javac] import roboguice.test.RoboUnitTestCase;
    [javac]                      ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:5: package roboguice.util does not exist
    [javac] import roboguice.util.RoboAsyncTask;
    [javac]                      ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:6: package roboguice.util does not exist
    [javac] import roboguice.util.RoboLooperThread;
    [javac]                      ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:12: cannot find symbol
    [javac] symbol: class RoboUnitTestCase
    [javac] public class SafeAsyncTest extends RoboUnitTestCase<MyApplication> {
    [javac]                                    ^
    [javac] /Users/mike/Projects/myapp/android/MyApp/bin/classes/com/myapp/activity/Stories.class: warning: Cannot find annotation method 'value()' in type 'roboguice.inject.InjectResource': class file for roboguice.inject.InjectResource not found
    [javac] /Users/mike/Projects/myapp/android/MyApp/bin/classes/com/myapp/activity/Stories.class: warning: Cannot find annotation method 'value()' in type 'roboguice.inject.InjectResource'
    [javac] /Users/mike/Projects/myapp/android/MyApp/bin/classes/com/myapp/activity/Stories.class: warning: Cannot find annotation method 'value()' in type 'roboguice.inject.InjectView': class file for roboguice.inject.InjectView not found
    [javac] /Users/mike/Projects/myapp/android/MyApp/bin/classes/com/myapp/activity/Stories.class: warning: Cannot find annotation method 'value()' in type 'roboguice.inject.InjectView'
    [javac] /Users/mike/Projects/myapp/android/MyApp/bin/classes/com/myapp/activity/Stories.class: warning: Cannot find annotation method 'value()' in type 'roboguice.inject.InjectView'
    [javac] /Users/mike/Projects/myapp/android/MyApp/bin/classes/com/myapp/activity/Stories.class: warning: Cannot find annotation method 'value()' in type 'roboguice.inject.InjectView'
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/GsonTest.java:15: cannot find symbol
    [javac] symbol  : class JsonParser
    [javac] location: class com.myapp.test.GsonTest
    [javac]         final JsonParser parser = new JsonParser();
    [javac]               ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/GsonTest.java:15: cannot find symbol
    [javac] symbol  : class JsonParser
    [javac] location: class com.myapp.test.GsonTest
    [javac]         final JsonParser parser = new JsonParser();
    [javac]                                       ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/GsonTest.java:18: cannot find symbol
    [javac] symbol  : class JsonElement
    [javac] location: class com.myapp.test.GsonTest
    [javac]         final JsonElement e = parser.parse(s);
    [javac]               ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/GsonTest.java:20: cannot find symbol
    [javac] symbol  : class JsonElement
    [javac] location: class com.myapp.test.GsonTest
    [javac]         final JsonElement e2 = parser.parse(s2);
    [javac]               ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:19: cannot find symbol
    [javac] symbol  : method getInstrumentation()
    [javac] location: class com.myapp.test.HttpTest
    [javac]         assertEquals("MyApp", getInstrumentation().getTargetContext().getResources().getString(com.myapp.R.string.app_name));
    [javac]                              ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:62: cannot find symbol
    [javac] symbol  : class RoboLooperThread
    [javac] location: class com.myapp.test.HttpTest
    [javac]         new RoboLooperThread() {
    [javac]             ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:82: cannot find symbol
    [javac] symbol  : method assertTrue(java.lang.String,boolean)
    [javac] location: class com.myapp.test.HttpTest
    [javac]         assertTrue(result[0], result[0].contains("Search"));
    [javac]         ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:87: cannot find symbol
    [javac] symbol  : class JsonObject
    [javac] location: class com.myapp.test.HttpTest
    [javac]         final JsonObject[] result = {null};
    [javac]               ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:90: cannot find symbol
    [javac] symbol  : class RoboLooperThread
    [javac] location: class com.myapp.test.HttpTest
    [javac]         new RoboLooperThread() {
    [javac]             ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:117: cannot find symbol
    [javac] symbol  : class JsonObject
    [javac] location: class com.myapp.test.HttpTest
    [javac]         final JsonObject[] result = {null};
    [javac]               ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/HttpTest.java:120: cannot find symbol
    [javac] symbol  : class RoboLooperThread
    [javac] location: class com.myapp.test.HttpTest
    [javac]         new RoboLooperThread() {
    [javac]             ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/LinksTest.java:27: cannot find symbol
    [javac] symbol  : method assertTrue(boolean)
    [javac] location: class com.myapp.test.LinksTest
    [javac]             assertTrue(m.matches());
    [javac]             ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/LinksTest.java:28: cannot find symbol
    [javac] symbol  : method assertEquals(java.lang.String,java.lang.String)
    [javac] location: class com.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you use Eclipse, a simpler approach is to not include your external library in the test project, but rather export it in the Eclipse Project settings. This will solve the issue

enter image description here

Time ago, I wrote a blog post explaining this: http://juristr.com/blog/2010/06/android-instrumentation-test/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...