{"id":24102,"date":"2015-06-15T21:01:18","date_gmt":"2015-06-15T13:01:18","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=24102"},"modified":"2018-04-07T00:20:26","modified_gmt":"2018-04-06T16:20:26","slug":"android-development-with-roboguice","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/","title":{"rendered":"Easier Android Development with RoboGuice"},"content":{"rendered":"<p>RoboGuice is a must-have framework that brings the <strong>simplicity and ease of dependency injection<\/strong> to Android. <a href=\"https:\/\/github.com\/google\/guice\">Guice<\/a> is a lightweight dependency injection framework for the Java platform. Dependency injection is a design pattern with the core principal of <strong>separating behavior from dependency resolution<\/strong>.<\/p>\n<p>It allows for the removal of \u201chard-coded\u201d dependencies and makes it possible to change them at compile or run time. This makes your <strong>code easier to write, change, unit test <\/strong>and<strong> reuse in other contexts<\/strong>. Your code becomes less of a boilerplate code and more of business logic.<\/p>\n<p>With RoboGuice, you can bring all that to Android. RoboGuice allows you to <strong>use your own base classes<\/strong> along with your own essential methods or functionalities. This makes RoboGuice <strong>compatible with most of the libraries<\/strong> as well as allow you to strip <strong> nearly all platform boilerplates<\/strong> via various injections like View injections, Resource injections, SystemService injections, etc.<\/p>\n<p>In simple terms, using RoboGuice in your Android project means <strong>writing less code, handling fewer errors, and having fewer headaches<\/strong>.<\/p>\n<h2>How to install RoboGuice<\/h2>\n<p>RoboGuice can be easily installed in your Gradle-based project by adding the following lines to your \u201cdependencies\u201d section of your app module\u2019s gradle build file:<\/p>\n<pre>\r\nproject.dependencies {\r\n     compile 'org.roboguice:roboguice:3.+'\r\n     provided 'org.roboguice:roboblender:3.+'\r\n }<\/pre>\n<h2>How to use RoboGuice<\/h2>\n<p>RoboGuice is easy to use, if you follow these basic rules:<\/p>\n<p>1. <strong>Extend your classes<\/strong> from the appropriate RoboGuice\u2019s base classes like RoboActivity, RoboFragment, RoboService, RoboListActivity, RoboActionBarActivity, etc.<\/p>\n<p>2. <strong>Inject views, resources or services<\/strong> using different annotations provided by RoboGuice such as \u201c<em>@InjectView<\/em>\u201c, <em>\u201c@InjectResource<\/em>\u201c, <em>\u201c@Inject<\/em>\u201c, etc.<\/p>\n<p>That\u2019s it. Simple, isn\u2019t it? You will feel more at home if you\u2019re familiar with <strong>Java Annotations<\/strong>.<\/p>\n<h2>No RoboGuice vs. RoboGuice<\/h2>\n<p>Let\u2019s compare how RoboGuice can minify your workload and improve your productivity by using this small example. Suppose we have an \u201cactivity_main.xml\u201d layout file having all the views listed below.<\/p>\n<p><strong>Note: <\/strong>Comments are added to improve understandability of the code.<\/p>\n<pre>\r\nclass NoRoboGuice extends Activity { \r\n\t\/\/ views\r\n\tTextView name;\r\n\tImageView thumbnail;\r\n\t\r\n\t\/\/ services\r\n\tLocationManager loc;\r\n\t\r\n\t\/\/ resources\r\n\tDrawable icon;\r\n\tString myName;\r\n\r\n\tpublic void onCreate(Bundle savedInstanceState) { \r\n\t\tsuper.onCreate(savedInstanceState);\r\n\t\t\r\n\t\tsetContentView(R.layout.activity_main); \/\/ sets the layout\r\n\t\t\r\n\t\tname      = (TextView) findViewById(R.id.name); \/\/ boilerplate initialization\r\n\t\tthumbnail = (ImageView) findViewById(R.id.thumbnail);  \/\/ boilerplate initialization\r\n\t\t\r\n\t\tloc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE); \/\/ boilerplate initialization\r\n\t\t\r\n\t\ticon      = getResources().getDrawable(R.drawable.icon); \/\/ boilerplate initialization\r\n\t\tmyName    = getString(R.string.app_name); \/\/ boilerplate initialization\r\n\t\t\r\n\t\tname.setText( \"Hello, \" + myName ); \/\/ actual code\r\n\t} \r\n}\r\n<\/pre>\n<p>Here\u2019s how it looks like with RoboGuice:<\/p>\n<pre>@ContentView(R.layout.activity_main) \/\/ sets the layout\r\nclass RoboGuice extends RoboActivity { \r\n\t\/\/ views\r\n\t@InjectView(R.id.name)             TextView name; \r\n\t@InjectView(R.id.thumbnail)        ImageView thumbnail; \r\n\t\r\n\t\/\/ resources\r\n\t@InjectResource(R.drawable.icon)   Drawable icon; \r\n\t@InjectResource(R.string.app_name) String myName; \r\n\t\r\n\t\/\/ services\r\n\t@Inject                            LocationManager loc; \r\n\r\n\tpublic void onCreate(Bundle savedInstanceState) { \r\n\t\tsuper.onCreate(savedInstanceState);\r\n\t\t\r\n\t\tname.setText( \"Hello, \" + myName ); \/\/ actual code\r\n\t} \r\n} \r\n<\/pre>\n<p>RoboGuice does not only reduce the code, but also helps to improve the readability and understandability of the source code. It takes the guesswork out of app development and <strong>your application code is no longer littered<\/strong> with the mechanics of the Android platform.<\/p>\n<p>This advantage greatly helps <strong>at the time of debugging<\/strong> or <strong>updating<\/strong> the application as you can easily read and change the source code. You no longer need to search for the actual code in between the boilerplate initialization code because now <strong>only the actual code exists<\/strong> and RoboGuice does the boilerplate initialization automatically for you.<\/p>","protected":false},"excerpt":{"rendered":"<p>RoboGuice is a must-have framework that brings the simplicity and ease of dependency injection to Android. Guice is a lightweight dependency injection framework for the Java platform. Dependency injection is a design pattern with the core principal of separating behavior from dependency resolution. It allows for the removal of \u201chard-coded\u201d dependencies and makes it possible&hellip;<\/p>\n","protected":false},"author":120,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3399],"tags":[115,2628,2856],"topic":[4521],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Easier Android Development with RoboGuice - Hongkiat<\/title>\n<meta name=\"description\" content=\"RoboGuice is a must-have framework that brings the simplicity and ease of dependency injection to Android. Guice is a lightweight dependency injection\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Easier Android Development with RoboGuice\" \/>\n<meta property=\"og:description\" content=\"RoboGuice is a must-have framework that brings the simplicity and ease of dependency injection to Android. Guice is a lightweight dependency injection\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2015-06-15T13:01:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-06T16:20:26+00:00\" \/>\n<meta name=\"author\" content=\"Ashutosh KS\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashutosh KS\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/android-development-with-roboguice\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/android-development-with-roboguice\\\/\"},\"author\":{\"name\":\"Ashutosh KS\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/0c0611010da9a578caa32a34232cf7bd\"},\"headline\":\"Easier Android Development with RoboGuice\",\"datePublished\":\"2015-06-15T13:01:18+00:00\",\"dateModified\":\"2018-04-06T16:20:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/android-development-with-roboguice\\\/\"},\"wordCount\":418,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"keywords\":[\"Android\",\"Android Tips\",\"Dev Tools\"],\"articleSection\":[\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/android-development-with-roboguice\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/android-development-with-roboguice\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/android-development-with-roboguice\\\/\",\"name\":\"Easier Android Development with RoboGuice - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"datePublished\":\"2015-06-15T13:01:18+00:00\",\"dateModified\":\"2018-04-06T16:20:26+00:00\",\"description\":\"RoboGuice is a must-have framework that brings the simplicity and ease of dependency injection to Android. Guice is a lightweight dependency injection\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/android-development-with-roboguice\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/android-development-with-roboguice\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/android-development-with-roboguice\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Easier Android Development with RoboGuice\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/0c0611010da9a578caa32a34232cf7bd\",\"name\":\"Ashutosh KS\",\"description\":\"Ashutosh is a writer, entrepreneur, and a tech evangelist with expertise in the area of Computer Programming.\",\"sameAs\":[\"https:\\\/\\\/www.hongkiat.com\\\/\"],\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/ashutosh_ks\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Easier Android Development with RoboGuice - Hongkiat","description":"RoboGuice is a must-have framework that brings the simplicity and ease of dependency injection to Android. Guice is a lightweight dependency injection","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/","og_locale":"en_US","og_type":"article","og_title":"Easier Android Development with RoboGuice","og_description":"RoboGuice is a must-have framework that brings the simplicity and ease of dependency injection to Android. Guice is a lightweight dependency injection","og_url":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2015-06-15T13:01:18+00:00","article_modified_time":"2018-04-06T16:20:26+00:00","author":"Ashutosh KS","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Ashutosh KS","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/"},"author":{"name":"Ashutosh KS","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/0c0611010da9a578caa32a34232cf7bd"},"headline":"Easier Android Development with RoboGuice","datePublished":"2015-06-15T13:01:18+00:00","dateModified":"2018-04-06T16:20:26+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/"},"wordCount":418,"commentCount":7,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"keywords":["Android","Android Tips","Dev Tools"],"articleSection":["Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/","url":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/","name":"Easier Android Development with RoboGuice - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"datePublished":"2015-06-15T13:01:18+00:00","dateModified":"2018-04-06T16:20:26+00:00","description":"RoboGuice is a must-have framework that brings the simplicity and ease of dependency injection to Android. Guice is a lightweight dependency injection","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/android-development-with-roboguice\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Easier Android Development with RoboGuice"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/0c0611010da9a578caa32a34232cf7bd","name":"Ashutosh KS","description":"Ashutosh is a writer, entrepreneur, and a tech evangelist with expertise in the area of Computer Programming.","sameAs":["https:\/\/www.hongkiat.com\/"],"url":"https:\/\/www.hongkiat.com\/blog\/author\/ashutosh_ks\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-6gK","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/24102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/120"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=24102"}],"version-history":[{"count":4,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/24102\/revisions"}],"predecessor-version":[{"id":26961,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/24102\/revisions\/26961"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=24102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=24102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=24102"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=24102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}