Top 5 Android libraries every Android developer should know about

In the last years, Android development has really become most used mobile OS. Android Studio with Gradle at its core is a dash of light after Eclipse. There are some open source libraries that we use on android development.

Here is a selection of five of our favorite ones and a list of links where you can find others.
Following our favorite libraries and links where you can find others.

GSON

Gson is a Java library used for serializing and deserializing Java objects from and into JSON. A task you will frequently need to do if you communicate with APIs. We mostly use JSON because it’s lightweight and much simpler than XML.

// Serialize 
String userJSON = new Gson().toJson(user);

// Deserialize
User user = new Gson().fromJson(userJSON, User.class);

VOLLEY

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub.

dependencies {
...
compile 'com.android.volley:volley:1.1.1'
}
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "http://www.google.com"

// Request a string response from the provided URL.
val stringRequest = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
// Display the first 500 characters of the response string.
//Response
},
Response.ErrorListener { textView.text = "That didn't work!" })

// Add the request to the RequestQueue.
queue.add(stringRequest)

One more recommended library you can use for network call:

RETROFIT:

Retrofit is a network library for Android . The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp.

This library makes downloading JSON or XML data from a web API fairly straightforward. Once the data is downloaded then it is parsed into a Plain Old Java Object (POJO) which must be defined for each “resource” in the response.

Make sure to require Internet permissions in your AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

Add the following to your app/build.gradle file:

dependencies {
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
}

Note: if you are upgrading from Retrofit 2 beta 1 or beta2, your package imports will need to be changed from import retrofit.XXXX to import retrofit2.XXXX. You will also need to update your OkHttp imports from import okhttp.XXXX to import okhttp3.XXXX because of the new OkHttp3 dependency:

dependencies {
implementation 'io.reactivex:rxjava:1.1.6'
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
}
// Trailing slash is needed
public static final String BASE_URL = "http://www.abc.com/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

EVENTBUS

EventBus is a library that simplifies communication between different parts of your application. For example, sending something from an Activity to a running Service, or easy interaction between fragments. Here is an example we use if the Internet connection is lost, showing how to notify an activity:

public class NetworkStateReceiver extends BroadcastReceiver {

// post event if there is no Internet connection
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getExtras()!=null) {
NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
// there is Internet connection
} else if(intent
.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
// no Internet connection, send network state changed
EventBus.getDefault().post(new NetworkStateChanged(false));
}
}

// event
public class NetworkStateChanged {

private mIsInternetConnected;

public NetworkStateChanged(boolean isInternetConnected) {
this.mIsInternetConnected = isInternetConnected;
}

public boolean isInternetConnected() {
return this.mIsInternetConnected;
}
}

public class HomeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EventBus.getDefault().register(this); // register EventBus
}

@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this); // unregister EventBus
}

// method that will be called when someone posts an event NetworkStateChanged
public void onEventMainThread(NetworkStateChanged event) {
if (!event.isInternetConnected()) {
Toast.makeText(this, "No Internet connection!", Toast.LENGTH_SHORT).show();
}
}

}

UNIVERSAL IMAGE LOADER

UIL is a library which provides asynchronous, out of the box loading and caching of images. It’s pretty straightforward to use:

imageLoader.displayImage(imageUri, imageView);

Although Picasso has a nicer API, it lacks in customization. With the UIL configuration builder almost everything can be configured (important for fetching and caching of really large images, which Picasso fails to do).

Good open source libraries will make your development a hell of a lot easier and faster. Popular libraries are often well tested and simple to use. In most cases you can easily import them into your Android Studio project from Maven. Add them into dependencies in your build.gradle file like this:

dependencies {
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.okhttp:okhttp:1.3.0'
compile 'com.squareup.retrofit:retrofit:1.3.0'
compile 'de.greenrobot:eventbus:2.2.+'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.1'
}

REALM

Realm is a mobile database that runs directly inside phones, tablets or wearables.

Features:

  • Mobile-first: Realm is the first database built from the ground up to run directly inside phones, tablets, and wearables.
  • Simple: Data is directly exposed as objects and queryable by code, removing the need for ORM’s riddled with performance & maintenance issues. Plus, we’ve worked hard to keep our API down to very few classes: most of our users pick it up intuitively, getting simple apps up & running in minutes.
  • Modern: Realm supports easy thread-safety, relationships & encryption.
  • Fast: Realm is faster than even raw SQLite on common operations while maintaining an extremely rich feature set.
buildscript {
repositories {
jcenter()
google()
maven {
url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
}
}
dependencies {
classpath "io.realm:realm-gradle-plugin:<version>-SNAPSHOT"
}
}

allprojects {
repositories {
jcenter()
google()
maven {
url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
}
}
}
//Create realm instance
Realm realm = Realm.getDefaultInstance();

// All writes are wrapped in a transaction
// to facilitate safe multi threading
realm.beginTransaction();

// Add a person
Person person = realm.createObject(Person.class);
person.setName("James John");
person.setAge(14);

realm.commitTransaction();

RealmResults<User> result = realm.where(User.class)
.greaterThan("age", 10) // implicit AND
.beginGroup()
.equalTo("name", "James")
.or()
.contains("name", "Jo")
.endGroup()
.findAll();

I hope this article will help you more in Android development. Feel free to leave comments below. Thank you.