Dagger 1.x @Inject抛出IllegalArgumentException(Dagger 1.x @Inject throws an IllegalArgumentException)

编程入门 行业动态 更新时间:2024-10-25 02:23:53
Dagger 1.x @Inject抛出IllegalArgumentException(Dagger 1.x @Inject throws an IllegalArgumentException)

我想用Android的依赖注入框架替换我们的组件注册表(使用dexfile类加载器魔术)。 第一次尝试是匕首。

在尝试时我收到以下错误:

11-06 13:05:41.040 16269-16269/com.daggertoolkitexample E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{daggertoolkitexample/com.dagger.MyActivity}: java.lang.IllegalArgumentException: No inject registered for members/com.dagger.MyActivity. You must explicitly add it to the 'injects' option in one of your modules. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295) ... Caused by: java.lang.IllegalArgumentException: No inject registered for members/com.dagger.MyActivity. You must explicitly add it to the 'injects' option in one of your modules. at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:302) at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:279) at com.dagger.MyApplication.inject(MyApplication.java:39) at com.dagger.MyBaseActivity.onCreate(MyBaseActivity.java:18) at com.dagger.MyActivity.onCreate(MyActivity.java:22) at android.app.Activity.performCreate(Activity.java:5372) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) ...

如果我在@Module中注入我的活动,我可以解决它。 它的工作无一例外。

@Module( library = true, injects = MyActivity.class) public class AuthManagementModul {...}`

但这不是我想要的。 我不能并想知道我组件的所有用户。

大家都知道出了什么问题吗?

这是我的示例代码:

public class MyBaseActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ((MyApplication) getApplication()).inject(this); } }

...

public class MyActivity extends MyBaseActivity { @Inject AuthManagement authManagement; ... }

...

public class MyApplication extends Application{ private ObjectGraph graph; @Override public void onCreate() { super.onCreate(); graph = ObjectGraph.create(new AuthManagementModul(this)); } public void inject(Object object) { graph.inject(object); } }

...

@Module( library = true ) public class AuthManagementModul { private final Application application; public AuthManagementModul(Application application) { this.application = application; } @Provides @Singleton AuthManagement provideAuthManagement() { return new AuthManagementImpl(application); } }

I want to replace our component registry (with dexfile class loader magic) with an dependency injection framework for Android. The first try is dagger.

When trying I get the following error:

11-06 13:05:41.040 16269-16269/com.daggertoolkitexample E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{daggertoolkitexample/com.dagger.MyActivity}: java.lang.IllegalArgumentException: No inject registered for members/com.dagger.MyActivity. You must explicitly add it to the 'injects' option in one of your modules. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295) ... Caused by: java.lang.IllegalArgumentException: No inject registered for members/com.dagger.MyActivity. You must explicitly add it to the 'injects' option in one of your modules. at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:302) at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:279) at com.dagger.MyApplication.inject(MyApplication.java:39) at com.dagger.MyBaseActivity.onCreate(MyBaseActivity.java:18) at com.dagger.MyActivity.onCreate(MyActivity.java:22) at android.app.Activity.performCreate(Activity.java:5372) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) ...

I can fix it if i inject my activity in the @Module. Its work without exception.

@Module( library = true, injects = MyActivity.class) public class AuthManagementModul {...}`

But this is not that i want. I don´t can and want to know all users of my component.

Has everyone an idea what's wrong?

Here is my example code:

public class MyBaseActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ((MyApplication) getApplication()).inject(this); } }

...

public class MyActivity extends MyBaseActivity { @Inject AuthManagement authManagement; ... }

...

public class MyApplication extends Application{ private ObjectGraph graph; @Override public void onCreate() { super.onCreate(); graph = ObjectGraph.create(new AuthManagementModul(this)); } public void inject(Object object) { graph.inject(object); } }

...

@Module( library = true ) public class AuthManagementModul { private final Application application; public AuthManagementModul(Application application) { this.application = application; } @Provides @Singleton AuthManagement provideAuthManagement() { return new AuthManagementImpl(application); } }

最满意答案

在这种情况下,您不希望将injects =添加到AuthManagementModul,而是添加到包含它的特定于活动的模块。

Dagger 1.x使用injects =作为要分析的图根的信号,因此它们必须存在 - 但它们不需要存在于叶节点库模块上 - 仅存在于活动使用的模块上。 考虑在更多分区行上拆分模块,如下所示:

@Module( injects = { ... all your activities }, includes = { AuthManagementModul.class, ApplicationModule.class } ) class EntryPointsModule {} @Module(library = true, complete = false) class AuthManagementModul { @Provides @Singleton AuthManagement provideAuthManagement(Application application) { return new AuthManagementImpl(application); } } @Module(library = true) class ApplicationModule { private final Application application; public ApplicationModule(Application application) { this.application = application; } @Provides @Singleton Application application() { return application; } }

然后像这样创建图形:

public class MyApplication extends Application{ private ObjectGraph graph; @Override public void onCreate() { super.onCreate(); // AuthManagementModul is automatically included because it has a default // constructor and is included by EntryPointsModule graph = ObjectGraph.create(new EntryPointsModule(), new ApplicationModule(this)); } public void inject(Object object) { graph.inject(object); } }

还有其他方法来构建它 - 你可以让ApplicationModule包含AuthModule和declare injects,所以你只有两个模块,等等。我建议这样,因为ApplicationModule是一个单独的关注点,它的唯一作用是将Application实例提升到图表中,AuthManagementModul专门用于支持auth功能,而EntryPointsModule则是整个图形的前端。

如果迁移到Dagger2,这个结构也很方便,因为EntryPointsModule自然地转换为@Component。

In this case, you don't want to add injects= to your AuthManagementModul, but rather to an activity-specific module which includes it.

Dagger 1.x uses injects= as a signal for what graph-roots to analyze, so they must be present -but they need not be present on leaf-node library modules - just on a module the activity uses. Consider breaking up your modules on more partitioned lines like so:

@Module( injects = { ... all your activities }, includes = { AuthManagementModul.class, ApplicationModule.class } ) class EntryPointsModule {} @Module(library = true, complete = false) class AuthManagementModul { @Provides @Singleton AuthManagement provideAuthManagement(Application application) { return new AuthManagementImpl(application); } } @Module(library = true) class ApplicationModule { private final Application application; public ApplicationModule(Application application) { this.application = application; } @Provides @Singleton Application application() { return application; } }

Then create your graph like so:

public class MyApplication extends Application{ private ObjectGraph graph; @Override public void onCreate() { super.onCreate(); // AuthManagementModul is automatically included because it has a default // constructor and is included by EntryPointsModule graph = ObjectGraph.create(new EntryPointsModule(), new ApplicationModule(this)); } public void inject(Object object) { graph.inject(object); } }

There are other ways to structure this - you could just have ApplicationModule include the AuthModule and declare injects, so you only have two modules, etc. I suggested this way because ApplicationModule is then a separate concern whose only role is to hoist the Application instance into the graph, AuthManagementModul is exclusively there to support the auth function, and EntryPointsModule is there to be the front of the whole graph.

If you migrate to Dagger2, this structure is also convenient in that EntryPointsModule naturally converts to a @Component.

更多推荐

本文发布于:2023-08-01 15:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   Dagger   Inject   throws   IllegalArgumentException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!