<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nishant Srivastava</title>
    <description>The latest articles on DEV Community by Nishant Srivastava (@nisrulz).</description>
    <link>https://dev.to/nisrulz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F11950%2FKkYK72zA.jpg</url>
      <title>DEV Community: Nishant Srivastava</title>
      <link>https://dev.to/nisrulz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nisrulz"/>
    <language>en</language>
    <item>
      <title>Is your Android Library, Lifecycle-Aware?</title>
      <dc:creator>Nishant Srivastava</dc:creator>
      <pubDate>Sun, 08 Oct 2017 01:20:24 +0000</pubDate>
      <link>https://dev.to/nisrulz/is-your-android-library-lifecycle-aware-2di</link>
      <guid>https://dev.to/nisrulz/is-your-android-library-lifecycle-aware-2di</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LimFRzhb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bwtapw7v91tetikgayje.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LimFRzhb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bwtapw7v91tetikgayje.png" alt="header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lifecycle events in Android.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It has been a pain point for Android developers all over the world. It is no brainer that, most of the times the main cause of a memory leak in the codebase is because an invalid state is accessed which is out of sync with the lifecycle of the activity/fragment of the app. What this means is that as an Android Developer you are forced to put up a lot of checks in place, making sure that your code state is in sync with the lifecycle of the Activity/Fragment.&lt;/p&gt;

&lt;p&gt;Things start to get a bit more messy when that is something you expect from others to do, which is the case when you are an Android Library Developer. As an android library developer, you need other app developers to conform to unregistering callbacks/call cleanup methods or specific functions to re-initialize at certain points in the lifecycle of Activity/Fragment.i.e onDestroy(), onResume(), onCreate(), etc.&lt;/p&gt;

&lt;p&gt;Now that is a lot to expect and ask for. We all know developers tend to avoid going through documentations (..to build features quickly and achieve deadlines, obviously ðŸ˜… ). So what that means is sometimes the cleanup part is missed or not understood quite properly for them to put in the right places in code. For example, if an &lt;code&gt;Activity&lt;/code&gt; receives a callback after it’s been stopped, then it’s pretty likely that your app is going to crash due to memory leak. Unregistering the callback is right before the Activity is destroyed is what is the needed, which is possible if the app developer calls the &lt;code&gt;cleanup()&lt;/code&gt; method from your android library, but it is expected of them. You cannot make sure that they will conform to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Well that is a problem, so what is the solution?
&lt;/h3&gt;

&lt;p&gt;The solution is to make your android library &lt;strong&gt;&lt;em&gt;Lifecycle-Aware&lt;/em&gt;&lt;/strong&gt;. Making android library aware of the states of the lifecycle of Activity/Fragment gives us, the Android library developers greater control over making sure that callbacks are unregistered and calls to cleanup methods or re-initialization happens at the right state in the lifecycle of Activity/Fragment. At the same time it means less dependence on app developer to conform to putting more code and checks in their app to handle the states of your android library. &lt;strong&gt;&lt;em&gt;Sounds like a win-win for both, eh!&lt;/em&gt;&lt;/strong&gt; ðŸ˜Ž&lt;/p&gt;

&lt;h3&gt;
  
  
  But how do we make our library code Lifecycle AwareÂ ?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Enter, Lifecycle Arch Components!
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://developer.android.com/topic/libraries/architecture/lifecycle.html"&gt;Lifecycle Arch Components&lt;/a&gt; are a new set of lifecycle-aware components that can track the lifecycle of an activity or fragment, and adjust their behavior accordingly.&lt;/p&gt;

&lt;p&gt;They are created by good folks at Google, were introduced at Google I/O 2017 and are currently in &lt;code&gt;beta&lt;/code&gt; stage. Being in &lt;code&gt;beta&lt;/code&gt; does not mean they aren’t ready. They seem to be the way most people are going to be moving forward in the near future as the library itself becomes stable. But we can get on the band wagon right now, as they are fairly ready.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Update: &lt;em&gt;Core lifecycle artifacts (runtime, common) have reached stable version &lt;code&gt;v1.0.0&lt;/code&gt;. The lifecycle compiler and extensions are now at version &lt;code&gt;v1.0.0-beta1&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Without taking much time, lets jump right into understanding how to wire these lifecycle components for your own android library…&lt;/p&gt;

&lt;p&gt;I will be using a simple example to demonstrate the wiring up process, which is available as a completely functional example app on github &lt;a href="https://github.com/nisrulz/android-examples/tree/develop/LifeCycleCompForLib"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Lets start by creating a new app. Once done, you can add a new library module&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R1nuHpE1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/phl0zzgpp7dtbv7pw39t.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R1nuHpE1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/phl0zzgpp7dtbv7pw39t.jpeg" alt="p1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your folder structure should look like the below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XDB7zI-X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/fxx19ztyxe3g99bp3q52.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XDB7zI-X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/fxx19ztyxe3g99bp3q52.jpeg" alt="p2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next add this &lt;code&gt;awesomelib&lt;/code&gt; module as a dependency to the &lt;code&gt;app&lt;/code&gt; module, so that the &lt;code&gt;app&lt;/code&gt; module has access to the public classes contained by the &lt;code&gt;awesomelib&lt;/code&gt; library module. To do that add the below to the &lt;code&gt;build.gradle&lt;/code&gt; of the &lt;code&gt;app&lt;/code&gt; module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="nf"&gt;project&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;':awesomelib'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Next you want to add a class to your &lt;code&gt;awesomelib&lt;/code&gt; library module. I created mine named as &lt;code&gt;AwesomeMainLib&lt;/code&gt;Â . This class is just to showcase the code for library which will be referenced in the &lt;code&gt;app&lt;/code&gt; module later on. For simplicity sake, lets make &lt;code&gt;AwesomeMainLib&lt;/code&gt; class a singleton. The code in that case would look like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;and in your &lt;code&gt;app&lt;/code&gt; module you would call &lt;code&gt;init()&lt;/code&gt; method via &lt;code&gt;AwesomeLibMain.getInstance().init()&lt;/code&gt; in &lt;code&gt;onCreate()&lt;/code&gt; and &lt;code&gt;cleanup()&lt;/code&gt; method via &lt;code&gt;AwesomeLibMain.getInstance().cleanup()&lt;/code&gt; in &lt;code&gt;onDestroy()&lt;/code&gt; respectively. Here is the code of &lt;code&gt;MainActivity.java&lt;/code&gt; class in &lt;code&gt;app&lt;/code&gt; module&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;But thats not the point here, right! We do not want the app developer to be calling these themselves. Also what happens if you want to trigger more methods at specific points in lifecycle of Activity/Fragment? The dependence on app developer increases by many folds and expecting them to do everything right is a like a chink in the armour.&lt;/p&gt;

&lt;p&gt;Well let us solve this situation by integrating with Lifecycle Arch Components.&lt;/p&gt;

&lt;p&gt;The first thing we need to do is add the required dependencies to &lt;code&gt;build.gradle&lt;/code&gt; to each module.&lt;/p&gt;

&lt;p&gt;Add the below to the &lt;code&gt;build.gradle&lt;/code&gt; for &lt;code&gt;awesomelib&lt;/code&gt; module&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="s2"&gt;"android.arch.lifecycle:runtime:1.0.0"&lt;/span&gt;
&lt;span class="n"&gt;annotationProcessor&lt;/span&gt; &lt;span class="s2"&gt;"android.arch.lifecycle:compiler:1.0.0-beta1"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;and add the below to the &lt;code&gt;build.gradle&lt;/code&gt; for &lt;code&gt;app&lt;/code&gt; module&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="s2"&gt;"com.android.support:appcompat-v7:26.1.0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;Note: We are using the latest &lt;a href="https://developer.android.com/topic/libraries/support-library/revisions.html#26-1-0"&gt;support lib v26.1.0&lt;/a&gt;Â , which now has AppCompatActivity implementing &lt;a href="https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner.html"&gt;LifecycleOwner&lt;/a&gt; interface from &lt;a href="https://developer.android.com/topic/libraries/architecture/index.html"&gt;Architecture Components&lt;/a&gt;. More about this, later in the post. For now just know you need support lib &lt;code&gt;v26.1.0&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;… that done, sync your project.&lt;/p&gt;

