Square Dagger IllegalStateException:无法加载类MyApplicationModule的模块适配器

时间:2014-11-24 21:34:14

标签: android dependency-injection dagger

我正在为我的Android项目使用eclipse和Dagger 1.2.2。我设法用Dagger实现了一个测试应用程序。但是对于我的"真实"申请我得到:

java.lang.RuntimeException:无法创建应用程序app.MyApplication:java.lang.IllegalStateException:无法加载类app.MyApplicationModule的模块适配器。请确保为此模块运行代码生成。

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

这是 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;
    }
}

这是我的 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>

我还将Dagger罐子包含在构建路径和工厂路径中。

4 个答案:

答案 0 :(得分:13)

对于任何可能像我一样遇到此问题的人。我面临的问题是我没有在我的gradle构建文件中添加依赖项,以允许匕首在我的类上进行代码生成:

添加:

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

更新:如果您不知道apt插件的位置,可以将其安装在项目gradle文件中。我的依赖关系看起来像这样:

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

答案 1 :(得分:1)

我发现了我的错误:

您必须检查&#34; 启用项目特定设置&#34; for&#34; Java Compiler&#34;和#34;注释处理&#34;。现在我的应用程序抛出了很多其他错误,但Dagger代码生成的东西可以工作!

答案 2 :(得分:0)

如果您使用multiDex,请不要忘记将其激活到YourApp.class

   @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        MultiDex.install(this);
    }

这解决了我的类似问题。

答案 3 :(得分:0)

project build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4+'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

app build.gradle:

buildscript {
    repositories {

        mavenCentral()
        ...
    }
...
}

...
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
...
dependencies {
...
    compile 'com.squareup.dagger:dagger:1.2.2'
    provided 'com.squareup.dagger:dagger-compiler:1.2.2'
    apt 'com.squareup.dagger:dagger-compiler:1.2.2'
...


  }

   configurations {
      provided
   }