{"id":781,"date":"2021-06-20T14:02:42","date_gmt":"2021-06-20T12:02:42","guid":{"rendered":"http:\/\/blog.ansuz.nl\/?p=781"},"modified":"2021-06-16T17:47:40","modified_gmt":"2021-06-16T15:47:40","slug":"retrofit-api-declaration-and-inheritance","status":"publish","type":"post","link":"http:\/\/blog.ansuz.nl\/index.php\/2021\/06\/20\/retrofit-api-declaration-and-inheritance\/","title":{"rendered":"Retrofit API declaration and inheritance"},"content":{"rendered":"\n<p>Retrofit 2.7 introduced <a href=\"https:\/\/github.com\/square\/retrofit\/pull\/3145?\">Interface inheritance<\/a>. This is great if you want to separate your API definition from the actual networking implementation. You can now define a &#8220;plain&#8221; interface as API contract, we&#8217;ll call this <code>FooApi<\/code>, and then have another (Retrofit specific) interface inherit from it and add all the Retrofit sugar.<\/p>\n\n\n\n<p>There seems to be a small catch though to make this fully work. You need to make sure that your concrete Retrofit API does not get obfuscated. If it does, Retrofit will throw the following runtime exception: <code>java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.)<\/code>.<br><\/p>\n\n\n\n<p>Let&#8217;s say we created this concrete API declaration and called it <code>FooRetrofitApi<\/code>. We&#8217;ve also wired up all Retrofit annotations so it is able to make requests to your API.<br><\/p>\n\n\n\n<p>All that is left to do is to exclude your concrete API from obfuscation. Do so by adding the following to your ProGuard rules: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-keep class nl.ansuz.FooRetrofitApi { *; }<\/pre>\n\n\n\n<p>Bob&#8217;s your uncle!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">P.S.<\/h2>\n\n\n\n<p>So why do the Retrofit annotations get stripped? I think that is because of the way the code is structured. If you follow clean code principles, you&#8217;ll end up referencing the <code>FooApi<\/code> throughout your project. The concrete <code>FooRetrofitApi<\/code> is only injected. As a result R8 might think that <code>FooRetrofitApi<\/code> can be more optimised than is actually the case.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Retrofit 2.7 introduced Interface inheritance. This is great if you want to separate your API definition from the actual networking implementation. You can now define a &#8220;plain&#8221; interface as API contract, we&#8217;ll call this FooApi, and then have another (Retrofit &hellip; <a href=\"http:\/\/blog.ansuz.nl\/index.php\/2021\/06\/20\/retrofit-api-declaration-and-inheritance\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[146],"class_list":["post-781","post","type-post","status-publish","format-standard","hentry","category-android","tag-retrofit"],"_links":{"self":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/comments?post=781"}],"version-history":[{"count":7,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/781\/revisions"}],"predecessor-version":[{"id":792,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/781\/revisions\/792"}],"wp:attachment":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/media?parent=781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/categories?post=781"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/tags?post=781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}