&lt;p&gt;Now is the time for us to integrate Lifecycle Arch Components into our android library module, &lt;code&gt;awesomelib&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We start by first implementing &lt;a href="https://developer.android.com/reference/android/arch/lifecycle/LifecycleObserver.html"&gt;LifecyleObserver&lt;/a&gt; class in our &lt;code&gt;AwesomeLibMain&lt;/code&gt; class.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;From android official docs&lt;/strong&gt;: LifecycleObserver is an interface which does not have any methods, instead, relies on &lt;a href="https://developer.android.com/reference/android/arch/lifecycle/OnLifecycleEvent.html"&gt;OnLifecycleEvent&lt;/a&gt; annotated methods to mark the class for lifecycle events.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Now comes the magic&lt;/em&gt;&lt;/strong&gt;. Once you have integrated Lifecycle Arch components, you have access to annotations which correspond to lifecycle events of Activity/Fragment such as&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@OnLifecycleEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Lifecycle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ON_CREATE&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;or&lt;/span&gt; 
&lt;span class="nd"&gt;@OnLifecycleEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Lifecycle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ON_DESTROY&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="na"&gt;and&lt;/span&gt; &lt;span class="n"&gt;so&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Once you annotate the right method with the specific annotation, that method would be triggered at the specified lifecycle event state, during the execution of Activity/Fragment.&lt;/p&gt;

&lt;p&gt;Lets have a look at what our &lt;code&gt;AwesomeLibMain&lt;/code&gt; class look, after we have annotated the methods&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;as you can see I added more methods and annotated them to get triggered at specific lifecycle event state.&lt;/p&gt;

&lt;p&gt;But we are not done here yet. This just tells which method will be triggered when a specific lifecycle event state is reached for the Activity/Fragment. What is still required is to add our &lt;code&gt;AwesomeLibMain&lt;/code&gt; class as an &lt;strong&gt;Observer&lt;/strong&gt; for the lifecycle of the desired Activity. Turns out it is a really simple step, as can be seen below&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: &lt;code&gt;MainActivity&lt;/code&gt; here extends &lt;code&gt;AppCompatActivity&lt;/code&gt; because we are using the latest &lt;a href="https://developer.android.com/topic/libraries/support-library/revisions.html#26-1-0"&gt;support lib v26.1.0&lt;/a&gt;Â , which now has &lt;code&gt;AppCompatActivity&lt;/code&gt; implementing &lt;a href="https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner.html"&gt;LifecycleOwner&lt;/a&gt; interface from &lt;a href="https://developer.android.com/topic/libraries/architecture/index.html"&gt;Architecture Components&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;From android official docs&lt;/strong&gt;: &lt;a href="https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner.html"&gt;LifecycleOwner&lt;/a&gt; is a single method interface that denotes that the class has a &lt;a href="https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.html"&gt;Lifecycle&lt;/a&gt;. It has one method, &lt;a href="https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner.html#getLifecycle%28%29"&gt;&lt;code&gt;getLifecycle()&lt;/code&gt;&lt;/a&gt;, which must be implemented by the class.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you are using a version of support lib below &lt;code&gt;v26.1.0&lt;/code&gt; then &lt;code&gt;MainActivity&lt;/code&gt; will need to extend &lt;code&gt;LifecycleActivity&lt;/code&gt; and app module’s &lt;code&gt;build.gradle&lt;/code&gt; will need to include to get access to &lt;code&gt;LifecycleActivity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;implementation "android.arch.lifecycle:extensions:1.0.0-beta1&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;so you basically needed to add it as an &lt;strong&gt;Observer&lt;/strong&gt; for lifecycle in &lt;code&gt;onCreate()&lt;/code&gt; of Activity via&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Add Lifecycle Observer&lt;/span&gt;
&lt;span class="n"&gt;getLifecycle&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;addObserver&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AwesomeLibMain&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;…and then remove the Observer for lifecycle in onDestroy() of Activity via&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Remove Lifecycle Observer&lt;/span&gt;
&lt;span class="n"&gt;getLifecycle&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;removeObserver&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AwesomeLibMain&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thats it! No, really thats all there is. You have successfully integrated lifecycle arch components in your &lt;code&gt;awesomelib&lt;/code&gt; and now it is observing Activity Lifecycle events and triggering the right methods at the right time, without requiring the &lt;code&gt;app&lt;/code&gt; developer to worry about anything.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: This is the only step &lt;code&gt;app&lt;/code&gt; developer needs to do when going to use your android library in terms of integrating your library code in the activity, apart from adding it in the &lt;code&gt;build.gradle&lt;/code&gt; dependency section. All methods will be triggered automatically when a said lifecycle event state is reached during the execution of the Activity/Fragment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What I have tried to explain here is more like a proof of concept, I am sure one can very easily implement it in an existing android library code and make use of the benefits lifecycle arch components bring along.&lt;/p&gt;

&lt;p&gt;For people worried about adding a lot of method counts to their app, below is a snapshot of method counts (used &lt;a href="https://github.com/google/android-classyshark"&gt;Classyshark&lt;/a&gt; Tool) for the used arch component libs, &lt;code&gt;awesomelib&lt;/code&gt; module and the sample app itself (from the sample &lt;code&gt;app&lt;/code&gt; I have linked below)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VPnIKmMI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/a84t91sc13gsp3uqg6hz.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VPnIKmMI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/a84t91sc13gsp3uqg6hz.jpeg" alt="p3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Simple rundown shows the method count for&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;arch component libs–&lt;strong&gt;122 methods&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;awesomelib-&lt;strong&gt;5 methods&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So as you can see they are pretty light in terms of adding method count. Support library is part of the app module and something usually all apps nowadays have, so you are basically not shipping your android library with that. ðŸ˜…&lt;/p&gt;

&lt;p&gt;As mentioned at the start of this post, the code for above is available on github &lt;a href="https://github.com/nisrulz/android-examples/tree/develop/LifeCycleCompForLib"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have suggestions or maybe would like me to add something to the content here, please let me know.&lt;br&gt;
Till then keep crushing code ðŸ¤“&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;I gave a talk on this topic at &lt;a href="http://droidcon.de/en/sessions/things-i-wish-i-knew-when-i-started-building-android-sdklibraries"&gt;Droidcon Berlin ’17&lt;/a&gt;, checkout the slides &lt;a href="https://speakerdeck.com/nisrulz/libraries"&gt;here&lt;/a&gt;.This post got featured in &lt;a href="http://androidweekly.net/issues/issue-276"&gt;Android Weekly Issue 276&lt;/a&gt;! ðŸ˜Ž&lt;/p&gt;

&lt;p&gt;P.S. : This post is also published on &lt;a href="https://android.jlelse.eu/is-your-android-library-lifecycle-aware-127629d32dcc"&gt;medium.&lt;/a&gt; and &lt;a href="http://crushingcode.nisrulz.com/is-your-android-library-lifecycle-aware/"&gt;my own blog&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>android</category>
      <category>library</category>
      <category>lifecycle</category>
    </item>
    <item>
      <title>Want To Step Up Your Android Learning Game? You Need To Read This First</title>
      <dc:creator>Nishant Srivastava</dc:creator>
      <pubDate>Sun, 30 Jul 2017 06:28:30 +0000</pubDate>
      <link>https://dev.to/nisrulz/want-to-step-up-your-android-learning-game-you-need-to-read-this-first</link>
      <guid>https://dev.to/nisrulz/want-to-step-up-your-android-learning-game-you-need-to-read-this-first</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;DISCLAIMER: If you consider yourself a beginner, intermediate, expert or a ninja in the Android realm, this secret sauce still applies to you.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  TL;DR: Build Something.
&lt;/h5&gt;

