Square Dagger IllegalStateException: Module adapter for class MyApplicationModule could not be loaded

user2368140 picture user2368140 · Nov 24, 2014 · Viewed 7.9k times · Source

I am using eclipse and Dagger 1.2.2 for my Android project. I managed to implement a test application with Dagger. But with my "real" application I get:

java.lang.RuntimeException: Unable to create application app.MyApplication: java.lang.IllegalStateException: Module adapter for class app.MyApplicationModule could not be loaded. Please ensure that code generation was run for this module.:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to create application app.MyApplication: java.lang.IllegalStateException: Module adapter for class app.MyApplicationModule could not be loaded. Please ensure that code generation was run for this module.
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4687)
    at android.app.ActivityThread.access$1400(ActivityThread.java:159)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Module adapter for class app.MyApplicationModule could not be loaded. Please ensure that code generation was run for this module.
    at dagger.internal.FailoverLoader$1.create(FailoverLoader.java:45)
    at dagger.internal.FailoverLoader$1.create(FailoverLoader.java:40)
    at dagger.internal.Memoizer.get(Memoizer.java:56)
    at dagger.internal.FailoverLoader.getModuleAdapter(FailoverLoader.java:57)
    at dagger.internal.Modules.loadModules(Modules.java:43)
    at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:174)
    at dagger.ObjectGraph$DaggerObjectGraph.access$000(ObjectGraph.java:138)
    at dagger.ObjectGraph.create(ObjectGraph.java:129)
    at app.MyApplication.onCreate(MyApplication.java:17)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4684)
    ... 10 more

This is MyApplication.java:

public class MyApplication extends Application implements InjectableApplication {
    private ObjectGraph objectGraph;

    @Override
    public void onCreate() {
        super.onCreate();

        objectGraph = ObjectGraph.create(new MyApplicationModule(this));
        objectGraph.inject(this);
    }

    @Override
    public void inject(Object o) {
        objectGraph.inject(o);
    }

    public ObjectGraph getObjectGraph() {
        return objectGraph;
    }
}

This is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name="app.MyApplication" >
        <activity
            android:name="app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I also included the Dagger jars to the build path and factory path.

Answer

j2emanue picture j2emanue · Dec 21, 2014

For anyone who might have had this issue as i did. The issue i was facing is that i did not add a dependency in my gradle build file to allow dagger to do code generation ontop of my classes:

Add this:

apt 'com.squareup.dagger:dagger-compiler:1.2.2'

update: if you dont know where the apt plugin is, you can install it in your projects gradle file. My dependencies look like this:

dependencies {
    classpath 'com.android.tools.build:gradle:1.0.0'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4+'
}