[英]Android Instrumentation test does not find classes from library project
So I'm getting a NoSuchMethodError
when running my Activity/instrumentation tests from Android Studio, on the code line which tries to call a method in a library module from the unit test. 所以当我从Android Studio运行我的Activity / instrumentation测试时,我会在尝试从单元测试中调用库模块中的方法的代码行上获得NoSuchMethodError
。
So this is my test: 所以这是我的测试:
public class MainActivityTest extends ActivityInstrumentationTestCase2 {
public void testMainActivity() {
final MainActivity target = (MainActivity) getActivity();
MyLibarary.someStaticMethod(); // yields java.lang.NoSuchMethodError
}
What's the deal here? 这是什么交易? I've defined my library as a "compile"-dependency in build.gradle, and it's compiling just fine. 我已经将我的库定义为build.gradle中的“编译” - 依赖,并且编译得很好。 The library is also invoked from the main app classes without problems. 该库也可以从主应用程序类中调用而不会出现问题。 It's only when I call it from the tests it fails. 只有当我从测试中调用它时才会失败。 My app and my tests are in the same module. 我的应用程序和我的测试都在同一模块中。
I've tried running the clean task, the assembleDebug and assembleDebugTest tasks manually. 我已经尝试手动运行clean任务,assembleDebug和assembleDebugTest任务。 No avail. 徒劳无功。
Project structure: 项目结构:
Root
|---MyApp
| |---src/main/...
| |---src/androidTest/...
|----MyLibrary
Running Android Studio v1.0.2 Gradle build tools v1.0.0 Running as an "Android Test" on module "MyApp" from the Run/Debug configurations of AS with default Instrumentation test runner. 运行Android Studio v1.0.2 Gradle构建工具v1.0.0使用默认的Instrumentation测试运行器从AS的Run / Debug配置作为模块“MyApp”上的“Android Test”运行。
Ok this one was a bit tricky. 好的,这个有点棘手。 Keyword: proguard minify . 关键词: proguard minify 。 Since the newly implemented method so far only was used by the instrumentation test, proguard didn't pick up on its usage and therefore removed it from the DEX in the proguardDebugTest build step. 由于迄今为止新实现的方法仅被仪器测试使用,因此proguard没有了解其使用情况,因此在proguardDebugTest构建步骤中将其从DEX中删除。
Solution: Either disable minification in the debug build (In gradle: android.buildTypes.debug.minifyEnabled false
), or use the method in the main app. 解决方案:在调试版本中禁用缩小(In gradle: android.buildTypes.debug.minifyEnabled false
),或使用主应用程序中的方法。
Not really up-to-date with Gradle
. 与Gradle
不是最新的。 But i think we are supposed to specify the testCompile
or the androidTestCompile
dependency as well in the build.gradle
if trying to write instrumentation tests. 但我认为我们都应该指定testCompile
或androidTestCompile
的依赖以及在build.gradle
如果试图写仪器测试。
Helpful Links: 有用的网址:
http://gradle.org/docs/current/userguide/java_plugin.html http://gradle.org/docs/current/userguide/java_plugin.html
Specifying test dependencies with the Gradle Android build system 使用Gradle Android构建系统指定测试依赖项
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.