Setting up Cucumber Android with Android Studio

I was setting up Cucumber for my Android project. Cucumber is a BDD way to write tests.

Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable domain-specific language and serves as documentation, automated tests and development-aid – all rolled into one format.

I use Gradle. This link from the official GitHub account of Cucumber should be your starting point.

  1. Download the latest version of the following files from the public maven Repo:
    • cucumber-android.jar
    • cucumber-core.jar
    • cucumber-html.jar
    • cucumber-java.jar
    • cucumber-junit.jar
    • cucumber-jvm-deps.jar
    • gherkin.jar

2. Put these files into “project-root / app / libs” and add this dependencies block of app/build.gradle

3. Gradle Sync. If you get any errors, check the list of errors below.

4. Add a new AndroidManifest.xml for your androidTest project in this location – project-root / app / src / androidTest / java /

5. Add a new class (eg – CucumberTestCase) that extends AndroidTestCase at “project-root/ app / src / androidTest / java / com / duggout /” with the below code:

6. Add a new class (eg – TestSteps) at “project-root / app / src / androidTest / java / com / duggout / steps /” to write your steps:

7. Write your first feature file at “project-root / app / src / androidTest / java / com / duggout / assets / features / “:

8. Run the feature test within Android Studio using Ctrl + Shift F10.

Screen Shot 2015-09-26 at 2.44.25 PM

I faced the following errors when setting up:

  1. Gradle Sync fails

Error:Execution failed for task ‘:app:preDexDebug’. org.gradle.process.internal.ExecException: Process ‘command ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/java” finished with non-zero exit value 1

Since this message is not very helpful, you need to use the gradle command to find the actual cause of this error.

/gradlew –info clean assembleDebug

For me, the issue was with cucumber-jvm-deps.jar (version 1.0.5). It had an incompatible class for Java 8. I downgraded it to 1.0.3 to solve it.

2. Build variants – Open Build Variants Tool Window in Android Studio and set the build type to Instrumentation

3. If you get “Undefined step” error, make sure you have created a class that extends AndroidTestCase and added CucumberOptions annotation with necessary parameters

4. If you get “java.lang.RuntimeException: Stub! ” in your JUnit assertEquals method, add junit in testCompile in your build.gradle as the first dependency. Or the 2nd dependency after the compile for libs directory.

5. When running from the command line using ‘./gradlew –info cC’, if you get “cucumber.runtime.CucumberException: No backends were found. Please make sure you have a backend module on your CLASSPATH.”, remove “@RunWith(Cucumber.class)” from the file

#HelloElixir – 5 Reasons to get started


Elixir was started in 2011 by Josè Valim, one of the core contributors to Ruby on Rails. Elixir is not Ruby for Erlang.

  1. Compiled and Scripted

    • Runs on Erlang VM – Elixir is written on top of Erlang. Elixir code is compiled to BEAM code that runs on Erlang VM. So you get the same performance as running an Erlang code. Erlang VM is popular for running low-latency, distributed and fault-tolerant systems.
    • REPL – Elixir can also be run as scripts without the need to compile. It has a REPL named iex (similar to irb for ruby) where you can learn to write simple Elixir code. “Everything is an expression” in Elixir.
    • Interoperate with Erlang modules – You can mix native Erlang modules when Elixir does not have support for something. Just prefix the module name with a colon.
    • Immutability – Everything is immutable in Elixir.
    • Metaprogramming – Elixir supports metaprogramming. Code eval:

      It can also compile code on the fly:
  2. Actor Model and “Shared Nothing”

    • Process-oriented – Every piece of code in Elixir runs under a process. This is different from the processes in OS. Processes in Elixir are light-weight and it is common to have thousands of processes running in a single machine. Even iex is running under a process.

      Processes communicate via messages. Just like your mailboxes. You can send and receive messages in your process. An example to send a message to self.

      I have used pattern matching above to capture the message into the ‘content’ variable. Another example to send a message to a spawned process:

    • Purely functional – There are no .new()/objects/instances in Elixir. The only way to maintain some state is to create a Process and make it respond with state information. Because of this nature, one has to use the Module methods to do an operation on a type. For example, to find the first item in a list:

      When we would want to chain multiple methods, the syntax becomes unreadable. Elixir solves this with the pipeline operator |>. Here is an example from Elixir Express:

      Without Pipeline operator:

      With Pipeline operator:

      Basically, the output of the first expression is passed as the first argument to the second method.

    • Concurrent – Because of it’s process oriented design, it is natural that concurrency is at its heart.
  3. Fault Tolerant and Distributed Programming

    • Supervisors – In a typical Java application, we would do this:

      One of Elixir’s philosophy is “fail fast”. Though Elixir supports try/rescue, it recommends developers to use only when really required. When you invoke a task, you can invoke it via Supervisors. When a child task crashes for some reason, the Supervisor ensures that the child is restarted to the state where it was known to work the last time.

    • Map-Reduce – Elixir let’s you run a CPU/Network intensive task on multiple nodes and aggregate them as tasks are complete. In my example, the reducer task is started only when all map tasks are complete. To parallelize map and reduce, follow this. Let’s pick up the usual task of grouping words by their length:

      I am submitting parallel tasks only for Map. Reduce task runs from the master machine itself. Submitting tasks and getting the output: 

  4. Rails like Web Framework

    • Phoenix Framework built with Elixir – You should check out the site.

      “A productive web framework that does not compromise speed and maintainability”

    • MVC – You know what it is
    • Ecto – Model, Migration, Validators etc.,
    • Channels – Websockets support
    • TDD
    • Build tools
    • Asset Pipeline
    • Screen Shot 2015-08-09 at 3.09.16 PM
      But, can Rails scale? Phoenix can.
  5. Hot Code Reloading and Non Stop Applications

    • What is it?  – Erlang VM can hold two versions of the same module in its memory. When you are making a process call to this module, VM provides the latest version. So the Module with the old version dies automatically when no processes are actually holding it. Read more about it here and here. Imagine an app where you don’t need to upload the new JAR and restart the service.
    • An example – I can update a Module’s definition from another host when the process is running. Here is a GIF that shows two IEx sessions – one updating the code of the other while the session is running:
    • Elixir - Hot code reloading
