You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm getting the following runtime error using Dagger 2 while trying to run Instrumentation tests (using Espresso). In my project, I create an instance of a Component in the Application class and use that instance to inject values into Activities, Fragments, etc.. The AndroidComponent constructor requires a Context and the Application class passes an instance of itself. I have debug and release build variants which run fine, but the Instrumentation Runner (or GoogleInstrumentationTestRunner in this case) causes this strange error.
Logcat
AndroidRuntime E FATAL EXCEPTION: main
E Process: com.project.android.dev, PID: 3556
E java.lang.NoClassDefFoundError: com/project/android/DataModule$$ProvidesApiFactory
E at com.project.android.Dagger_AndroidComponent.initialize(Dagger_AndroidComponent.java:65)
E at com.project.android.Dagger_AndroidComponent.<init>(Dagger_AndroidComponent.java:57)
E at com.project.android.Dagger_AndroidComponent.<init>(Dagger_AndroidComponent.java:31)
E at com.project.android.Dagger_AndroidComponent$Builder.build(Dagger_AndroidComponent.java:117)
E at com.project.android.AndroidComponent$Initializer.init(AndroidComponent.java:21)
E at com.project.android.App.onCreate(App.java:51)
E at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
E at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4344)
E at android.app.ActivityThread.access$1500(ActivityThread.java:135)
E at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E at android.os.Handler.dispatchMessage(Handler.java:102)
E at android.os.Looper.loop(Looper.java:136)
E at android.app.ActivityThread.main(ActivityThread.java:5017)
E at java.lang.reflect.Method.invokeNative(Native Method)
E at java.lang.reflect.Method.invoke(Method.java:515)
E at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E at dalvik.system.NativeStart.main(Native Method)
E Caused by: java.lang.ClassNotFoundException: Didn't find class "com.project.android.DataModule$$ProvidesApiFactory" on path: DexPathList[[zip file "/system/framewo
rk/android.test.runner.jar", zip file "/data/app/com.project.android.dev.test-2.apk", zip file "/data/app/com.project.android.dev-2.apk"],nativeLibraryDirectories=[
/data/app-lib/com.project.android.dev.test-2, /data/app-lib/com.project.android.dev-2, /vendor/lib, /system/lib]]
E at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
E at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
E ... 18 more
Process 3556 ended
Below is basically what the classes in question consist of. The design is pretty much straight from the samples.
The main gist of App.java
public class App extends android.app.Application {
private AndroidComponent component;
@Override
public void onCreate() {
super.onCreate();
component = AndroidComponent.Initializer.init(this.getApplicationContext());
component().inject(this);
}
public AndroidComponent component() {
return component;
}
}
The main gist of AndroidComponent.java
@Singleton
@Component(modules = { DataModule.class })
public interface AndroidComponent {
void inject(BaseActivity activity);
void inject(App application);
void inject(BaseFragment fragment);
public final static class Initializer {
static AndroidComponent init(Context app) {
return Dagger_AndroidComponent.builder()
.dataModule(new DataModule(app))
.build();
}
}
}
The main gist of DataModule.java
public class DataModule {
private final Context application;
public DataModule(Context application) {
this.application = application;
}
@Provides @Singleton
Api providesApi() {
return new Api(application);
}
}
The text was updated successfully, but these errors were encountered:
bryanstern
changed the title
Cannot build Component during Instrumentation Tests
Cannot initialize Component during Instrumentation Tests
Dec 6, 2014
I'm getting the following runtime error using Dagger 2 while trying to run Instrumentation tests (using Espresso). In my project, I create an instance of a Component in the Application class and use that instance to inject values into Activities, Fragments, etc.. The AndroidComponent constructor requires a Context and the Application class passes an instance of itself. I have debug and release build variants which run fine, but the Instrumentation Runner (or GoogleInstrumentationTestRunner in this case) causes this strange error.
Logcat
Below is basically what the classes in question consist of. The design is pretty much straight from the samples.
The main gist of App.java
The main gist of AndroidComponent.java
The main gist of DataModule.java
The text was updated successfully, but these errors were encountered: