匕首找不到注射成员模块(Dagger can't find injectable membe

2019-07-19 06:28发布

我用匕首在Android项目中的依赖注入,并且可以编译和构建应用程序的罚款。 对象图似乎是正确的正常工作,但是当我添加匕首编译器作为一个依赖在编译时出现错误,它报告了一些离奇失误:

[ERROR] error: No binding for com.squareup.tape.TaskQueue<com.atami \
    .mgodroid.io.NodeIndexTask> required by com.atami \
    .mgodroid.ui.NodeIndexListFragment for com.atami.mgodroid \
    .modules.OttoModule
[ERROR] error: No binding for com.squareup.tape.TaskQueue<com.atami \
    .mgodroid.io.NodeTask> required by com.atami \
    .mgodroid.ui.NodeFragment for com.atami.mgodroid.modules.OttoModule
[ERROR] error: No injectable members on com.squareup.otto.Bus. Do you want 
     to add an injectable constructor? required by com.atami. \
     mgodroid.io.NodeIndexTaskService for 
     com.atami.mgodroid.modules.TaskQueueModule

奥托错误看起来像一个埃里克·伯克在他提到了Android应用解剖演示大约没有一个@Provides注释,但你可以看到下面我做的。

我的奥托和任务队列模块如下:

@Module(
        entryPoints = {
                MGoBlogActivity.class,
                NodeIndexListFragment.class,
                NodeFragment.class,
                NodeActivity.class,
                NodeCommentFragment.class,
                NodeIndexTaskService.class,
                NodeTaskService.class
        }
)
public class OttoModule {

    @Provides
    @Singleton
    Bus provideBus() {
        return new AsyncBus();
    }

    /**
     * Otto EventBus that posts all events on the Android main thread
     */
    private class AsyncBus extends Bus {
        private final Handler mainThread = new Handler(Looper.getMainLooper());

        @Override
        public void post(final Object event) {
            mainThread.post(new Runnable() {
                @Override
                public void run() {
                    AsyncBus.super.post(event);
                }
            });
        }
    }
}

...

@Module(
    entryPoints = {
        NodeIndexListFragment.class,
        NodeFragment.class,
        NodeIndexTaskService.class,
        NodeTaskService.class
    }
)
public class TaskQueueModule {

    private final Context appContext;

    public TaskQueueModule(Context appContext) {
        this.appContext = appContext;
    }

    public static class IOTaskInjector<T extends Task> 
        implements TaskInjector<T> {

        Context context;

        /**
         * Injects Dagger dependencies into Tasks added to TaskQueues
         *
         * @param context the application Context
         */
        public IOTaskInjector(Context context) {
            this.context = context;
        }

        @Override
        public void injectMembers(T task) {
            ((MGoBlogApplication) context.getApplicationContext())
                .objectGraph().inject(task);
        }
    }

    public static class ServiceStarter<T extends Task> 
        implements ObjectQueue.Listener<T> {

        Context context;
        Class<? extends Service> service;

        /**
         * Starts the provided service when a Task is added to the queue
         *
         * @param context the application Context
         * @param service the Service to start
         */
        public ServiceStarter(Context context, 
                              Class<? extends Service> service) {
            this.context = context;
            this.service = service;
        }

        @Override
        public void onAdd(ObjectQueue<T> queue, T entry) {
            context.startService(new Intent(context, service));

        }

        @Override
        public void onRemove(ObjectQueue<T> queue) {
        }
    }


    @Provides
    @Singleton
    TaskQueue<NodeIndexTask> provideNodeIndexTaskQueue() {
        ObjectQueue<NodeIndexTask> delegate = 
            new InMemoryObjectQueue<NodeIndexTask>();
        TaskQueue<NodeIndexTask> queue = new TaskQueue<NodeIndexTask>(
            delegate, new IOTaskInjector<NodeIndexTask>(appContext));
        queue.setListener(new ServiceStarter<NodeIndexTask>(
            appContext, NodeIndexTaskService.class));
        return queue;
    }

    @Provides
    @Singleton
    TaskQueue<NodeTask> provideNodeTaskQueue() {
        ObjectQueue<NodeTask> delegate = 
            new InMemoryObjectQueue<NodeTask>();
        TaskQueue<NodeTask> queue = new TaskQueue<NodeTask>(
            delegate, new IOTaskInjector<NodeTask>(appContext));
        queue.setListener(new ServiceStarter<NodeTask>(
            appContext, NodeTaskService.class));
        return queue;
    }
}

...

/**
 * Module that includes all of the app's modules. Used by Dagger
 * for compile time validation of injections and modules.
 */
@Module(
        includes = {
                MGoBlogAPIModule.class,
                OttoModule.class,
                TaskQueueModule.class
        }
)
public class MGoBlogAppModule {
}

Answer 1:

匕首的全图表分析从一个完整的模块工作。 即@Module(完整=真),这是缺省值。 因为它是默认的,将匕首在默认情况下,假设所有绑定都可以从该模块或它包含明确的模块。

在这种情况下,你已经给你要求完成两个模块,但匕首没有办法在编译时绑在一起,这些无需额外的信号。 总之,没有OttoModule寂寂TaskQueueModule,编译器将尝试分析OttoModule其声称完整性,失败了,因为现在没有关于TaskQueueModule。

修改OttoModule的注解这样:

@Module(
  includes = TaskQueueModule.class,
  entryPoints = {
    MGoBlogActivity.class,
    NodeFragment.class,
    NodeActivity.class,
    NodeCommentFragment.class,
    NodeIndexTaskService.class,
    NodeTaskService.class
  }
)

然后匕首就会知道,OttoModule是完整的,它包括其他模块充分定义的一部分。

注:匕首编译器无法检测TaskQueueModule是存在的类路径上,只是“知道”,开发商意欲它与OttoModule无需额外的信号来使用。 例如,您可能有定义任务队列,它会选择哪一个几个模块? 该声明必须是明确的。



文章来源: Dagger can't find injectable members on a module