&lt;p&gt;&lt;em&gt;No really! I am not kidding here. Hear me out on this.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fktvo1fkx6xjy9ee8ulps.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fktvo1fkx6xjy9ee8ulps.gif" alt="header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The secret to becoming better at Android development, with all the new shiny stuff being released every year and making you go through the fear of missing out and what not is just to build out something that’s functional, even before reading a blog post or documentation.&lt;/p&gt;

&lt;p&gt;It is really very easy to say you read up the documentation and you have gone through a blog post that explains the intricate concepts/a new library introduced by either Google or some 3rd party developer/company. I am not saying it’s not required or a waste of time because it’s very important to understand the concepts and then proceed with the using the new and shiny library or a new concept you just came to know about. You SHOULD always understand the underlying concepts to keep yourself from misusing the API/framework/architecture approach.&lt;/p&gt;

&lt;p&gt;But here is where I would like to pause and mention a different approach which has worked for me always and with people I have introduced this concept to. The concept isn’t anything new. It’s a simple extra step of approaching a new library/architecture in Android realm, which is to…&lt;/p&gt;

&lt;h5&gt;
  
  
  ..build a simple, isolated and bare bone example android app to see a working example of what your are trying toÂ learn.
&lt;/h5&gt;

&lt;p&gt;That’s all. I call this &lt;strong&gt;&lt;em&gt;Example Driven Development(EDD)&lt;/em&gt;&lt;/strong&gt; approachðŸ¤“.&lt;/p&gt;

&lt;p&gt;The reason this works out very well is that these simplified examples Android apps are devoid of any complexity you would have in your own apps. If you are like me who likes to see things working before jumping into implementing it in the complex app I am working on, this approach will be very interesting to you (unless of-course you are already aware of it and use it, in that case, jump to the bottom of this story ðŸ˜… )&lt;/p&gt;

&lt;p&gt;These simplified examples android apps also provide you opportunities to experiment with concept/library you are trying to implement as well as spot possible bugs or issues far before you would have done otherwise if you would have implemented it directly inside your complex Android app.&lt;/p&gt;

&lt;p&gt;Some people might argue that it is a time taking process and eventually a waste of time and energy because at the end of the day the example app is thrown out of the window and the concept/library has to be implemented into the android app you are working on.&lt;/p&gt;

&lt;p&gt;I strongly disagree to that point, with my own personal reasons to justify that (based on my experience). This might not be true for everyone, but I have found it to work for the majority of people I have suggested this approach. I believe such simplified example android apps are like&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Unit Tests&lt;/em&gt;&lt;/strong&gt;, simple and focused to understand the functionality and working of the concept/library.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Comments in code&lt;/em&gt;&lt;/strong&gt;, they are there for you to comeback to, understand how the concept/library works and plays along with on the platform at the most basic level.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;It wouldn’t make sense for me to advocate an approach if I can’t show how I use it for myself, correct?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So I happen to be following this approach for sometime now and it so happens some people in the community reached out to me to share the same with them. And thus &lt;a href="https://github.com/nisrulz/android-examples" rel="noopener noreferrer"&gt;Android Example&lt;/a&gt; came to exist as it is today…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nisrulz/android-examples" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fnisrulz%2Fandroid-examples%2Fraw%2Fdevelop%2Fimg%2Fgithub_banner.png" alt="android_examples"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;…I created this repo last year where I started putting in all the example apps I had built while working on a feature/library/concept as I was doing android development at work or while just learning something new for myself. Turns out it has grown quite big now with &lt;strong&gt;100+ android examples&lt;/strong&gt; (including &lt;a href="https://github.com/nisrulz/android-examples#example-apps-from-my-android-libraries" rel="noopener noreferrer"&gt;sample apps&lt;/a&gt; from &lt;a href="https://github.com/nisrulz/nisrulz.github.io#open-source-contributions" rel="noopener noreferrer"&gt;android libraries I have built myself&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Do check it out, to get a taste of what I have been blabbering about up until now. &lt;strong&gt;&lt;em&gt;Just jump in and build up one of the apps demonstrating concept/library implementation that you always wanted to understand and get your hands dirty hacking on the code!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Better still, contribute back to the repo with more (missing) simplified android examples or improving the ones that exist in the Github repo. Contributions of any size are always welcome.&lt;/p&gt;

&lt;p&gt;If you still think that this approach is flawed, I am more than happy to hear about anything else that has worked for you.&lt;/p&gt;

&lt;p&gt;If you have suggestions or maybe would like me to add something to the content here, please let me know.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;P.S: I did a write up sometime back about how I manage dependencies for such a huge number of example apps. If you are interested in that you can find that &lt;a href="http://crushingcode.nisrulz.com/update-dependencies-code-repeat/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post was originally shared on my blog &lt;a href="http://crushingcode.nisrulz.com/" rel="noopener noreferrer"&gt;Crushing C.O.D.E&lt;/a&gt;, &lt;a href="http://crushingcode.nisrulz.com/want-to-step-up-your-android-learning-game-you-need-to-read-this-first/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and is also published on &lt;a href="https://medium.com/@nisrulz/want-to-step-up-your-android-learning-game-you-need-to-read-this-first-e0cb9a7816a3" rel="noopener noreferrer"&gt;medium.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Checkout my &lt;a href="https://github.com/nisrulz" rel="noopener noreferrer"&gt;Github&lt;/a&gt; or simply follow me on &lt;a href="https://twitter.com/nisrulz" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; ðŸ˜&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Until next post keep crushing code ðŸ¤“&lt;/p&gt;

</description>
      <category>android</category>
      <category>edd</category>
      <category>example</category>
      <category>bestpractices</category>
    </item>
    <item>
      <title>Things I wish I knew when I started building Android SDK/Libraries</title>
      <dc:creator>Nishant Srivastava</dc:creator>
      <pubDate>Tue, 21 Mar 2017 18:07:54 +0000</pubDate>
      <link>https://dev.to/nisrulz/things-i-wish-i-knew-when-i-started-building-android-sdklibraries</link>
      <guid>https://dev.to/nisrulz/things-i-wish-i-knew-when-i-started-building-android-sdklibraries</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This post was originally published on &lt;a href="https://android.jlelse.eu/things-i-wish-i-knew-when-i-started-building-android-sdk-libraries-dba1a524d619" rel="noopener noreferrer"&gt;medium.&lt;/a&gt; and also featured in &lt;a href="http://androidweekly.net/issues/issue-249" rel="noopener noreferrer"&gt;Android Weekly&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fheader.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fheader.png" alt="header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It all starts when some android developer tries to figure out a solution to a problem he/she is having while building their &lt;strong&gt;"Awesome Android App"&lt;/strong&gt;. During the process, most developers would encounter a couple of issues and in tandem, to those, they would come up with possible solutions.&lt;/p&gt;

&lt;p&gt;Now here is a thing, if you are like me, who believes that if the problem was big enough for me to spend some time on it and there wasn’t an existing solution out there, I would abstract the whole solution in a modular manner, which eventually turns out to be an android library. Just so that whenever in future I encounter this problem again, I can reuse this solution easily.&lt;/p&gt;

&lt;p&gt;So far, so good. So you have built the library and probably started using it completely privately or if you think someone else could make use of the same solution you release the code as an android library i.e.you open source the code. I believe (..or rather that is what it looks like..) at this point everyone thinks they are done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WRONG!&lt;/strong&gt; This very point is where most people usually miss out that this android library code is going to be used by other developers that do not sit next to you and that to them this is just some android library they wish to use to solve a similar problem. The better your approach of designing the API the better the chances of making sure that the library will be used as it is intended to be and whoever is using it isn’t confused. It should be clear from the very start what needs to be done to start using the library.&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;em&gt;Why does this happen?&lt;/em&gt;
&lt;/h5&gt;

&lt;p&gt;The devs that write these android libraries are usually the ones who don’t care about the API design when building one. At least the majority of them don’t. Not because they are indifferent but I think most of them are just beginners and there is no set rules that they can look up to be better at designing the API. I was in the same boat some time back, so I can understand the frustration of not having a lot of information in this field.&lt;/p&gt;

&lt;p&gt;So I have had my experiences and I happen to release some of the code as android libraries (&lt;a href="https://github.com/nisrulz/android-tips-tricks#extra--android-libraries-built-by-me" rel="noopener noreferrer"&gt;which you can check out here&lt;/a&gt;). I have come up with a quick list of points which each and every developer who designs an API in the form of Android Library should keep in mind (some of them may apply to designing API in general too).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Point to note here, my list isn’t exhaustive and I may not be covering everything. It covers things I have encountered and wished I knew it when I started and thus I will keep on updating this post as and when I learn with more experience in the future.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we dive into anything let’s first answer the most basic questions that everyone would have regarding building Android SDK/Library. So here goes&lt;/p&gt;

&lt;h3&gt;
  
  
  Why would you create an android SDK/Library?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fquestion.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fquestion.gif" alt="question"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sure it is...well, you do not have to create an SDK/library in all cases. It makes more sense to decide on building one based on the understanding of what value you bring to the table. Ask yourself the below&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Is there some existing solution that would solve the problem?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
If your answer is Yes, then possible try and use that existing solution. &lt;br&gt;
Say that does not solve your specific problem, even in that scenario it is better to start by forking the code, modifying it to solve the problem and then using it versus starting from scratch.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Bonus Points&lt;/strong&gt; to you if you go ahead and submit a Pull Request to get the fix you made pushed into the existing library code so that the community can benefit from it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If your answer is No, then go ahead and build the android SDK/library. Share it with the world later on so that others can make use of it in their projects.&lt;/p&gt;
&lt;h3&gt;
  
  
  What are the packaging options for your artifacts?
&lt;/h3&gt;

&lt;p&gt;Even before you start building your library you need to decide on how do you want to deliver your artifacts to developers.&lt;br&gt;
Let me start here by describing some terms which we might use in the post here. To begin with let me describe what is an artifact first,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In general software terms, an &lt;strong&gt;"artifact"&lt;/strong&gt; is something produced by the software development process, whether it be software related documentation or an executable file.&lt;br&gt;
In Maven terminology, the artifact is the resulting output of a maven build, generally a &lt;code&gt;jar&lt;/code&gt; , &lt;code&gt;war&lt;/code&gt; , &lt;code&gt;aar&lt;/code&gt; or other executable files.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s look at the options you have&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Library Project&lt;/strong&gt;: Code that you have to checkout and link into your project. It is the most flexible one as you can modify the code once you have it in your code, but also introduces issues such as being in sync with upstream changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JAR&lt;/strong&gt;: &lt;strong&gt;J&lt;/strong&gt;ava &lt;strong&gt;AR&lt;/strong&gt;chive is a package file format typically used to aggregate many Java class files and associated metadata into one file for distribution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AAR&lt;/strong&gt;: &lt;strong&gt;A&lt;/strong&gt;ndroid &lt;strong&gt;AR&lt;/strong&gt;chive is similar to JAR with added functionality. Unlike JAR files, AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawable in addition to Java classes and methods.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  We have the artifact, Now what? Where does one host these artifacts?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Ftrickquestion.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Ftrickquestion.gif" alt="question"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not really...&lt;br&gt;
Turns out you have a couple of options here too, each having its own pros and cons. Let’s take a look at each&lt;/p&gt;
&lt;h5&gt;
  
  
  Local AAR
&lt;/h5&gt;

&lt;p&gt;If you happen to be very specific about not wanting to put your android library artifact into any repository, you can generate your local aar file and use that directly. Read &lt;a href="http://stackoverflow.com/a/28816265/2745762" rel="noopener noreferrer"&gt;this stackoverflow answer&lt;/a&gt; to understand how to do that.&lt;/p&gt;

&lt;p&gt;In a gist is you need to put the &lt;code&gt;aar&lt;/code&gt; file in the &lt;code&gt;libs&lt;/code&gt; directory (create it if needed), then, add the following code in your &lt;code&gt;build.gradle&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="k"&gt;dependencies&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;compile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;name:&lt;/span&gt;&lt;span class="s1"&gt;'nameOfYourAARFileWithoutExtension'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;ext:&lt;/span&gt;&lt;span class="s1"&gt;'aar'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;repositories&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;flatDir&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;dirs&lt;/span&gt; &lt;span class="s1"&gt;'libs'&lt;/span&gt;
       &lt;span class="o"&gt;}&lt;/span&gt;
 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;..what comes with this is that now whenever you want to share your android library you are passing around your &lt;code&gt;aar&lt;/code&gt; file(…which is not the best way to share your android library).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Avoid doing this as much as you can&lt;/strong&gt;, since it is prone to a lot of problems the biggest one being manageability and maintainability of the code base. &lt;br&gt;
