Burak Dede's Blog

Android Support Library Confusion

March 28, 2017

Lets start with offical definition of the support library

When developing apps that support multiple API versions, you may want a standard way to provide newer features on earlier versions of Android or gracefully fall back to equivalent functionality. Rather than building code to handle earlier versions of the platform, you can leverage these libraries to provide that compatibility layer. In addition, the Support Libraries provide additional convenience classes and features not available in the standard Framework API for easier development and support across more devices.

What are the main benefits of using support libraries?

  • Backward Compatibility for newer API
  • Convenience Helper Classes
  • Debugging, Testing, and Utilities

Support library topic may be one of the most confusing parts of the Android development and whole SDK. I remember after Fragments introduced with API Level 11 (which if I remember correctly Honeycomb) they introduced a `support library to provide backward functionality for platforms earlier than Honeycomb. It was just a single library and you add your dependency to get Fragments functionality. As new versions launched this support library than grew into a massive library of code.

When feature does not exist on older version you have couple of options

  • Write your own support, really?
  • Runtime build check & prevention (that is just sweeping under carpet)
  • Offical support library

Offical support library is the safest option of all. The advantages would be there are people working on this full time so future updates, thoroughly tested so less buggy than your custom support library and come with helper classes as an extra.

Yes, support library thing seems confusing because it really is…

After support library got really big they divide it into a couple of versioned libraries.

  • support-v4
  • appcompat-v7
  • recyclerview-v7…..

v# scheme means that it will support all the way back to 4 or 7 API Level. But that would not be the case for all functionality. Offical doc

These are the most known ones but there are more. Check revision page for a list of changes and other ones.

When people say support library they are meaning support-v4 as the support library.

dependency support library

This is getting confusing especially using Fragments inside application. Normally you extend your activity from Activity class, if you support Fragments you would use FragmentActivity (this is not necessary if you are using native fragments instead of support one). This is all great, support-v4 library already have android.support.v4.app.Fragment and android.support.v4.app.FragmentActivity classes but the latest version of the Android Studio include appcompat-v7 as support library instead of support-v4 and most of the activity classes extend from AppCompatAcitivty.

support diagram

Some support libraries also include classes from other support libraries or directly depend on them. AppCompatAcitivity which comes with appcompat-v7 and extend from FragmentActivity from support-v4 so you get both of them if you just use AppCompatActivity. Above image explains it all better.

If you are going to support Fragments without AppCompat library you would need extra support-v4 library but if not appcompat-v7 already had it.

Android Debugging - Java Exception Breakpoints

March 27, 2017

Lets face it debugging is part of the process while developing mobile applications whether it is an Android app or not. It is really frustrating chasing for hours to find simple increment bug or non-called function after your application reach to this will become more frequent.

I will share you a feature that I have been using for a while which makes change-debug-test part much more effective in my opinion. Here is the usual flow

  • Add new feature
  • Build and sign app to test on device
  • App blows up and now look at what exception or error is
  • Nothing comes out, let’s attach debugger to see what’s going on
  • Wait until to come to that screen or feature

This is the general flow when you face with runtime exception they will be mostly NullPointerException but sometimes those are little trickier. Android Studio have a feature that allows you to add new generic breakpoint to catch Exception based on the filter.

Run -> View Breakpoints -> Add(+) -> Java Exception Breakpoint -> RuntimeException

android debugging exceptions

By doing above steps you are basically saying that hey create new breakpoint as soon as you catch any RuntimeException. After applying that next time your app throw RuntimeException it will be directly created breakpoint and go to that line with a debugger attached and you can inspect all the variables on that spot.

This is, of course, tricky because it will also catch/stop any exception thrown by runtime/framework libraries included but not directly related your code. Beware of that.

Book - The Power of Habit - Notes

March 23, 2017

The Power of Habit is the new book I am currently reading and for first few chapters it better than I expected. I value books based on how fast I can apply the knowledge to my life. Since this book is about daily habits, how the brain works and how we form new habits it is applicability is higher than other books.

My GoodReads Account if you wanted to follow what other books I am reading.

habit loop

Chapter 1 - The Habit Loop

  • Old habits die hard and literally they never die.
  • Old habits stay where they are inside the brain.
  • New habits form new patterns but old habits still ready to be activated.
  • When you have a habit of eating fast food at night you can change this habit with the new one but habit loop still triggers old habit inside the brain even though you stop eating fast food and replaced it drinking water instead.
  • Habits have cycle cue - routine - reward.
  • Most of our bad habits can work like domino if you can find the keystone habit and change it other will follow. The book includes a case of a woman which changes life somehow by changing keystone habit.
  • Your brain does not like your mundane daily life so it delegates or converts most of the tasks to habits and stay low profile.

brain activity

  • Delegation is not applicable when you are learning or doing something the first time.
  • Even though brain stop learning new things it can still form new habits.
  • Brain delegates its task to habit when habit started and take control back when habit end with the reward.
  • We can focus on other things while we are in habit cycle because the brain is not actively working in the period. eg. Driving the car while you remember that you forgot something
  • Habit loops are fragile especially cue part. When the brain senses a little change habit loop breaks.
  • When scientists changed reward place rats had a hard time figuring out what to do. When they use cat sound instead of a click sound on cue phase could not figure out what to do.

Chapter 2 - The Craving Brain

craving habit loop

  • Pepsodent toothpaste become so popular that it changed daily toothbrushing habits of Americans.
  • Hopkins (founder of the Pepsodent campaign) created simple cue-reward from daily life and turn it into habit
  • The real science behind the toothbrushing habit was habit created craving.
  • Hopkins rules become defacto standard on marketing (even though most of them already tried before Hopkins)
  • Febreze case is wildly interesting. It is hard to find the right cue-reward mechanism.
  • Febreze first positioned itself as a product to get rid of the bad smell (literally removing it). This is failed at first
  • Febreze repositioned itself as an after cleaning product which created another cue-reward with craving good smell after cleaning session.
  • Craving phase happen after you got the reward and sometimes it is so compelling that it emerges on cue phase too (experiment on monkeys with grape juice shows that monkeys create spikes in brain activity even on the cue phase after multiple tries).
  • Some products have pseudo ingredients inside them to trigger craving and habit loop. Toothpaste contain sulfate even though it doesn’t have to. It creates foam effect which people think that it works when foam appears along with herbal sense.
  • To learn new habits spark craving sense. If you want to run or exercise put your jogging shoes somewhere visible to remind that.

Catching Up with Android Development

March 21, 2017

It has been a long time since I develop anything for mobile screens. My native mobile development story started around 2009 with Android 1.5 and since then I move onto different platforms including iOS to Blackberry (yes blackberry). I stop developing for mobile phones around when Android 4.x came out. I had my personal reasons to not continue developing but some of the high-level ones are

  • inconsistencies around device types and APIs
  • backward compatibility mess
  • lack of stable development tools

I stopped actively developing anything but still followed most of the news and changes. Since I have time lately I decide to give it a shot to see if anything above changed or at least is it still fun to do mobile development like when I started. This is post is all about catching up on the Android side.

IDE, SDK, Emulator, Phone

These four prerequisites are still required in order to start developing but it is much more fluid right now. Android Studio come prepackaged with Android SDK (I think it is the latest one) and you don’t have to deal with setting up SDK or connecting it with IDE, adding plugin etc.

Apply Changes

I must say emulator is much faster and usable and with the new Apply Changes option change-test cycle is really easy.

Since there is not much defacto book on Android and it is really hard to keep content of the book updated with ever changing Android SDK best way to consult is Android developer page (https://developer.android.com/training/index.html)

[Update] I read Big Nerd Ranch book Android Programming before and from release date it looks like it is up to date with latest Android version so if you are interested in books you can check that one.

I have minor issiues with phone and Android Studio bridge but fixed it with downloading driver from manufacturer site, still much better than editing files with manufacturer codes.

ViewGroup, View, XML

As you may expect Android development still going around xml viewgroups/views and java code but I must say Android Studio is making this so much easier. We still have ViewGroups (layouts) and Views (widgets) and XML for designing UIs. ConstraintLayout is new for me but it looks like RelativeLayout but much more powerful and have lots of option to place your views according to parent or sibling views.

ConstraintLayout is new ViewGroup and designed for adaptive UIs and tries to make ViewGroup tree more flat so that rendering will be quick. It looks a lot like RelativeLayout but much more adaptive one depending on the constraints and other elements.

Rendering times for the changes on design view is much faster even though multiple changes takes places still you can see the results in a fairly efficient way. Project structure starts to get messy when you app reach to significant size but new project structure makes it really easy to navigate with IntelliJ great search shortcuts.

new project structure

And finally when you add new Activity class it will be added to AndroidManifest automatically. This probably will save hundred or thousands because of why is this chrashing and then you realize you forgot to add new activity to manifest bugs are now gone.

Around 2010-2011 there are good books about Android but as we reach to 2014 most of them become outdated. Even though there are not good quality books out there I must say Android docs doing a really great job.


powered by TinyLetter