Picasso — How to Use & Migrate to Picasso's Upcoming 3.x Version

In the last two years we've written 24 Picasso tutorials, which describe every little feature of Picasso. We would love to write more, but unfortunately there has been no new release of Picasso in over two years. The latest version at the time of writing is still 2.5.2.

However, development has not been standing still. Picasso is not an abandoned project. There have been hundreds of commits since the last release. Those new commits bring in some goodies, like OkHttp 3 as the default network stack, a parameterless .with() call (instead of the annoying .with(context)), and some under-the-hood improvements.

In this tutorial, you'll learn how to add the current snapshot of Picasso 3.0.0 to your project. You'll also learn how to migrate from a current 2.5.2 version.

Picasso Series Overview

Gradle Dependencies

At first, you'll need to add two new Gradle dependencies to your project:

// Picasso 3.0.0 snapshot
compile 'com.squareup.picasso:picasso:3.0.0-20170712.175103-16'

// theoretically, this should work too, but seems to be broken right now:
compile 'com.squareup.picasso:picasso:3.0.0-SNAPSHOT'  

Sync your project and you can move to the next step.

Use Picasso 3.0.0

Once you synced your Android project, you can use Picasso. As most changes are under the hood, the general usage of Picasso stayed the same:

String internetUrl = "http://i.imgur.com/DvpvklR.png";  
Picasso  
    .with()
    .load(internetUrl)
    .into(imageViewInternet);

Migrate from Picasso 2.5.2 to Picasso 3.0.0

Because Picasso's fluent interface stayed the same, the migration guide (as of now) is fairly short.

.with Without Context

However, one significant change is that you don't have to pass the context to the .with() call anymore. That means you can replace all the .with(context) in your app with .with().

// Picasso 2.5.2
String internetUrl = "http://i.imgur.com/DvpvklR.png";  
Picasso  
    .with(MainActivity.this) // or whatever your context is
    .load(internetUrl)
    .into(imageViewInternet);

// Picasso 3.0
String internetUrl = "http://i.imgur.com/DvpvklR.png";  
Picasso  
    .with()
    .load(internetUrl)
    .into(imageViewInternet);

Remove OkHttp 3 Hacks

If you've implemented a custom downloader to use Picasso 2.5.2 with OkHttp 3 as the network stack, you can remove that from your code base. It's not necessary anymore!

Callback Interface Changes

Picasso's callback interfaces now return the exception as a parameter, if something went wrong. Thus, you'll have to adjust your callback implementation

String internetUrl = "http://i.imgur.com/DvpvklR.png";

// Picasso 2.5.2
Picasso  
    .with()
    .load(internetUrl)
    .fetch(new Callback() {
        @Override
        public void onSuccess() {
             // yeah
        }

        @Override
        public void onError() {
            // no :(
        }
    });

// Picasso 3.0
Picasso  
    .with()
    .load(internetUrl)
    .fetch(new Callback() {
        @Override
        public void onSuccess() {
            // yeah
        }

        @Override
        public void onError(Exception e) {
            // no :( - but now with an exception \o/
        }
    });

Target Changes

If you're using targets to load images to your callback instead of an ImageView, you'll also have to change your override methods. The onBitmapFailed() callback now also carries the Exception e as a parameter:

// Picasso 2.5.2
private Target target = new Target() {  
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {}

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        // TODO do some action/warning/error message
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};

// Picasso 3.0
private Target target = new Target() {  
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {}

    @Override
    public void onBitmapFailed(Exception e, Drawable errorDrawable) {
        // TODO do some action/warning/error message, now with exception \o/
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};

Summary

In this tutorial you've learned how to add Picasso 3.0.0 to your project. You've also seen how to migrate to the new snapshot from Picasso 2.5.2. Finally, you've seen a glimpse into the first calls with Picasso 3.0.0.

Do you have further questions on this topic or about Picasso in general? Let us know on Twitter @futurestud_io or leave a comment below.

Enjoy coding & make it rock!

Explore the Library

Find interesting tutorials and solutions for your problems.