Popular Android Libraries

Popular Android Libraries

Libraries are major game changers in software development irrespective of platform or stack. With libraries, we leverage the efforts of other developers to perform actions/functions faster, more effective, and with lesser boilerplate codes. In this article, we will look at various categories in Android development and the common libraries used in them.

1. Android Libraries—Image Loading

Image loading libraries come in very handy to avoid high memory consumption caused by loading multiple images at the same time. Images are the greatest source of Out of Memory errors in Android development. These libraries, therefore, reduce the hassle of loading and caching images together with minimizing memory usage to provide a seamless user experience. Let’s take a look at two of the commonly used image loading libraries: Glide and Picasso.

Glide

Glide is an image loading library focused on smooth scrolling. Glide ensures image loading is both as fast and as smooth as possible by applying smart automatic down-sampling and caching to minimize storage overhead and decode times. It also re-uses resources like byte arrays and automatically releases application resources where necessary. At the time of writing, Glide’s latest version requires a minimum SDK of API 14 (Android 4.0) and requires a compile SDK of API 26 (Android 8.0) or later.

We first need to make sure we have the Maven and Google repositories in our project build.gradle file:

repositories {
  mavenCentral()
  google()
}

Then, we add the library dependency in our app-module build.gradle file and sync it to make the library available for use:

implementation 'com.github.bumptech.glide:glide:4.4.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'

We can then load an image from a remote URL with a single line of code:

GlideApp
  .with(this)
  .load("https://res.cloudinary.com/demo/video/upload/dog.png")
  .into(imageView);

The with method can take a ContextActivityFragment or View object. The load method takes a remote URL or a drawable file (e.g R.drawable.image). The imageView instance, passed as an argument to the into method, should be of type ImageView.

2. Android Libraries—Videos

Displaying videos poses to be another difficult task during development. The process and details to take care of can be too numerous to handle. In this category, there are a few available options. However, as the most popular and powerful one is ExoPlayer, we will focus this section on it.

ExoPlayer

ExoPlayer is an Android media player library developed by Google. It provides an alternative to Android’s MediaPlayer API for playing audio and video (both locally and over the internet) with some added advantages. ExoPlayer supports features not currently supported by Android’s MediaPlayer API, like DASH and SmoothStreaming adaptive playbacks. One of ExoPlayer’s biggest advantage is its easy customization.

Using ExoPlayer

The first step is to make sure we have the JCenter and Google repositories included in the project build.gradle configuration file:

repositories {
    jcenter()
    google()
}

Next, we need to add a Gradle compile dependency to the same file:

implementation 'com.google.android.exoplayer:exoplayer:2.6.0'

Then, in our layout resource file, we add the SimpleExoPlayerView component:

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
   android:id="@+id/simple_exoplayer_view"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"/>

After that, in the corresponding Activity class, we instantiate ExoPlayer’s classes:

SimpleExoPlayerView simpleExoPlayerView;
SimpleExoPlayer player;

We then initialize our simpleExoPlayerView in the onCreate method of our Activity:

simpleExoPlayerView = findViewById(R.id.simple_exoplayer_view);

And, in the onStart method, we call the setupPlayer method:

@Override
protected void onStart() {
    super.onStart();
    setupPlayer();
}

And the setupPlayer method:

void setupPlayer(){
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory videoTrackSelectionFactory =
            new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector =
            new DefaultTrackSelector(videoTrackSelectionFactory);

    //initialize the player with default configurations
    player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

    //Assign simpleExoPlayerView
    simpleExoPlayerView.setPlayer(player);

    // Produces DataSource instances through which media data is loaded.
    DataSource.Factory dataSourceFactory =
            new DefaultDataSourceFactory(this, Util.getUserAgent(this, "CloudinaryExoplayer"));

    // Produces Extractor instances for parsing the media data.
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

    // This is the MediaSource representing the media to be played.
    MediaSource videoSource = new ExtractorMediaSource(videoUri,
            dataSourceFactory, extractorsFactory, null, null);

    // Prepare the player with the source.
    player.prepare(videoSource);
}

Here, we initialized the player with some default configurations and then assigned it to the SimpleExoPlayerView instance. The videoUri is of type Uri. Every file stored our device has a Uri turning it into a unique address to that file. If we intend to display a video from a remote URL, we have to parse it this way:

Uri videoUri = Uri.parse("any_remote_url");