{"id":698,"date":"2017-01-14T11:40:38","date_gmt":"2017-01-14T04:40:38","guid":{"rendered":"http:\/\/www.lixionary.com\/?p=698"},"modified":"2017-01-14T11:40:38","modified_gmt":"2017-01-14T04:40:38","slug":"android-dependency-injection-using-dagger-2","status":"publish","type":"post","link":"https:\/\/lixionary.com\/index.php\/2017\/01\/14\/android-dependency-injection-using-dagger-2\/","title":{"rendered":"Android Dependency Injection Using Dagger 2"},"content":{"rendered":"<p>Dagger 2 is a dependency injection\u00a0library\u00a0that relies heavily on annotation preprocessing to automatically generate a series of classes that provide dependencies to various parts of your application. The preprocessing occurs as part of compilation. The class files that are created are human readable\u200a\u2014\u200asome are meant to be referenced just like any other java classes, others should just silently do their magic in the background.<\/p>\n<p>Dagger 2 uses the following annotations:<\/p>\n<ul>\n<li>@Module and @Provides: define classes and methods which provide dependencies<\/li>\n<li>@Component: enable selected modules and used for performing dependency injection<\/li>\n<li>@Inject: request dependencies. Can be used on a constructor, a field, or a method<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-740\" src=\"https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/0f4e4-20170109_224813.png?w=1024&#038;h=660\" alt=\"\" width=\"1024\" height=\"660\" srcset=\"https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/0f4e4-20170109_224813.png 1253w, https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/0f4e4-20170109_224813-300x193.png 300w, https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/0f4e4-20170109_224813-1024x660.png 1024w, https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/0f4e4-20170109_224813-768x495.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2>Module and Providers<\/h2>\n<p>Get your bean ready with @Provides annotation on method level and remember to put @Module at each class that containing them. Any parameter given in method with @Provides will be injected as long as the parameter already created before as bean.<\/p>\n<p>[pastacode lang=&#8221;java&#8221; user=&#8221;Felixsu&#8221; path_id=&#8221;54Aqo&#8221; file=&#8221;dagger2_module.java&#8221; highlight=&#8221;&#8221; lines=&#8221;&#8221; provider=&#8221;bitbucketsnippets&#8221;\/]<\/p>\n<p>Module also become a building block for each component. Everything provided by\u00a0modules that component depends on, would able to be injected as in\u00a0<code>provideSharedPreference<\/code> method.<\/p>\n<p>Inside module, we can define scope of a providers\u00a0with extra annotation like @Singleton and\/or create any self made custom one. Those scope would be useful later in component section. Please take a note, @Singleton is not provided by Dagger, it only a conventions for us to flag providers that supposed to created once in Application lifecycle, so feel free to use any annotation\u00a0if you want.<\/p>\n<h2>Component<\/h2>\n<p>Responsible to inject any fields and constructors that annotated with @Inject.\u00a0Component\u00a0defines the connection between provider of objects (modules) and the objects which expresses a dependency. Component also need to be annotated with scope <strong>and<\/strong>\u00a0it will only takes bean coming from providers annotated\u00a0with same scope.<\/p>\n<p>[pastacode lang=&#8221;java&#8221; user=&#8221;Felixsu&#8221; path_id=&#8221;54Aqo&#8221; file=&#8221;dagger2_component.java&#8221; highlight=&#8221;&#8221; lines=&#8221;&#8221; provider=&#8221;bitbucketsnippets&#8221;\/]<\/p>\n<p>Later component can become beans provider\u00a0for other component. Let say you have Singleton SharedPrefereces in AppModule and want to use it on your MainActivityComponent, then you need\u00a0MainActivityComponent depends on AppComponent and explicitly provides method which exposed the singleton SharedPreferences bean.<\/p>\n<p>[pastacode lang=&#8221;java&#8221; user=&#8221;Felixsu&#8221; path_id=&#8221;54Aqo&#8221; file=&#8221;dagger2_component_activity.java&#8221; highlight=&#8221;&#8221; lines=&#8221;&#8221; provider=&#8221;bitbucketsnippets&#8221;\/]<\/p>\n<h2>Target<\/h2>\n<p>Last part of this post, targeting instance\u00a0that would be injected by our beans. To Inject the beans, simply put @Inject annotation whether in fields, constructor, or even method*.<\/p>\n<p>[pastacode lang=&#8221;java&#8221; user=&#8221;Felixsu&#8221; path_id=&#8221;54Aqo&#8221; file=&#8221;dagger2_injections_activity.java&#8221; highlight=&#8221;&#8221; lines=&#8221;&#8221; provider=&#8221;bitbucketsnippets&#8221;\/]<\/p>\n<p>Component interface we built before will be implemented by Dagger 2 for our use. Dagger 2 will create us a component builder that get ready all necessary bean inside its component object. Any method that doesn&#8217;t have qualified parameter, like void inject(MainActivity activity),\u00a0will be used to inject the parameter itself. So when we call inject(this) in snippet above, it means Dagger 2 would inject all @inject annotated things with provided beans.<\/p>\n<p>*for method parameter injections i haven&#8217;t tried\u00a0it myself<\/p>\n<p>Additional read:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.vogella.com\/tutorials\/Dagger\/article.html#introduction-to-the-concept-of-dependency-injection\">http:\/\/www.vogella.com\/tutorials\/Dagger\/article.html#introduction-to-the-concept-of-dependency-injection<\/a><\/li>\n<li><a href=\"http:\/\/frogermcs.github.io\/dependency-injection-with-dagger-2-custom-scopes\/\">http:\/\/frogermcs.github.io\/dependency-injection-with-dagger-2-custom-scopes\/<\/a><\/li>\n<\/ul>\n<p>Working sample codes:<\/p>\n<ul>\n<li><a href=\"https:\/\/Felixsu@bitbucket.org\/Felixsu\/android-dependency-injection.git\">https:\/\/Felixsu@bitbucket.org\/Felixsu\/android-dependency-injection.git<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Dagger 2 is a dependency injection\u00a0library\u00a0that relies heavily on annotation preprocessing to automatically generate a series of classes that provide dependencies to various parts of your application. The preprocessing occurs as part of compilation. The class files that are created are human readable\u200a\u2014\u200asome are meant to be referenced just like any other java classes, others [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":741,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[9,7],"tags":[39,115,116,125,130,190,220,228],"class_list":["post-698","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge","category-story","tag-android","tag-dagger","tag-dagger-2","tag-dependency-injection","tag-di","tag-google","tag-injection","tag-java"],"uagb_featured_image_src":{"full":["https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/48c1a-20170109_230010.png",1079,1079,false],"thumbnail":["https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/48c1a-20170109_230010-150x150.png",150,150,true],"medium":["https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/48c1a-20170109_230010-300x300.png",300,300,true],"medium_large":["https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/48c1a-20170109_230010-768x768.png",768,768,true],"large":["https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/48c1a-20170109_230010-1024x1024.png",1024,1024,true],"1536x1536":["https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/48c1a-20170109_230010.png",1079,1079,false],"2048x2048":["https:\/\/lixionary.com\/wp-content\/uploads\/2017\/01\/48c1a-20170109_230010.png",1079,1079,false]},"uagb_author_info":{"display_name":"lixsu92","author_link":"https:\/\/lixionary.com\/index.php\/author\/lixsu92\/"},"uagb_comment_info":0,"uagb_excerpt":"Dagger 2 is a dependency injection\u00a0library\u00a0that relies heavily on annotation preprocessing to automatically generate a series of classes that provide dependencies to various parts of your application. The preprocessing occurs as part of compilation. The class files that are created are human readable\u200a\u2014\u200asome are meant to be referenced just like any other java classes, others&hellip;","_links":{"self":[{"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/posts\/698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/comments?post=698"}],"version-history":[{"count":0,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/posts\/698\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/media\/741"}],"wp:attachment":[{"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/media?parent=698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/categories?post=698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/tags?post=698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}