使用匕首模块没有与QUOT;&其注入QUOT;指示匕首、指示、模块、QUOT

2023-09-05 06:49:25 作者:覆水难收

我想使匕首在工作中没有其注入指令的@Module注释里面。我立足我的测试项目上的 Android的简单匕首例如

I am trying to make Dagger work without the "injects" directive inside the @Module annotation. I am basing my test project on the Android Simple Dagger example

这是给我的问题的一部分:

This is the part that is giving me problems:

@Module(
    injects = HomeActivity.class,
    complete = false
)
public class DemoModule {
  // TODO put your application-specific providers here!
}

(编辑):这在我的code是CTXModules.java

这是我想删除的部分是=其注入HomeActivity.class。我知道我可以用@Inject标注在构造函数中删除的部分有纪念我自己的模块,但不知何故,不与被添加到图中的模块工作。由于该行存在,一切都运行得很好。

The part that I'd like to remove is the "injects = HomeActivity.class". I know I can mark my own modules with the @Inject annotation in the constructor to remove that part there, but somehow it doesn't work with the module that is added to the graph. With that line there, everything works just fine.

我需要这个的原因是因为匕首将在基础库项目,这将是对一些项目都有一个共同的code的基础,因此在目前或写这部分的$的基础上实施C $三我不知道有什么课程将注入模块。

The reason I need this is because dagger will be implemented in a base-library project that will be the foundation for some projects that share a common code base and therefore at the moment or writing the this part of the code I don't know what classes will inject modules.

就是我试图做甚至可能吗?

Is what I am trying to do even possible?

我认为这是可能的,因为Android模块类不使用该指令。

I assume it is possible because the Android Module class does not use that directive.

希望这是很清楚。在此先感谢!

Hope it's clear enough. Thanks in advance!

(编辑)

我应该提到它。在我的模块我删除注入= HomeActivity.class,并增加库=真正的之类的Android模块类。什么情况呢是,我得到这个错误(我的坏之前,我不加吧):

I should have mentioned it. In my module I remove "injects = HomeActivity.class" and add "library = true" like in the Android Module class. What happens then is that I get this error (my bad I not added it before):

12-10 09:21:16.807: E/AndroidRuntime(21783): FATAL EXCEPTION: main
12-10 09:21:16.807: E/AndroidRuntime(21783): Process: com.ef.daggertestproject, PID: 21783
12-10 09:21:16.807: E/AndroidRuntime(21783): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ef.daggertestproject/com.ef.daggertestproject.MainActivity}: java.lang.IllegalArgumentException: No inject registered for members/com.ef.daggertestproject.MainActivity. You must explicitly add it to the 'injects' option in one of your modules.
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.os.Looper.loop(Looper.java:137)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.app.ActivityThread.main(ActivityThread.java:4998)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at java.lang.reflect.Method.invoke(Method.java:515)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at dalvik.system.NativeStart.main(Native Method)
12-10 09:21:16.807: E/AndroidRuntime(21783): Caused by: java.lang.IllegalArgumentException: No inject registered for members/com.ef.daggertestproject.MainActivity. You must explicitly add it to the 'injects' option in one of your modules.
12-10 09:21:16.807: E/AndroidRuntime(21783):    at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:281)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:258)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at com.ef.daggertestproject.MyApplication.inject(MyApplication.java:47)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at com.ef.daggertestproject.BaseActivity.onCreate(BaseActivity.java:27)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at com.ef.daggertestproject.MainActivity.onCreate(MainActivity.java:16)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.app.Activity.performCreate(Activity.java:5243)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-10 09:21:16.807: E/AndroidRuntime(21783):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-10 09:21:16.807: E/AndroidRuntime(21783):    ... 11 more

另外,我已经上传我的测试项目,github上

(决赛编辑)根据杰克的回答是:声明一个模块作为一个图书馆没有缓解匕首需要了解注射点因此,回答我原来的问题是,它是不可能的。

(Final Edit) according to Jake's answer: "Declaring a module as a library does not alleviate the needs of Dagger to know about injection points." And therefore the answer to my original question is that it's not possible.

推荐答案

您想

@Module(library=true)

下面就是文档说关于

假如果是必要的,以满足其所有注射类型的该模块中的所有包含绑定。如果一个模块是不是一个库模块,它是获附加静态检查:工具可以检测是否包含绑定是没有必要的。 如果您提供的不使用该模块的图形绑定,那么您必须声明库=真。

False if all the included bindings in this module are necessary to satisfy all of its injectable types. If a module is not a library module, it is eligible for additional static checking: tools can detect if included bindings are not necessary. If you provide bindings that are not used by this module's graph, then you must declare library = true.

(重点煤矿)的

声明为库不缓解匕首的需要了解注射点的模块。你还必须声明与上市器喷射对象图的模块。

Declaring a module as a library does not alleviate the needs of Dagger to know about injection points. You still must declare a module in the object graph with the listed injects.

你的榜样的一个极端简化的版本是这样的:

An extreme simplified version of your example would look like this:

repo/
 +- library/
 |   +- Foo.java
 |   `- FooModule.java
 |
 `- app/
     +- BarActivity.java
     `- BarModule.java

FooModule.java

@Module(library = true)
public final class FooModule {
  @Provides @Singleton provideFoo() {
    return Foo();
  }
}

BarModule.java

@Module(
  injects = BarActivity.class,
  includes = FooModule.class
)
public final class BarModule {
}

BarActivity.java (或类似):

ObjectGraph og = ObjectGraph.create(new BarModule());
og.inject(this);