Ofcourse, there is much more to the list – Pattern matching, rich data structures, Mix tool, OTP, Lazy loading, Streams, Generators etc.,
Elixir is mature enough and the eco system is fairly big enough to be able to use it in Production.
Elixir Community is on Slack.

#HelloElixir – Ruby like binding.pry in Elixir

One of the best debugging tools in Ruby is it’s ability to pause at any point in code using binding.pry. Elixir has a similar one too – IEx.pry.

For a sample module like this:

Here is how you would use it:

You could evaluate the local variables that were in scope before the pry session was invoked. You could change the value of those variables in the pry session but it will not affect the values of the variables in the original IEx session.

Invoke respawn command to exit the pry session.

Android: Drag to Refresh with ListView

Providing ‘Drag to Refresh’ support for an Activity with only ListView is a problem. Since ListView itself has a scroll functionality, it conflicts with the scroll of SwipeRefreshLayout.

The current behaviour that begins the refresh when we just want to scroll the list to the top:

Scrolling Conflict

To fix this, you need to override the onScrollStateChanged and onScroll of your ListView’s setOnScrollListener.

Build the layout XML of your activity like this:

You will have a root RelativeLayout. It’s only child is a SwipeRefreshLayout. It’s only child again is a LinearLayout which holds your ListView and an optional loading panel.

And in your Activity’s onCreate method:

You will enable the swipeContainer only when the ListView is already scrolled to the top.

See how it is fixed now:


Did this fix the scrolling problem?

Phoenix Framework – Ecto Cheat Sheet

For all the examples below, we are using the following names:
Project name – HelloPhoenix

Model name – Message with two columns – content and owner

1. Create an Ecto Model:

phoenix.gen.model generates only the model.

phoenix.gen.html generates Controllers, Models and Templates.

2. Persist a model in database:

  1. Create a map using the attributes of your model and the values.
  2. Create a changeset using Model.changeset(ModelClass, map). Changeset is an intermediate dataset on which validations are performed.
  3. Repo.insert!(changeset) to do the actual insert.

3. Fire a SELECT query

a. Using a model: Select 10 messages ordered by inserted_at desc.

b. Using raw SQL:

If you do not have a model defined, you can use the table name directly as a string.

generates a SQL: select name from posts where > 0;

For custom raw SQL:

4. Update a model

  1. Get the existing model using Repo.get(ModelClass, primary_key_value)
  2. Mutate the model by applying the new values using | operator.
  3. Update with Repo.update(model)


