Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot initialize Component during Instrumentation Tests #88

Closed
bryanstern opened this issue Dec 6, 2014 · 1 comment
Closed

Cannot initialize Component during Instrumentation Tests #88

bryanstern opened this issue Dec 6, 2014 · 1 comment

Comments

@bryanstern
Copy link

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);
    }
}
@bryanstern bryanstern changed the title Cannot build Component during Instrumentation Tests Cannot initialize Component during Instrumentation Tests Dec 6, 2014
@bryanstern
Copy link
Author

After researching this more, I'm pretty sure the issue is actually with Espresso.
https://code.google.com/p/android-test-kit/issues/detail?id=105

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant