当测试项目在 libs 目录中有 jar 时,无法构建和运行使用“ant create test-project”创建的 android 测试项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2472059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't build and run an android test project created using "ant create test-project" when tested project has jars in libs directory
提问by emmby
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.
我有一个模块可以构建一个名为 MyApp 的应用程序。我有另一个为该应用程序构建一些测试用例,称为 MyAppTests。他们都构建了自己的 APK,并且在我的 IDE 中都可以正常工作。我想使用 ant 构建它们,以便我可以利用持续集成。
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.
使用 Christopher 在上一个问题中的提示,我过去常常android create test-project -p MyAppTests -m ../MyApp -n MyAppTests
创建必要的构建文件来构建和运行我的测试项目。这似乎工作得很好(一旦我删除了它为我构建的不必要的测试用例并将我的 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?
知道是什么导致了“预验证类中的类引用解析为意外实现”错误?
采纳答案by emmby
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.
问题是android ant 构建脚本存在bug,编译tester 项目时不包含被测项目的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.
解决方案是调整compile
最初在 android 中的目标android_test_rules.xml
以添加<fileset dir="${tested.project.absolute.dir}/libs" includes="*.jar" />
到<classpath>
指令中。
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:
这是修改后的compile
目标。通过将其添加到 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>
回答by Juri
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
如果您使用 Eclipse,一个更简单的方法是不在测试项目中包含您的外部库,而是将它导出到 Eclipse 项目设置中。这将解决问题
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/
回答by ToddG
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"...
我有一个类似的问题,使用 Intellij IDEA (11.1.1)。虽然我的应用程序可以很好地构建和部署到设备,但当我尝试运行它时,我的测试应用程序会发出大量的 dex 错误:“预验证类中的类引用解析为意外实现”...
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'.
这篇文章帮助我找出问题在于应用程序和测试应用程序 .dex 文件中的重复类文件,然后我手动验证了这些文件。事实证明,要从 test-app 中排除 app 类,在 test-app 的模块设置中,我需要将其依赖项 app 的范围从“编译”更改为“提供”。
回答by fadden
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. The implementation in one APK was used for pre-verification and optimization, but the other implementation is being used during execution. 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.
通常,出现警告是因为相同的代码出现在两个不同的 APK 中。一个 APK 中的实现用于预验证和优化,但另一个实现在执行过程中使用。VM 检测到这种情况并拒绝该类,因为验证和优化是使用一组不再正确的假设执行的。
The way to fix this is to ensure that there is only one implementation of a class available to the VM. This may require fighting with the build scripts a bit more.
解决这个问题的方法是确保只有一个类的实现可供 VM 使用。这可能需要更多地使用构建脚本。
You can view the contents of an APK with "dexdump". (There's also "dexlist", which is a bit more concise, but I don't remember if that's part of the SDK.)
您可以使用“dexdump”查看 APK 的内容。(还有“dexlist”,它更简洁一点,但我不记得那是否是 SDK 的一部分。)
回答by Andreas
Like @fadden said, the same code is in two different APKs. This can e.g. happen when your test and tested project both depend on the same android library project. That does not have to be direct as library projects can depend on other library projects.
就像@fadden 所说的,相同的代码在两个不同的 APK 中。例如,当您的测试和测试项目都依赖于同一个 android 库项目时,就会发生这种情况。这不一定是直接的,因为库项目可以依赖于其他库项目。
The problem in the ant rules file seems to be fixed. At least in SDK tools r11. The compile target can be found in mail_rules.xml
and not in test_rules.xml
, just for those who got confused.
蚂蚁规则文件中的问题似乎已修复。至少在 SDK 工具 r11 中。编译目标可以在 中mail_rules.xml
而不是在 中找到test_rules.xml
,仅供那些感到困惑的人使用。
回答by Seelengut
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.
您是否尝试在测试 apk 中包含 .class 文件,而不是从原始项目生成新的 .class 文件?这解决了我的问题。
回答by Display name
I faced the same problem with unit tests being run by maven. By removing the link to the android libraries already imported by the main project the problem was fixed.
我在 maven 运行的单元测试中遇到了同样的问题。通过删除主项目已经导入的 android 库的链接,问题得到了解决。
回答by Abhinav Manchanda
回答by Chandra Lakkimsetty
android update test-project -m <project path> -p <test project path>
ant clean
ant debug
After running these commands, you can run ant tests. Note: If it gives JUnit related errors, please add Junit jar file in the test project libs folder.
运行这些命令后,您可以运行 ant 测试。注意:如果出现 JUnit 相关错误,请在测试项目的 libs 文件夹中添加 Junit jar 文件。