开始探索Espresso 2.0,但似乎遇到了打嗝。我无法让测试成功运行任何包含Dagger的项目。当我运行测试时,我得到以下异常(最后的整个堆栈跟踪):
java.lang.NoClassDefFoundError: com/pdt/daggerexample/model/DaggerExampleAppModule$$ModuleAdapter$ProvideMySingletonProvidesAdapter
应用程序在未从AndroidInstrumentationTest运行时运行。
以下是一些相关文件,我还将项目上传到github,以便更快地结帐/复制https://github.com/paul-turner/espressoDaggerExample。
的build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.pdt.daggerexample"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
}
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.jakewharton:butterknife:5.1.1'
compile 'com.squareup.dagger:dagger:1.2.2'
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}
测试:
public class SampleEspressoTest extends ActivityInstrumentationTestCase2<MainActivity> {
public SampleEspressoTest() {
super(MainActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
// Espresso will not launch our activity for us, we must launch it via getActivity().
getActivity();
}
public void testCheckText() {
onView(ViewMatchers.withId(com.pdt.daggerexample.R.id.espresso_test))
.check(matches(withText("Espresso Test")));
}
}
模块:
package com.pdt.daggerexample.model;
import com.pdt.daggerexample.inject.DaggerExampleApplication;
import com.pdt.daggerexample.MainActivity;
import com.pdt.daggerexample.SecondActivity;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module(injects = {
MainActivity.class,
SecondActivity.class,
}, complete = true)
public class DaggerExampleAppModule {
private final DaggerExampleApplication mDaggerExampleApplication;
public DaggerExampleAppModule(DaggerExampleApplication daggerExampleApplication) {
mDaggerExampleApplication = daggerExampleApplication;
}
@Provides
@Singleton
public MySingleton provideMySingleton() {
return new MySingleton(mDaggerExampleApplication.getApplicationContext(), "FOOBAR!");
}
@Provides
public MyRegularOldClassInstance provideMyRegularOldClassInstance() {
return new MyRegularOldClassInstance();
}
}
堆栈跟踪:
12-24 15:18:17.986 1282-1282/? E/MonitoringInstrumentation﹕ Dying now...
12-24 15:18:17.986 1282-1282/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com/pdt/daggerexample/model/DaggerExampleAppModule$$ModuleAdapter$ProvideMySingletonProvidesAdapter
at com.pdt.daggerexample.model.DaggerExampleAppModule$$ModuleAdapter.getBindings(DaggerExampleAppModule$$ModuleAdapter.java:28)
at com.pdt.daggerexample.model.DaggerExampleAppModule$$ModuleAdapter.getBindings(DaggerExampleAppModule$$ModuleAdapter.java:13)
at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:185)
at dagger.ObjectGraph$DaggerObjectGraph.access$000(ObjectGraph.java:138)
at dagger.ObjectGraph.create(ObjectGraph.java:129)
at com.pdt.daggerexample.inject.DaggerExampleApplication.onCreate(DaggerExampleApplication.java:16)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
at android.app.ActivityThread.access$1300(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.pdt.daggerexample.model.DaggerExampleAppModule$$ModuleAdapter$ProvideMySingletonProvidesAdapter
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at com.pdt.daggerexample.model.DaggerExampleAppModule$$ModuleAdapter.getBindings(DaggerExampleAppModule$$ModuleAdapter.java:28)
at com.pdt.daggerexample.model.DaggerExampleAppModule$$ModuleAdapter.getBindings(DaggerExampleAppModule$$ModuleAdapter.java:13)
at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:185)
at dagger.ObjectGraph$DaggerObjectGraph.access$000(ObjectGraph.java:138)
at dagger.ObjectGraph.create(ObjectGraph.java:129)
at com.pdt.daggerexample.inject.DaggerExampleApplication.onCreate(DaggerExampleApplication.java:16)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
at android.app.ActivityThread.access$1300(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:21)
这个问题让我有点兴奋。您可能需要从espresso依赖项中排除javax.inject。早期版本的Android似乎不太容忍相同的依赖项。
有些事情:
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
exclude group: 'javax.inject'
}
请注意,这可能会让你无处不在。模拟器设备日志有一些关于碰撞发生位置的有用提示。 E.g:
(Ldagger/internal/Binding; had used a different Ljavax/inject/Provider; during pre-verification)
或
DexOpt: not resolving ambiguous class 'Ljunit/framework/TestSuite;'
答案 1 :(得分:0)
我已经解决了这个问题,并添加了以下代码:
configurations {
androidTestCompile.exclude group: 'com.android.support', module: 'support-v4'
}