Another issue with this approach is you cannot make sure that the users of this artifact are in sync with the latest code.&lt;br&gt;
Not to mention the whole process is lengthy and prone to human error, just to integrate the library in an android project.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  Local/Remote Maven Repositories
&lt;/h5&gt;

&lt;p&gt;&lt;em&gt;What if you wanted to use the android library privately?&lt;/em&gt;&lt;br&gt;
The solution for that is to deploy your own instance of artifactory (read about how to do that &lt;a href="http://jeroenmols.com/blog/2015/08/06/artifactory/" rel="noopener noreferrer"&gt;here&lt;/a&gt;) or using Github or Bitbucket repository as your own maven repository (read about how to do that &lt;a href="http://crushingcode.nisrulz.com/own-a-maven-repository-like-a-bosspart-1/" rel="noopener noreferrer"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Again &lt;strong&gt;this is specific to you using your android library privately. If you want to share this with others its not the approach you wanna stick to.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first issue that this approach has is that your artifact is in a private repository, to give access to this library you have to give access to the private repository which could be a security issue.&lt;br&gt;
The second issue is that to use your android library one would need to include an extra line in their root &lt;code&gt;build.gradle&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="k"&gt;allprojects&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;repositories&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;
        &lt;span class="n"&gt;maven&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="s1"&gt;'http://url.to_your_hosted_artifactory_instance.maven_repository'&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;..which to be fair is an extra step and we are all here to make the process simpler. It is easier on the creator part to push the android library out quickly but adds an extra step for the users to use the library code.&lt;/p&gt;

&lt;h5&gt;
  
  
  Maven Central, Jcenter or JitPack
&lt;/h5&gt;

&lt;p&gt;Now the easiest way to push it out immediately is via &lt;strong&gt;JitPack&lt;/strong&gt;. So you would want to do that. JitPack takes your code from a public git repository, checks out the latest release code, builds it to generate the artifacts and later publishes to their self-hosted maven repository.&lt;br&gt;
However, the issue at hand is same as the one for local/remote maven repositories that users of your android library would need to include an extra line in their root &lt;code&gt;build.gradle&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="k"&gt;allprojects&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;repositories&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;
        &lt;span class="n"&gt;maven&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="s1"&gt;'https://www.jitpack.io'&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read about how to publish your android library to JitPack &lt;a href="http://crushingcode.co/publish-your-android-library-via-jitpack/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The other option you have is of Maven Central or Jcenter. &lt;/p&gt;

&lt;p&gt;Personally &lt;strong&gt;&lt;em&gt;I would suggest you stick to Jcenter&lt;/em&gt;&lt;/strong&gt; as it well documented and better managed. It is also the default repository that is looked up for dependencies in Android projects (…unless someone changed it). &lt;/p&gt;

&lt;p&gt;If you publish to Jcenter, bintray the company behind it gives you the option to sync with Maven Central from within their publishing platform. Once published it is as simple as adding the below line to your build.gradle file to use the library in any android project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="k"&gt;dependencies&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="s1"&gt;'com.github.nisrulz:awesomelib:1.0'&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read about how to publish your android library to Jcenter &lt;a href="http://crushingcode.co/publish-your-android-library-via-jcenter/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With all those basic questions out of the way, let us look at things one should take care of while building an Android SDK/Library&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid multiple arguments
&lt;/h3&gt;

&lt;p&gt;Every android library has to be usually initialized with some arguments and to do that you would usually be passing a set of arguments to either a constructor or have an init function to setup your library. Whenever doing that consider the below&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Passing more than 2–3 arguments to your init() function is bound to cause more headaches than provide ease of use.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just because its hard to remember the exact mapping of these arguments and the order in which they are declared in the library code.It also is prone to more mistakes as anyone can make a mistake of passing &lt;code&gt;int&lt;/code&gt; value in a &lt;code&gt;String&lt;/code&gt; field or vice versa.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// DONOT DO THIS
void init(String apikey, int refresh, long interval, String type);
// DO this
void init(ApiSecret apisecret);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where ApiSecret is an Entity Class, declared as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApiSecret&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;apikey&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;refresh&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;interval&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// constructor&lt;/span&gt;
    &lt;span class="cm"&gt;/* you can define proper checks(such as type safety) and
     * conditions to validate data before it gets set
     */&lt;/span&gt;

    &lt;span class="c1"&gt;// setter and getters&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or you can also use &lt;code&gt;Builder Pattern&lt;/code&gt; as an alternative approach to the above.&lt;/p&gt;

&lt;p&gt;You can read more about Builder Pattern &lt;a href="https://sourcemaking.com/design_patterns/builder" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;a href="https://jlordiales.me/about/" rel="noopener noreferrer"&gt;JOSE LUIS ORDIALES&lt;/a&gt; talks in depth about how to implement it in your code, take a look &lt;a href="https://jlordiales.me/2012/12/13/the-builder-pattern-in-practice/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ease of use
&lt;/h3&gt;

&lt;p&gt;When building your android library, keep in mind the usability of the library and the methods you expose. It should be&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Intuitive&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For everything that's happening in the android library code, there should be some feedback either in the logs or in the view. Depends on what kind of an android library is being built. If it does something that cannot be comprehended easily, the android library basically “does not work in the language of devs. It should do what the user of android library expects it to do without having to look up the documentation.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Consistent&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The code for the android library should be well thought and should not change drastically between versions. Follow &lt;a href="http://semver.org/" rel="noopener noreferrer"&gt;semantic versioning&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Easy to use, Hard to misuse&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It should be easily understandable in terms of implementation and its usage in the first sight itself. The exposed public methods should have enough validation checks to make sure people cannot misuse its functionality other than what it was coded and intended for.Provide sane defaults and handle scenarios when dependencies are not present.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Feasy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Feasy.gif" alt="easy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Minimize Permissions
&lt;/h3&gt;

&lt;p&gt;In the current times, when everyone just wants to jump the road and ask as many permissions, you should pause and think about do you really need that extra permission. Take care of these points especially&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Minimize your permissions as much as you can.&lt;/li&gt;
&lt;li&gt;Use Intents to let dedicated apps do the work for you and return the processed result.&lt;/li&gt;
&lt;li&gt;Enable and disable your features based off if you have the permission for it. Do not let your code crash just because you do not have the said permission. If at all, you must educate the user well before requesting the permission and that why it is required. If possible have a fallback functionality if the permission isn’t approved.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is how you check if you have a said permission granted or not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;hasPermission&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;permission&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;checkCallingOrSelfPermission&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;permission&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;PackageManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PERMISSION_GRANTED&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some of the devs would say that they really need that specific permission, what to do in that case. Well, your library code should be generic for all types of apps that need the specific functionality. If you can provide hooks such as functions to let users of your android library pass the data you need the dangerous permission for. In that way, you do not force the devs to require a permission they do not want to. In absence of the permission provide a fallback implementation. Simple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Requiring GET_ACCOUNTS permission (as a requisite to use the 
 * library) is avoided here by providing a function which lets the 
 * devs to get it on their own and feed it to a function in the 
 * library.
 */&lt;/span&gt;
&lt;span class="nc"&gt;MyAwesomeLibrary&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEmail&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"username@emailprovider.com"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Minimize Requisites
&lt;/h3&gt;

&lt;p&gt;We have all been there. We have a specific functionality that requires that the device has a certain feature. The usual way you would approach this is by defining the below in your manifest file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;uses-feature&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;"android.hardware.bluetooth"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;..the problem with this is that when this is defined in the android library code, this would get merged into the app manifest file during the manifest-merger phase of &lt;code&gt;build&lt;/code&gt; and thus hide the app in Play Store for devices that do not have the Bluetooth unit (this is something the Play Store does as filtering). So basically an app that was earlier visible to a larger audience would now be visible to a smaller audience, just cause you added that to your library code.&lt;/p&gt;

&lt;p&gt;Well, that’s not we want, do we? Nope. So how do we solve this? &lt;br&gt;
&lt;em&gt;Well what you need to do is not include that &lt;code&gt;uses-feature&lt;/code&gt; in your manifest file for the android library but rather check for the feature during runtime in your code as below&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;feature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PackageManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;FEATURE_BLUETOOTH&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isFeatureAvailable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;feature&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPackageManager&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;hasSystemFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;feature&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;.. this way there is no entry in the manifest and once it merges into the app, it won’t let the app get filtered in the Play Store. &lt;/p&gt;

&lt;p&gt;As an added feature though if the feature is not available you can just disable the functionality in your library code and have some fallback functionality in place. It is a Win- Win for both the android dev who built the library and the dev who integrates the lib in their app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Support different versions
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fhowmany.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fhowmany.gif" alt="how many"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;_How many are out there exactly?_&lt;/center&gt;

&lt;p&gt;If you have a feature that’s available in a certain version of android, you should do the check for that in code and disable the feature if the version is lower than supported.&lt;br&gt;
As a rule of thumb support the full spectrum of versions via defining in &lt;code&gt;minSdkVersion&lt;/code&gt; and &lt;code&gt;targetSdkVersion&lt;/code&gt;. What you should do internally to your library code is check for the android version at runtime and enable/disable the feature or use a fallback.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Method to check if the Android Version on the device is greater than or equal to Marshmallow.&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isMarshmallow&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;VERSION&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SDK_INT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;VERSION_CODES&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;M&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Do not log in production
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fno.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fno.gif" alt="donot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;_Just DO NOT._&lt;/center&gt;

&lt;p&gt;Almost every time I am asked to test an app or an android library project the first thing that I have seen is that they log everything up in the open, in their release code.&lt;br&gt;
As a rule of thumb, never log in production. You should use &lt;a href="https://developer.android.com/studio/build/build-variants.html" rel="noopener noreferrer"&gt;build-variants&lt;/a&gt; with &lt;a href="https://github.com/JakeWharton/timber" rel="noopener noreferrer"&gt;timber&lt;/a&gt; to help you in the process to separate logging info in production vs debug builds. A simple solution can be to provide a &lt;code&gt;debuggable&lt;/code&gt; flag that the devs can flip to enable/disable logging from your android library&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In code     &lt;/span&gt;
&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;debuggable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;MyAwesomeLibrary&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apisecret&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;debuggable&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// In build.gradle     &lt;/span&gt;
&lt;span class="n"&gt;debuggable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Do not crash silently and fail fast
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fcrash.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fcrash.png" alt="crash"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have seen this a lot of times now. Some of the devs would not log their errors and exception in logcat! Which basically adds a headache to the users of the android library when they are trying to debug the code. In tandem to the last tip about not logging in production, you must understand that exceptions and errors need to be logged irrespective of being in debug or production. If you do not want to log in production, at least provide a functionality of enabling logs via passing some flag when you initialize your library. i.e&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ApiSecret&lt;/span&gt; &lt;span class="n"&gt;apisecret&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;debuggable&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
      &lt;span class="o"&gt;...&lt;/span&gt;
      &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debuggable&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
          &lt;span class="c1"&gt;// This is printed only when debuggable is true&lt;/span&gt;
          &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;....&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is important that your android library fails immediately and shows an exception to the user of your android library instead of being hung up on doing something. Avoid writing code which would block the Main Thread.&lt;/p&gt;

&lt;h3&gt;
  
  
  Degrade gracefully in an event of error
&lt;/h3&gt;

&lt;p&gt;What I mean by this is that when say your android library code fails, try to have a check so that the code would not crash the app instead only the functionality provided by your library code is disabled.&lt;/p&gt;

&lt;h3&gt;
  
  
  Catch specific exceptions
&lt;/h3&gt;

&lt;p&gt;Continuing with the last tip, you might notice that in my last code snippet I am using a &lt;code&gt;try-catch&lt;/code&gt; statement. Catch statement specifically catches all &lt;code&gt;Exception&lt;/code&gt; as its a base class. There is no specific distinction between one exception vs the other one. So what one must do is define specific types of &lt;code&gt;Exception&lt;/code&gt; as per the requirement at hand. i.e &lt;code&gt;NUllPointerException&lt;/code&gt;, &lt;code&gt;SocketTimeoutException&lt;/code&gt;, &lt;code&gt;IOException&lt;/code&gt;, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handle poor network conditions
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Floading.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Floading.gif" alt="poor connections"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;_…this gets on my nerves, seriously!_&lt;/center&gt;

&lt;p&gt;If the android library you wrote deals with making network calls, a very simple thing that usually goes unnoticed is that you should always consider a case of what happens if the network is slow or non-responsive.&lt;/p&gt;

&lt;p&gt;What I have observed is that library code developers assume that the network calls being made will always go through. A good example will be if your android library fetches some config file from the server to initialize itself. Now when developing the library the devs assume that the config file will always get downloaded. What they forget is that on a flaky network, the library code will not be able to download the config file and hence would crash the whole codebase. If simple checks and a strategy to handle such situations are built right into the android library code, it saves quite a number of people the headaches they would have otherwise.&lt;/p&gt;

&lt;p&gt;Whenever possible batch your network calls and avoid multiple calls. This also &lt;a href="https://developer.android.com/training/monitoring-device-state/index.html" rel="noopener noreferrer"&gt;saves a lot of battery&lt;/a&gt;, &lt;a href="https://developer.android.com/training/efficient-downloads/efficient-network-access.html" rel="noopener noreferrer"&gt;read here&lt;/a&gt;&lt;br&gt;
Reduce the amount of data you transfer over the network by moving away from JSON and XML to &lt;a href="https://google.github.io/flatbuffers/" rel="noopener noreferrer"&gt;Flatbuffers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.android.com/topic/performance/power/network/index.html" rel="noopener noreferrer"&gt;Read more about managing network here&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Reluctance to include large libraries as dependencies
&lt;/h3&gt;

&lt;p&gt;This one goes without much explanation. As most of fellow Android Devs would be knowing, there is a method count limit of 65K methods for android app code. Now say if you have a transitive dependency on a large library, you would introduce two undesirable effects to the android app your library is being included&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You will considerably increase the method count of the android app, even though your own library codebase has a low method count footprint since you would transitively download the larger library and thus it will contribute to the method count too.&lt;/li&gt;
&lt;li&gt;If the method count hits the 65K limit, just because of your library code that transitively downloaded the larger library, the app developer will be forced to get into the lands of multi-dexing. Trust me on this, no one wants to get into the multi-dexing world.
In such a scenario, your library has introduced a bigger problem than solving the initial problem. So most probably your library will be replaced by some other library that does not add to the method count or basically that takes care everything in a better way.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Do not require dependencies unless you very much have to
&lt;/h3&gt;

&lt;p&gt;Now this rule is something that I think everyone knows, right? Do not bloat your android libraries with dependencies you do not need. But the point to note here is that even if you need dependencies you do not have to make the users of the library download it transitively. i.e the dependency does not need to be bundled with your android library.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Well, then the question arises as to how do we use it if it is not bundled with our library?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well, the simple answer is you ask your users to provide that dependency to you during compile time. What this means is that not every user might need the functionality which requires the dependency. And for those users, if you cannot find the dependency as provided to you, you just disable the functionality in your code. But for those who need it, they will provide you the dependency, by including it in their build.gradle .&lt;br&gt;
How to achieve this? Check in classpath&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;hasOKHttpOnClasspath&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;forName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.squareup.okhttp3.OkHttpClient"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ClassNotFoundException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, you can use &lt;code&gt;provided&lt;/code&gt;(Gradle v2.12 and below) or &lt;code&gt;compileOnly&lt;/code&gt;(Gradle v2.12+)(&lt;a href="https://blog.gradle.org/introducing-compile-only-dependencies" rel="noopener noreferrer"&gt;Read here for complete information&lt;/a&gt;), so as to be able to get hold of the classes defined by the dependency during compile time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="k"&gt;dependencies&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// for gradle version 2.12 and below&lt;/span&gt;
   &lt;span class="n"&gt;provided&lt;/span&gt; &lt;span class="s1"&gt;'com.squareup.okhttp3:okhttp:3.6.0'&lt;/span&gt;
   &lt;span class="c1"&gt;// or for gradle version 2.12+&lt;/span&gt;
   &lt;span class="n"&gt;compileOnly&lt;/span&gt; &lt;span class="s1"&gt;'com.squareup.okhttp3:okhttp:3.6.0'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;A word of caution here, you can only use this functionality of requiring a dependency if its a complete java dependency. i.e if its an android library you want to include at compile time, you can not reference its transitive libs as well as resources which need to be present before compilation. A pure java dependency, on the other hand, has only java classes and they are the only ones that would be added to classpath during the compilation process.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Try not to hog the startup
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fworst.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fworst.gif" alt="worst"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;_no kidding…_&lt;/center&gt;

&lt;p&gt;What I mean by this is that, as soon as the app starts up try not to initialize your android library greedily. What that would tend to do is that it will increase the startup time for the App itself, even though the app does simply nothing at startup except off course initialize your android library.&lt;/p&gt;

&lt;p&gt;The solution to such a problem is to do all work of initializing off the main thread i.e in a new thread, async. Better if you use &lt;code&gt;Executors.newSingleThreadExecutor()&lt;/code&gt; and keep the number of thread to just one.&lt;/p&gt;

&lt;p&gt;Another solution would be to initialize components of your android library &lt;strong&gt;on demand&lt;/strong&gt; i.e &lt;em&gt;Load them up/initialize them only when they are needed.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Remove functionality and features gracefully
&lt;/h3&gt;

&lt;p&gt;Do not remove your public functions between versions as that would lead the builds of many users of your android library break and they would be clueless as to why did that even happen.&lt;/p&gt;

&lt;p&gt;Solution: Deprecate the functions by marking them &lt;a class="mentioned-user" href="https://dev.to/deprecated"&gt;@deprecated&lt;/a&gt; and then define a roadmap of their removal in future versions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make your code Testable
&lt;/h3&gt;

&lt;p&gt;Making sure you have tests in your code isn’t actually a rule to follow. You should be doing this everywhere and for every project app or library without saying.&lt;/p&gt;

&lt;p&gt;Test your library code by making use of Mocks, avoiding final classes, not having static methods, etc.&lt;/p&gt;

&lt;p&gt;Writing code with interfaces around your public API also makes your android library capable of swapping implementations easily and in turn, makes the code more testable.i.e you can provide mock implementations easily when testing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Document Everything!
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fdavinci.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fdavinci.gif" alt="document"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Being the creator of the android library you would know about your code, but the people who are going to use it won’t know about it unless you expect them to figure out by reading your source code (you should never need that).&lt;/p&gt;

&lt;p&gt;Document your library well including every detail about how to use it and detailing every feature you have implemented.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Readme.md file and place it at the root of your repository.&lt;/li&gt;
&lt;li&gt;Have Javadoc comments in your code, covering all public functions.
They should cover and explain 

&lt;ul&gt;
&lt;li&gt;Purpose of the public method&lt;/li&gt;
&lt;li&gt;The arguments passed&lt;/li&gt;
&lt;li&gt;Return type&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Bundle a sample app which demonstrates a working example of how the library and its features are used.&lt;/li&gt;
&lt;li&gt;Make sure you keep a detailed change log for your changes. A good place to do that would be to add the information right in your release section for the specific version tag.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fchangelog.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Fchangelog.jpeg" alt="changelog"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;_Screenshot of Github Releases section for Sensey android library_&lt;/center&gt;

&lt;p&gt;…and &lt;a href="https://github.com/nisrulz/sensey/releases" rel="noopener noreferrer"&gt;here is the link to releases section&lt;/a&gt; for &lt;a href="https://github.com/nisrulz/sensey" rel="noopener noreferrer"&gt;Sensey&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Provide a most minimalistic Sample App
&lt;/h3&gt;

&lt;p&gt;This goes without saying. Always provide the most minimalistic Sample app with your library code, as that is the first thing other devs will checkout to understand a working example of using your android library. &lt;/p&gt;

&lt;p&gt;The simpler it is the easier it is to understand. Making the sample app look fancy and code complex would only undermine the actual goal of the sample app, that is to provide a working example of using your android library.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consider putting up a License
&lt;/h3&gt;

&lt;p&gt;Most of the time developers forget about the Licensing piece. This is one factor that decides the adoption of your android library.&lt;/p&gt;

&lt;p&gt;Say you decided to license your android library in a restrictive manner i.e Using &lt;strong&gt;GPL&lt;/strong&gt; license, would mean that whoever uses your library and makes modification will have to contribute back to your codebase in order to keep using the android library. Putting such restrictions hampers the adoption of android libraries and developers tend to avoid such codebases.&lt;br&gt;
The solution to this is that you stick to more open licenses such as &lt;strong&gt;MIT&lt;/strong&gt; or &lt;strong&gt;Apache 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Read about licensing at this &lt;a href="https://choosealicense.com/" rel="noopener noreferrer"&gt;simple site&lt;/a&gt; and about need of &lt;a href="http://jeroenmols.com/blog/2016/08/03/copyright/" rel="noopener noreferrer"&gt;copyright in your code here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Last but not the least, get feedback
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Ffeedback.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fcrushingcode.github.io%2Fimages%2Fposts%2Fthings-i-wish-i-knew-when-i-started-building-android-sdk-libraries%2Ffeedback.gif" alt="feedback"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yeah, you heard that right! Your android library was built to cater to your needs initially. Once you put it out for others to use, you will come to know a lot of issues in it. Hear out your fellow devs and gather feedback. Act on it considering and weighing on the functionality to introduce or fix while maintaining the goals of the android library intact.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;In short, you need to take care of the below points while building&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid multiple arguments&lt;/li&gt;
&lt;li&gt;Ease of use&lt;/li&gt;
&lt;li&gt;Minimize permissions&lt;/li&gt;
&lt;li&gt;Minimize requisites&lt;/li&gt;
&lt;li&gt;Support different versions&lt;/li&gt;
&lt;li&gt;Do not log in production&lt;/li&gt;
&lt;li&gt;Do not crash silently and fail fast&lt;/li&gt;
&lt;li&gt;Degrade gracefully in an event of error&lt;/li&gt;
&lt;li&gt;Catch specific exceptions&lt;/li&gt;
&lt;li&gt;Handle poor network conditions&lt;/li&gt;
&lt;li&gt;Reluctance to include large libraries as dependencies&lt;/li&gt;
&lt;li&gt;Do not require dependencies unless you very much have to&lt;/li&gt;
&lt;li&gt;Try not to hog the startup&lt;/li&gt;
&lt;li&gt;Remove features and functionalities gracefully&lt;/li&gt;
&lt;li&gt;Make your code testable&lt;/li&gt;
&lt;li&gt;Document everything&lt;/li&gt;
&lt;li&gt;Provide a most minimalistic sample app&lt;/li&gt;
&lt;li&gt;Consider putting up a license&lt;/li&gt;
&lt;li&gt;Get feedback, lots of them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a rule of thumb follow the rule of SPOIL-ing your Library&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simpleâ€Š–â€ŠBriefly and Clearly expressed&lt;/li&gt;
&lt;li&gt;Purposefulâ€Š–â€ŠHaving or showing resolve&lt;/li&gt;
&lt;li&gt;OpenSourceâ€Š–â€ŠUniversal Access, Free license&lt;/li&gt;
&lt;li&gt;Idiomaticâ€Š–â€ŠNatural to the native environment&lt;/li&gt;
&lt;li&gt;Logicalâ€Š–â€ŠClear, Sound Reasoning&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I read this some time back in a presentation by some author I cannot recall. I took note of it as it makes a lot of sense and provides a clear picture in a very concise manner. If you know who the author is, please comment it and I will add his link and give due credit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Ending Thoughts
&lt;/h3&gt;

&lt;p&gt;I hope this post helps fellow android devs in building better android libraries. Android Community benefits extensively from using android libraries published daily by fellow android devs and if everyone starts to take care of their API design process keeping in mind the end user (other android developers) we would all be a step closer to an even better ecosystem as a whole.&lt;/p&gt;

&lt;p&gt;These guidelines are compiled on my experience of developing android libraries. I would love to know your views on the pointers mentioned above. Please leave a comment, and let me know!&lt;/p&gt;

&lt;p&gt;If you have suggestions or maybe would like me to add something to the content here, please let me know.&lt;/p&gt;

&lt;p&gt;Checkout my &lt;a href="https://github.com/nisrulz" rel="noopener noreferrer"&gt;Github&lt;/a&gt; or simply follow me on &lt;a href="https://twitter.com/nisrulz" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and keep on crushing code!ðŸ¤“ ðŸ˜&lt;/p&gt;

</description>
      <category>android</category>
      <category>library</category>
      <category>sdk</category>
      <category>bestpractices</category>
    </item>
    <item>
      <title>I could not find a simple Gesture Detection android library, so I built one</title>
      <dc:creator>Nishant Srivastava</dc:creator>
      <pubDate>Sun, 19 Mar 2017 06:01:18 +0000</pubDate>
      <link>https://dev.to/nisrulz/i-could-not-find-a-simple-gesture-detection-android-library-so-i-built-one</link>
      <guid>https://dev.to/nisrulz/i-could-not-find-a-simple-gesture-detection-android-library-so-i-built-one</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This post was originally published on &lt;a href="https://android.jlelse.eu/i-could-not-find-a-simple-gesture-detection-android-library-so-i-built-one-334c0a307c16"&gt;medium.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While working on various projects, there have been times when I have had to implement various gesture-based events. Every time I have had to do that I had to write the whole code for getting the &lt;code&gt;SensorManager&lt;/code&gt; and getting access to the &lt;code&gt;SensorEvent&lt;/code&gt;, plus the extra logic to detect the gesture. At first, it was just fine to do that since I was a beginner around that time. Then soon I got weary of the whole boilerplate. Around this time I started looking for a solution like an android library which would help me avoid all that boilerplate as well as enable me to maintain my code properly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Well, the problem at hand is basically the boilerplate code and the need to maintain your code. Duplicating code across projects and remembering the logic for each gesture was bound to end up in one of the projects in some form of error. It was no doubt inefficient and very repetitive. I was looking for a solution to all this because as Software Engineers we all strive to automate tasks or remove the need for duplicating code. As Douglas Crockford has said&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Code reuse is the Holy Grail of Software Engineering.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What was disappointing was that everything available was either too complex in terms of implementation or was very limiting.&lt;br&gt;
&lt;strong&gt;..and that's when I decided to write my own Android Library, Sensey.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Dku3o6V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AeQTvz7o0YIYTGgaW_VLOVA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Dku3o6V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AeQTvz7o0YIYTGgaW_VLOVA.png" alt="header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sensey&lt;/strong&gt; was born out of a need to be able to make Gesture Detection in android as simple as it can get. Not everyone needs to look up &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, &lt;code&gt;z&lt;/code&gt; values of &lt;strong&gt;&lt;em&gt;Accelerometer&lt;/em&gt;&lt;/strong&gt; or &lt;code&gt;Azimuth&lt;/code&gt;, &lt;code&gt;Pitch&lt;/code&gt; and &lt;code&gt;Roll&lt;/code&gt; values of &lt;strong&gt;&lt;em&gt;Orientation Sensor&lt;/em&gt;&lt;/strong&gt;*. There are other devs who only wish to detect if a certain Gesture was detected or not (period).&lt;/p&gt;

&lt;p&gt;So when I set out to build &lt;strong&gt;Sensey&lt;/strong&gt;, I had a very clear mindset that I want it to be exactly thatâ€Š–â€Šsimple and clean.&lt;br&gt;
When I first wrote Sensey, it wasn't perfect. But it did the job well for me. Since I couldn't find the solution for myself in the first place, I guessed (or assumed) that at least a few devs in the community would be in the same boat as me and thus from the start I was sure to make it open sourced (&lt;a href="https://github.com/nisrulz/sensey"&gt;You can check it out on Github&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--osqmXmk6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A4dHLn1anDpZ_PKLulYI5dQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--osqmXmk6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A4dHLn1anDpZ_PKLulYI5dQ.png" alt="sample app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I recently released the version &lt;code&gt;1.6.0&lt;/code&gt; of &lt;strong&gt;Sensey&lt;/strong&gt; and you can tell that it has come a long way from what it was in version &lt;code&gt;1.0&lt;/code&gt;. Let's walk through the process of integrating Sensey.It is super simple to integrate Sensey in your current android project. &lt;/p&gt;

&lt;p&gt;Simply add the below to your build.gradle file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="s1"&gt;'com.github.nisrulz:sensey:1.6.0'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;..sync your gradle and then initialize Sensey in your activity&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Sensey&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Sensey at this point is integrated into your app. Now based on what kind of Gesture you want to detect, you will need to start and stop detectors provided by Sensey.&lt;/p&gt;

&lt;p&gt;Say for the sake of a simple example, if we wanted to detect a Shake Gesture. All you need to do is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an instance of ShakeListener
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;ShakeDetector&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ShakeListener&lt;/span&gt; &lt;span class="n"&gt;shakeListener&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ShakeDetector&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ShakeListener&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onShakeDetected&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// Shake detected, do something&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="nd"&gt;@Override&lt;/span&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onShakeStopped&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// Shake stopped, do something&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to start listening for Shake gesture, pass the instance &lt;code&gt;shakeListener&lt;/code&gt; to &lt;code&gt;startShakeDetection()&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Sensey&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;startShakeDetection&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shakeListener&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To stop listening for Shake gesture, pass the instance &lt;code&gt;shakeListener&lt;/code&gt; to &lt;code&gt;stopShakeDetection()&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Sensey&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;stopShakeDetection&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shakeListener&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yup, that's about all it takes to know if a shake gesture was performed and when it stopped.&lt;/p&gt;

&lt;p&gt;Let's check out the complete list of gesture detectors Sensey provides&lt;/p&gt;

&lt;h4&gt;
  
  
  Flip Detector
&lt;/h4&gt;

&lt;p&gt;Sensey can detect if your device facing up or facing down. Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#flip"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Light Detector
&lt;/h4&gt;

&lt;p&gt;Sensey can detect if your device in a position where it is receiving any light or not. Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#light"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Orientation Detector
&lt;/h4&gt;

&lt;p&gt;Sensey can detect if your device's top side is up or bottom side is up. It can tell if your device's left side is up vs right side being up. Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#orientation"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Proximity Detector
&lt;/h4&gt;

&lt;p&gt;Sensey can detect if there is an object in the proximity of the device i.e either it is far or near to the device. Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#proximity"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Shake Detector
&lt;/h4&gt;

&lt;p&gt;Detects a Shake gesture performed on the device and also when the gesture stops.Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#shake"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Wave Detector
&lt;/h4&gt;

&lt;p&gt;This one is a simple implementation. It uses proximity sensor with timing to detect if a hand wave gesture is performed in front of the device screen. Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#wave"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Chop Detector
&lt;/h4&gt;

&lt;p&gt;Added in release 1.6.0 this one is brand new. It allows you to detect if a Chop gesture is performed with the device. Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#chop"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Wrist Twist Detector
&lt;/h4&gt;

&lt;p&gt;Added in release 1.6.0 this one is another brand new detector. This one allows you to detect if the wrist was twisted in a circular manner while holding the device. Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#wristtwist"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Movement Detector
&lt;/h4&gt;

&lt;p&gt;Another brand new detector added in release 1.6.0, it allows you to know if the device was moved or not. Once moved, it will also detect when the device becomes stationary. Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#movement"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Sound Level Detector
&lt;/h4&gt;

&lt;p&gt;Another brand new detector added in release 1.6.0, it allows you to measure the loudness of sound in the environment around the device (calculated on a scale of dB i.e decibel).Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#soundlevel"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pinch Scale Detector
&lt;/h4&gt;

&lt;p&gt;Sensey can use this detector to know if the user is executing a pinch scale in or out gesture on the touch screen. It will also be able to detect when the scale gesture was stopped. Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#pinchscale"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Touch Type Detector
&lt;/h4&gt;

&lt;p&gt;This one can do a lot of things, you can choose which one to use or not to use. It can detect when someone on the device screen does the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single Tap&lt;/li&gt;
&lt;li&gt;Double Tap&lt;/li&gt;
&lt;li&gt;Two Finger Tap&lt;/li&gt;
&lt;li&gt;Three Finger Tap&lt;/li&gt;
&lt;li&gt;Scroll Up, Down, Left or Right&lt;/li&gt;
&lt;li&gt;Swipe Left or Right&lt;/li&gt;
&lt;li&gt;Long Press&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check out the implementation &lt;a href="https://github.com/nisrulz/sensey/wiki/Usage#touchtype"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Phew! That's a lot of gesture detectors packed inside one android library. Well, the list will keep on growing when more gestures get implemented in future releases. The core focus though would always remain the same,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;simplicity over complexity and ease of use.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Who is it for
&lt;/h3&gt;

&lt;p&gt;For people who simply want to detect gesture as an event.&lt;/p&gt;

&lt;h3&gt;
  
  
  Who is it not for
&lt;/h3&gt;

&lt;p&gt;For anyone who wishes to get more advance and wants to tap into the raw SensorEvent values.&lt;/p&gt;

&lt;p&gt;That said, the android library isn't perfect and I would love it if you can try it out and file &lt;a href="https://github.com/nisrulz/sensey/issues"&gt;issues&lt;/a&gt; on Github to make it better.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nisrulz/sensey"&gt;Check out Sensey on Github (Star the repo if you find it useful)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have suggestions, please let me know in the comment section.&lt;/p&gt;

&lt;p&gt;Checkout my &lt;a href="https://github.com/nisrulz"&gt;Github&lt;/a&gt; or simply follow me on &lt;a href="https://twitter.com/nisrulz"&gt;Twitter&lt;/a&gt; and keep on crushing code!ðŸ¤“ ðŸ˜&lt;/p&gt;

</description>
      <category>android</category>
      <category>sdk</category>
      <category>gesture</category>
      <category>detection</category>
    </item>
    <item>
      <title>Hi, I'm Nishant.</title>
      <dc:creator>Nishant Srivastava</dc:creator>
      <pubDate>Sat, 18 Mar 2017 05:05:14 +0000</pubDate>
      <link>https://dev.to/nisrulz/hi-im-nishant</link>
      <guid>https://dev.to/nisrulz/hi-im-nishant</guid>
      <description>&lt;p&gt;Hi, I'm &lt;a href="http://www.nisrulz.com/" rel="noopener noreferrer"&gt;Nishant Srivastava&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I am a Software Engineer/Founding Team Member (at) OmniLabs, Inc. with 4 years of experience in Android SDK Engineering, DSP and Interaction Design. I am an avid dreamer, doodler, FOSS enthusiast and a caffeine-dependent life-form.&lt;/p&gt;

&lt;p&gt;I am also the lead organizer (at) Google Developer Group, New Delhi.&lt;/p&gt;

&lt;p&gt;I am recommended by 4 out of 5 people that recommend things ;)&lt;/p&gt;

&lt;p&gt;You can find me on &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Twitter as &lt;a href="https://twitter.com/nisrulz" rel="noopener noreferrer"&gt;@nisrulz&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Github as &lt;a href="https://github.com/nisrulz" rel="noopener noreferrer"&gt;nisrulz&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>introduction</category>
    </item>
  </channel>
</rss>