100 words you should use if you are a ThoughtWorker

  1. Smell
  2. Does it scale?
  3. RESTful
  4. Build
  5. Merge Conflicts
  6. WFH
  7. Pairing
  8. Retro
  9. DevOps
  10. End-Point
  11. Pipeline
  12. Is it covered?
  13. Concurrent
  14. Hack
  15. Agile
  16. CRUD
  17. Microservices
  18. Design Pattern
  19. Devbox
  20. UX
  21. Defect
  22. HackerNews
  23. Sign up
  24. Dev Huddle
  25. PubSub
  26. Feedback
  27. Yoda
  28. Transaction
  29. Release
  30. Standup
  31. Context
  32. Roll off
  33. Is it in Prod?
  34. Expense
  35. Community
  36. Travel
  37. Log
  38. Delivery
  39. Team outing
  40. Bootcamp
  41. People Champion
  42. Clone
  43. Open Culture
  44. In Analysis
  45. MacBook
  46. Catchup
  47. Infrastructure
  48. Ready for Sign off
  49. Recruitment
  50. Fork
  51. Immersion
  52. Flat structure
  53. Intern
  54. Spike
  55. Process
  56. Git Patch
  57. Last Mile
  58. In Dev
  59. Lateral
  60. Global Assignment aka GA
  61. Low latency
  62. Sick Leave
  63. Grad
  64. Customer
  65. Inception
  66. STEP
  67. Campus
  68. Iteration
  69. Scrum
  70. Thought Process
  71. Exit
  72. Culture
  73. Local Market
  74. Callback
  75. Fire and Forget
  76. Benefits
  77. QA
  78. University
  79. Sub Millisecond
  80. Timesheet
  81. Value Add
  82. Vacation
  83. Atomic
  84. Volleyball
  85. Deploy
  86. Meetup
  87. Parked
  88. Black box
  89. Training
  90. Downstream
  91. Go Live
  92. Threads
  93. Away Day
  94. Policy
  95. Conference
  96. DropBox
  97. Check-in
  98. Credibility
  99. Performance
  100. Pass/Pursue


I am happy to announce to public. It was in beta with internal testing with my friends for the past few months. If you watch Cricket, then you should start using the site right away!

Some highlights from the site:

Player Profile, Stats and News, MS Dhoni’s profile:

MS Dhoni - Player profile



Open Cricket Search: Search for anything in Cricket

For example, ask when was the last time south africa chased down 400+ successfully? and you’ll get the answer as “12 March 2006”

when was the last time south africa chased down 400+ successfully?

Slack – Hyperlink Message Format – Markdown

Slack does not support hyperlinks in your message format when you type in Desktop app or Web app. But it does support hyperlinks when you use the API. The message format is “<url|text>”. An example below:

So when you post the above message to Slack using its webhook API:

It looks like this below:

Slack Hyperlink Markdown Format

Faster Autocomplete with typeahead.js, Elasticsearch & nginx

ElasticSearch is a search server built on top of Apache Lucene. You can push millions of documents into its throat, it will index them in real-time. And when you search for a query, it returns the matching documents faster than you blink (which takes 100ms atleast). Can’t believe? Check the numbers below:

I am searching for “sachin stats” against 300,000 documents. The result is in seconds. 0.019 is 19 ms.

Now we need to connect this search API into our typeahead js UI. There are two options:

  1. Expose the Elasticsearch port 9200 over the web. This can be dangerous. Anyone would be able to scan your entire index.
  2. Use nginx reverse proxy as a middleman to redirect requests for a specific URL (say /autocomplete) to the corresponding Elasticsearch API.

How you would do option 2:

Nginx configuration for the reverse proxy:

opencricket in the above configuration is the index name in Elasticsearch. So all requests (GET, POST etc.,) to your nginx server with the url /autocomplete is redirected to Elasticsearch API running on 9200.

This is your Bloodhound configuration of Typeahead.js to send your autocomplete queries to Elasticsearch directly. This was tested in typeahead js v0.11.1. There were lot of changes introduced in typeahead options recently. If you are using the latest version, check the documentation.

Now, let’s check the app:

Faster autocomplete (< 20 ms)

As you can see, the time each autocomplete call took was averaged at 20 ms. Awesome! Now, make your autocomplete faster!

Open Cricket – Search for any cricket match

Ask about any cricket matches:

Won by a team in a series: matches won by india in world cup in 2015
Lost by a team batting 2nd: matches lost by india against australia batting 2nd in odi
Won by a team chasing a target: matches won by south africa chasing 400 in odi
Played by a team in a year: matches played by australia in test in this year
Played by a team scoring a particular score: matches played by sri lanka scoring 600+ in test
Won by a team against a particular team: matches won by india against pakistan in world cup
Won by a team with specific wickets left: matches won by india with 10 wickets left in odi

Try it out, and post your feedback in the comments!