In this blog post, I will share 7 Android libraries that every developer should use to enhance the application from different perspectives.
When developing in Android, usually you have to deal with several requirements, such as doing RESTful API calls, data serialization, loading and processing images as well as development related matters like project structure, optimizations, logging and more.
Dealing with such problems usually requires a huge amount of time if you are willing to write your own implementations manually.
But why reinvent the wheel?
There are already tens of great free open source Android libraries, in which the open source community people already spent tons of hours developing and contributing to these projects to come up with robust, useful and almost bug free Android libraries to be helpful for all the developers worldwide.
And they spend even more time supporting and solving the raised issues.
You will definitely notice a dramatic improvement in your code quality, application performance and development productivity after you learn and start using the libraries I am mentioning in this article.
So here are the 7 Android Libraries unveiled.
The super lightweight library to manipulate JSON Objects inside Java. It provides super easy methods to serialize and deserialize Java objects to and from JSON.
Gson plays well with Java Generics and can be used with both Java models from your application module, or 3rd party libraries.
Gson mainly provides 2 methods fromJson()
and toJson()
. The former converts a Json string
to a Java model as provided in the method’s second parameter type. The latter does the inverse, it converts the given Java model to a Json string
.
To start using Gson in your Android code, you only need to include it into your build.gradle file as below:
dependencies{
compile 'com.google.code.gson:gson:2.8.2'
}
To learn more about Gson library and its features, with sample code and API guide, press here.
This is best library to implement the Dependency Injection (DI) pattern into your application.
There are already other Android libraries that can help you achieve the DI pattern like Robo Guice (deprecated as of August 2016), Butter Knife or Android annotations. Each of these has its pros and cons.
And in my opinion, I see that Google’s Dagger version 2 provides the best option when it comes to implementing a robust and well-structured dependency injection pattern.
Previously, there have been two versions of Dagger library from both Square Inc. and Google.
Square’s version of Dagger 1.x is now deprecated and no longer maintained.
Google has built on top of Dagger 1 and is now maintaining Dagger 2, which brings a great development and performance improvement on the framework, with a compile-time validation of the object graph, full trace-ability, easy debugging of provision and creation and more improvements.
To use Dagger 2 in gradle:
dependencies {
compile 'com.google.dagger:dagger:2.x'
annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
compile 'com.google.dagger:dagger-android-support:2.x'
}
This only reveals the tip of the Dagger 2 iceberg - there is a lot to learn about this great library. You can start from here.
The most convenient way to do http(s)-based Restful API calls. Retrofit is a type-safe Http Client that is built on top of OkHttp
library.
It provides an easy and simple way to introduce your API endpoints using attributes that should be defined inside a Java Interface, and then you instantiate the Retrofit instance with its builder method, after that, you implement the method call, and attach a callback object, which will override two standard methods: onResponse
and onFailure
.
Retrofit gives you the ability to modify the header objects of the request, add interceptor for the request chain, implement custom authenticator and custom model converter factory.
To start using Retrofit, first you need to include library reference in your build.gradle file:
dependencies {
compile 'com.squareup.retrofit2:retrofit:2.3.0'
}
Check Retrofit website to continue learning.
Android Asynchronous Networking and Image Loading, Ion provides you a fluent API code to do a set of asynchronous tasks related to API calls, downloading files, loading images into ImageView
.
All ion operations return Future Object and can be gracefully cancelled.
In fact, Ion
combines the features of multiple Android libraries into one. It is a great way to achieve the tasks of doing asynchronous API calls, loading images, downloading files, cancelling requests and many more.
To use Ion
library, add a reference to it as shown below:
dependencies {
compile 'com.koushikdutta.ion:ion:2.+'
}
If you are interested in learning more about Ion
Library, check its GitHub Repository.
A product of fabric.io
which was acquired by Google since January, 2017.
Crashlytics is the ultimate crash reporting and monitoring tool.
The administration web portal provides comprehensive information about crashes and users. It provides a real-time view of crashes, with a full deobfuscated stack trace of the crash, the device type, OS version. It also gives geographical information about the user who had the crash.
To integrate Crashlytics inside your app, there is an Android studio plugin for fabric that can be installed.
To install the Fabric plugin, go to File -> Settings -> Plugins, and then in the search bar, type ‘Fabric’. You will get the option ‘Fabric for Android Studio’, then choose ‘install’ and follow the instructions afterwards.
Once done, you will start seeing a Fabric tab view in your Android studio, from there, press ‘sign up’ to create a new fabric account, it will take you to fabric.io
account creation page, from there, input the account details you want, then finish the setup.
Once you confirm your account, enter your credentials into Android studio fabric view, and then from the products list, choose Crashlytics.
Follow the on-screen instructions to finish integrating your Android app with Crashlytics.
For more details, installation instructions, and code examples, check fabric.io website.
Instead of loading an image directly into an ImageView
, why not do it asynchronously?
Picasso gives you the option to download an image from a url and then display it into the ImageView
without you having to block the UI while waiting for the image to get downloaded, the ImageView
itself will display a small loading spinner, and once the image gets ready, it displays it seamlessly.
Picasso also features adapter downloads, image simple and custom transformations, download and error placeholders and more.
To use Picasso library, just include it in your gradle’s dependencies section:
dependencies {
compile 'com.squareup.picasso:picasso:2.5.2'
}
To learn more about Picasso, check out this link.
A unit testing framework that enables running the unit tests inside JVM instead of running them on emulator or real device.
Robolectric tests can handle many native operations such as view inflation and resource loading, that you cannot achieve with normal JUnit tests, so this gives you a near experience of testing on a real device or an emulator.
To start writing Robolectric unit tests, you first need to include the Robolectric library into your gradle file.
If you are using Android Studio 3 Alpha 5 version or higher, you will need to include the resources merging as part of your gradle test options alongside with referencing the library itself.
testImplementation "org.robolectric:robolectric:3.6.1"
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
Annotate your test with the Robolectric test runner:
@RunWith(RobolectricTestRunner.class)
public class AccountActivityTest {
}
See Robolectric.org for a complete guide to learn more about this industry-standard unit testing framework.
Conclusion
You need always to focus on delivering quality Android apps in a timely manner. The digital age is already fast forwarding and everyday, there are hundreds of apps being pushed to Google Play store.
The challenge is growing and the urge to have fast paced iterations of app development is significantly rising.
To be able to catch up with the digital transformations’ high-speed train, you need to be a proactive developer, which means you should always be on the lookout for the best Android libraries and tools that will improve your productivity and enhance the app development experience.
Make sure to choose your tools, SDKs and libraries wisely.
I recommend that you also read my article about 9 Bulletproof ways to become a better developer.
This is a comprehensive article about all the things you can do to improve your technical skills and become a better and proactive developer.
I have included seven of the most essential and useful Android libraries for developers to learn and use.
I might be missing some more important libraries, please share it in a comment so all of us can learn from each other.
Bonus
For this important topic, I would like to share this captivating music which is
“Water Music – Suite number 2 in D major. Movement 12” by George Frideric Handel
Enjoy it!
CodeProject