简体   繁体   English

当测试的项目在libs目录中有jar时,无法构建和运行使用“ant create test-project”创建的android测试项目

[英]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. 我有一个模块,可以构建一个名为MyApp的应用程序。 I have another that builds some testcases for that app, called MyAppTests. 我有另一个为该应用程序构建一些测试用例,称为MyAppTests。 They both build their own APKs, and they both work fine from within my IDE. 他们都建立自己的APK,他们都在我的IDE中工作正常。 I'd like to build them using ant so that I can take advantage of continuous integration. 我想用ant构建它们,以便我可以利用持续集成。

Building the app module works fine. 构建应用程序模块工作正常。 I'm having difficulty getting the Test module to compile and run. 我很难让Test模块进行编译和运行。

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. 使用上一个问题中的 Christopher的提示,我使用了android create test-project -p MyAppTests -m ../MyApp -n MyAppTests来创建必要的构建文件来构建和运行我的测试项目。 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. 这似乎工作得很好(一旦我删除了为我构建的不必要的测试用例并将我的AndroidManifest.xml恢复为我在使用android create替换之前使用的那个),但我有两个问题。

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.myapp.test.LinksTest
    [javac]             assertEquals( map.get(url), m.group(1) );
    [javac]             ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:19: cannot find symbol
    [javac] symbol  : method getInstrumentation()
    [javac] location: class com.myapp.test.SafeAsyncTest
    [javac]         assertEquals("MyApp", getInstrumentation().getTargetContext().getString(com.myapp.R.string.app_name));
    [javac]                              ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:27: cannot find symbol
    [javac] symbol  : class RoboLooperThread
    [javac] location: class com.myapp.test.SafeAsyncTest
    [javac]         new RoboLooperThread() {
    [javac]             ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:65: cannot find symbol
    [javac] symbol  : method assertEquals(com.myapp.test.SafeAsyncTest.State,com.myapp.test.SafeAsyncTest.State)
    [javac] location: class com.myapp.test.SafeAsyncTest
    [javac]         assertEquals(State.TEST_SUCCESS,state[0]);
    [javac]         ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:74: cannot find symbol
    [javac] symbol  : class RoboLooperThread
    [javac] location: class com.myapp.test.SafeAsyncTest
    [javac]         new RoboLooperThread() {
    [javac]             ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:105: cannot find symbol
    [javac] symbol  : method assertEquals(com.myapp.test.SafeAsyncTest.State,com.myapp.test.SafeAsyncTest.State)
    [javac] location: class com.myapp.test.SafeAsyncTest
    [javac]         assertEquals(State.TEST_SUCCESS,state[0]);
    [javac]         ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:113: cannot find symbol
    [javac] symbol  : class RoboLooperThread
    [javac] location: class com.myapp.test.SafeAsyncTest
    [javac]         new RoboLooperThread() {
    [javac]             ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:144: cannot find symbol
    [javac] symbol  : method assertEquals(com.myapp.test.SafeAsyncTest.State,com.myapp.test.SafeAsyncTest.State)
    [javac] location: class com.myapp.test.SafeAsyncTest
    [javac]         assertEquals(State.TEST_SUCCESS,state[0]);
    [javac]         ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:154: cannot find symbol
    [javac] symbol  : class RoboLooperThread
    [javac] location: class com.myapp.test.SafeAsyncTest
    [javac]         new RoboLooperThread() {
    [javac]             ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java:187: cannot find symbol
    [javac] symbol  : method assertEquals(com.myapp.test.SafeAsyncTest.State,com.myapp.test.SafeAsyncTest.State)
    [javac] location: class com.myapp.test.SafeAsyncTest
    [javac]         assertEquals(State.TEST_SUCCESS,state[0]);
    [javac]         ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/StoriesTest.java:11: cannot access roboguice.activity.GuiceListActivity
    [javac] class file for roboguice.activity.GuiceListActivity not found
    [javac] public class StoriesTest extends ActivityUnitTestCase<Stories> {
    [javac]                                                      ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/StoriesTest.java:21: cannot access roboguice.application.GuiceApplication
    [javac] class file for roboguice.application.GuiceApplication not found
    [javac]         setApplication( new MyApplication( getInstrumentation().getTargetContext() ) );
    [javac]                         ^
    [javac] /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/StoriesTest.java:22: incompatible types
    [javac] found   : com.myapp.activity.Stories
    [javac] required: android.app.Activity
    [javac]         final Activity activity = startActivity(intent, null, null);
    [javac]                                                ^
    [javac] 39 errors
    [javac] 6 warnings

BUILD FAILED
/opt/local/android-sdk-mac/platforms/android-1.6/templates/android_rules.xml:248: Compile failed; see the compiler error output for details.

Total time: 24 seconds

That's not a hard problem to solve. 这不是一个难以解决的问题。 I'm not sure it's the right thing to do, but I copied the missing libraries (roboguice and gson) from the MyApp/libs directory to the MyAppTests/libs directory and everything seems to compile fine. 我不确定这是正确的做法,但我将缺少的库(roboguice和gson)从MyApp / libs目录复制到MyAppTests / libs目录,一切似乎都可以正常编译。

But that leads to the second problem, which I'm currently stuck on. 但这导致了第二个问题,我目前仍然坚持这个问题。 The tests compile fine but they won't run: 测试编译正常,但它们不会运行:

$ cp ../MyApp/libs/gson-r538.jar libs/

$ cp ../MyApp/libs/roboguice-1.1-SNAPSHOT.jar libs/

0 10:23 /Users/mike/Projects/myapp/android/MyAppTests $ ant run-testsBuildfile: 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] 1396 KB/s (288354 bytes in 0.201s)
     [exec]     pkg: /data/local/tmp/MyApp-debug.apk
     [exec] Success

-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 5 source files to /Users/mike/Projects/myapp/android/MyAppTests/bin/classes
    [javac] Note: /Users/mike/Projects/myapp/android/MyAppTests/src/com/myapp/test/SafeAsyncTest.java uses unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.

-dex:
     [echo] Converting compiled files and external libraries into /Users/mike/Projects/myapp/android/MyAppTests/bin/classes.dex...
     [echo]          

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

-package-debug-sign:
[apkbuilder] Creating MyAppTests-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/MyAppTests/bin/MyAppTests-debug.apk

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

run-tests:
     [echo] Running tests ...
     [exec] 
     [exec] android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests:INSTRUMENTATION_RESULT: shortMsg=Class ref in pre-verified class resolved to unexpected implementation
     [exec] INSTRUMENTATION_RESULT: longMsg=java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
     [exec] INSTRUMENTATION_CODE: 0

BUILD SUCCESSFUL
Total time: 38 seconds

Any idea what's causing the "Class ref in pre-verified class resolved to unexpected implementation" error? 知道是什么导致“预验证类中的类引用解决了意外实现”错误?

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. 如果使用Eclipse,更简单的方法是不在测试项目中包含外部库,而是将其导出到Eclipse Project设置中。 This will solve the issue 这将解决问题

在此输入图像描述

Time ago, I wrote a blog post explaining this: http://juristr.com/blog/2010/06/android-instrumentation-test/ 前一段时间,我写了一篇博文,解释了这一点: http//juristr.com/blog/2010/06/android-instrumentation-test/

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. 问题是android ant构建脚本中存在一个错误,在编译测试项目时不包含测试项目的libs目录。 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. 如果你试图通过将lib复制到测试人员项目的libs目录来解决这个问题,那么你会像我一样在运行时遇到类验证问题,正如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. 解决方案是在android的android_test_rules.xml调整compile目标,将<fileset dir="${tested.project.absolute.dir}/libs" includes="*.jar" /><classpath>指令中。

Here's the revised compile target. 这是修订后的compile目标。 By adding it to the build.xml in your TESTER project, it will take precedence over the one in android_test_rules.xml: 通过将其添加到TESTER项目中的build.xml,它将优先于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>
</target>

I had a similar problem, using Intellij IDEA (11.1.1). 我有一个类似的问题,使用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"... 我的应用程序可以很好地构建和部署到设备上,当我尝试运行时,我的测试应用程序会发出大量的dex错误:“预验证类中的类ref解决了意外实现”...

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. 这篇文章帮助我弄清楚问题是应用程序和测试应用程序.dex文件中的重复类文件,然后我手动验证。 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'. 事实证明,要从test-app中排除app类,在test-app的模块设置中,我需要将其依赖项app的范围从“compile”更改为“provided”。

I don't see the actual error message in the text above, but I think I can answer. 我没有看到上面文本中的实际错误消息,但我想我可以回答。

Generally, the warning happens because the same code appears in two different APKs. 通常,警告发生是因为相同的代码出现在两个不同的APK中。 The implementation in one APK was used for pre-verification and optimization, but the other implementation is being used during execution. 一个APK中的实现用于预验证和优化,但另一个实现在执行期间使用。 The VM detects the situation and rejects the class, because the verification and optimization were performed with a set of assumptions that are no longer true. VM检测到这种情况并拒绝该类,因为验证和优化是通过一组不再适用的假设来执行的。

The way to fix this is to ensure that there is only one implementation of a class available to the VM. 解决此问题的方法是确保VM只有一个类可用。 This may require fighting with the build scripts a bit more. 这可能需要更多地与构建脚本作斗争。

You can view the contents of an APK with "dexdump". 您可以使用“dexdump”查看APK的内容。 (There's also "dexlist", which is a bit more concise, but I don't remember if that's part of the SDK.) (还有“dexlist”,这有点简洁,但我不记得那是否是SDK的一部分。)

Like @fadden said, the same code is in two different APKs. 就像@fadden所说,相同的代码在两个不同的APK中。 This can eg happen when your test and tested project both depend on the same android library project. 这可能发生在您的测试和测试项目都依赖于相同的android库项目时。 That does not have to be direct as library projects can depend on other library projects. 这不必是直接的,因为图书馆项目可以依赖于其他图书馆项目。

The problem in the ant rules file seems to be fixed. 蚂蚁规则文件中的问题似乎是固定的。 At least in SDK tools r11. 至少在SDK工具r11中。 The compile target can be found in mail_rules.xml and not in test_rules.xml , just for those who got confused. 编译目标可以在mail_rules.xml中找到,而不是在test_rules.xml ,只适用于那些感到困惑的人。

I faced the same problem with unit tests being run by maven. 我遇到了由maven运行的单元测试的同样问题。 By removing the link to the android libraries already imported by the main project the problem was fixed. 通过删除主项目已导入的android库的链接,问题得到修复。

The scope of the project to be tested should be compile and that of the libraries should be provided. 要测试的项目范围应该是编译的,应该提供库的范围。

For more, read here 更多信息,请阅读此处

android update test-project -m <project path> -p <test project path>

ant clean

ant debug 

After running these commands, you can run ant tests. 运行这些命令后,您可以运行ant测试。 Note: If it gives JUnit related errors, please add Junit jar file in the test project libs folder. 注意:如果它给出了与JUnit相关的错误,请在测试项目libs文件夹中添加Junit jar文件。

Have you tried to include the .class files on the test apk, instead of generating new .class files from your original project? 您是否尝试在测试apk中包含.class文件,而不是从原始项目生成新的.class文件? This solved the problem in my case. 这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android Test-Project(蚂蚁构建)中引用库项目 - How can I reference library project in an Android Test-Project (ant build) android test-project构建工具v17中的VerifyError - VerifyError in android test-project build tools v17 使用ANT脚本构建Android测试项目 - Build Android Test Project using ANT script 使用Ant构建项目并运行JUnit Test Android应用程序,但Ant Junit Test失败 - Using Ant to build project and run JUnit Test Android app but Ant Junit Test fails 重置ant build.xml以运行android测试项目 - reset ant build.xml for run android test project 无法使用 ant 构建 android 测试项目 - Unable to build android test project with ant 使用命令行和Libs文件夹中的jar创建android项目 - Create android project using command line with jars in Libs folder 在没有源代码的情况下使Android测试项目可用(“make test of the test-project”) - Make Android test project available without source code (“make apk of the test-project”) 如何在公共目录(不是libs)中引用外部jar文件以使用ant构建android项目? - How do I reference external jar files in a common directory (not libs) to build android project using ant? 使用ant与库项目的Android单元测试 - Android unit test using ant with library project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM