When you’re developing an Android app, you can of course build everything from scratch, or use some of the thousands available open-source libraries. It’ll save you a lot of time, and often let you use complex UI animations, components, or algorithms, which you wouldn’t be able to code from scratch (or not without a lot of time). I even know some people who’d like to use nothing but libraries to build their app.
Since I’ve used a lot of libraries for Astonishing Comic Reader, I’ve decided to write this article, and talk about some of the most useful ones.
First of all, I’d like to quickly talk about support libraries. There are a lot of them out there, and even if new releases often come with bugs (especially Appcompat, or the brand new design support library), you have to use (some of) them. Even if you don’t require an API level 4 support, the support v4 library contains very useful stuff like fragments or retro-compatible animations. Just take a look at the revisions list.
Android annotations (AA) is one of my favorite Android libraries since I’ve started to develop on this platform. It simplifies your code by generating a lot of boilerplate code, like views injection, background threads, intents building, custom views, preferences, Rest API calls… Well, it does it almost all. Just take a look at the official cookbook, you’ll see what I mean.
AA actually generates subclasses from annotations, where all the code you don’t want to write is. For example, if you want to use AA on a specific activity class called MyActivity, you’re using annotations like @EActivity (to declare that your class will use AA annotations) and @ViewById (to avoid the boring findViewById). At compilation, a class called MyActivity_ (notice the “_” symbol) is generated. This is this class that you’ll declare in your manifest, and use to build intents.
AA really makes your life easier, on a lot of things. Maybe too much things, and that’s one of its flaws. It quickly creates a very strong dependency between your code and the library, and removing it might be a pain. But if you want to replace all these features with other libraries, you can start with things like Dagger and Butterknife.
The other major flaw is that it creates a lot of new subclasses (actually, one per annotated classes). Then, it almost doubles the number of methods in your code, which might dramatically lead you to the unpopular multidex error. Keep it in mind if you want to use it.
Square is an American company specialized in mobile payments. But their talented developers have developed some of the most popular Android libraries, including Otto, Dagger, Retrofit, Picasso… I’ll talk about some of them here, but you can check the complete list here.
Otto is an event bus library. Basically, it allows you to send messages across the different components of your application. And trust me, that’s sometimes very, very useful. You can for example send objects from a fragment to another or notify an activity from a custom view. It’s not a must-have in all your projects, and you should be careful about overusing it.
In ACR, I use Otto to synchronize the zoom level between the pager’s page, or to notify the view that suggestions have been loaded. You can also use it to send events about network operations, or database data retrieving.
There is a very good alternative to Otto called EventBus. I’m not very fond of it, but it has some very convenient features, like managing events trough inheritance, which Otto doesn’t.
Retrofit is a REST client library for Android which clearly eases your work with web services.
The only things you have to do is to declare an interface, containing all your API endpoints (annotated with things like @GET or @Query), and use this interface to declare a RestAdapter and a service class, which is basically your interface implementation.
What I like with Retrofit is that it’s very, very easy to use, and it’ll clearly change your life. It allows you to simply declare your endpoints, or customize them with annotations. It automatically parses you results using Gson, but you can use a custom converter if you want (XML parser for example). Easy setup, great default behaviour, and advanced customization if needed. What else?
I’ll probably write another article about image libraries, since image processing is at the core of ACR, but I got to talk about Picasso. Yes, it’s another library by Square, designed to ease image downloading and manipulation. The goal is to let developers easily display images from the Internet, and automatically handle things like cache, network failures, placeholders etc.
I’ve seen on a Google + poll that a lot of developers don’t use any library to manage images. If you don’t, do it. Right now. Even if you’ve your own library for that. If you don’t want Picasso, you can pick something like Universal Image Loader, Glide or even Fresco. Each of these libraries have been developed, tested and improved by thousands of fellow developers. And they clearly ease your life!
JSON processing is nowadays a very easy task in most cases. You probably all know Gson, the Google library to parse JSON. There are alternatives like Jackson, or the new player in town: Moshi (also by Square).
Finding new libraries
Finding new Android libraries is easy: just use Google. But if you want to do it the right way, there are a lot of websites out there which are referencing new libraries each day. My favorite one is Android arsenal, but you can also visit AndroidLibs or directly test libraries on your mobile with Libraries for developers.