• Top 5 Android Libraries Every Android Developer should know about – V. 2015
  • The Nuggets translation Project
  • Translator: Kassadin
  • Reviewer: Xiuweikang LiHB

In June 2014, we published an article about five of the top Android open source libraries that we use all the time and believe every Android developer should know about. A lot has changed on Android since then, so we wrote this post, an updated version of our five favorite open source libraries.

Here is the list of updates:

1. Retrofit

Retrofit is still our favorite when it comes to implementing REST APIs.

Their website says, “Retrofit transforms REST apis into Java interfaces.” Yes, there are other solutions, but Retrofit has proven to be the most elegant and convenient solution for managing API calls in a project. Using annotations to add request methods and relative addresses keeps the code clean and simple.

Annotations allow you to easily add request bodies, manipulate urls or request headers, and add query parameters.

Adding a return type to a method causes the method to execute synchronously, whereas adding a Callback causes it to execute asynchronously, calling back the success or failure methods when complete.

public interface RetrofitInterface {

    // async callback
    @GET("/api/user")
    User getUser(@Query("user_id") int userId, Callback<User> callback);

    / / synchronize
    @POST("/api/user/register")
    User registerUser(@Body User user);
}


/ / case
RetrofitInterface retrofitInterface = new RestAdapter.Builder()
            .setEndpoint(API.API_URL).build().create(RetrofitInterface.class);

// Get user id 2048
retrofitInterface.getUser(2048.new Callback<User>() {
    @Override
    public void success(User user, Response response) {

    }

    @Override
    public void failure(RetrofitError retrofitError) {

    }
});Copy the code

Retrofit uses Gson by default, so there is no need to parse JSON manually. Other converters are also supported.

Retrofit 2.0 is currently under active development, still in beta, but you can get it here. Since Retrofit 1.9, a lot of things have been cut, and there are some major changes such as using a new call interface instead of callbacks.

2. DBFlow

If you are planning to store arbitrarily complex data in your project, you should use DBFlow. As stated on their GitHub, it’s “a fast, powerful, and very simple Android database ORM library that writes database code for you.”

Some simple chestnuts:

// Query a List
new Select().from(SomeTable.class).queryList();
new Select().from(SomeTable.class).where(conditions).queryList();

// Query Single Model
new Select().from(SomeTable.class).querySingle();
new Select().from(SomeTable.class).where(conditions).querySingle();

// Query a Table List and Cursor List
new Select().from(SomeTable.class).where(conditions).queryTableList();
new Select().from(SomeTable.class).where(conditions).queryCursorList();

// SELECT methods
new Select().distinct().from(table).queryList();
new Select().all().from(table).queryList();
new Select().avg(SomeTable$Table.SALARY).from(SomeTable.class).queryList();
new Select().method(SomeTable$Table.SALARY, "MAX").from(SomeTable.class).queryList();
Copy the code

DBFlow is a good ORM, which eliminates a lot of boilerplate code for working with databases. While there are other ORM solutions available for Android, DBFlow has proven to be the best solution for us.

3. Glide

Glide is a library for loading pictures. Current options include Universal Image Loader and Picasso; But Glide is, in my opinion, the best choice right now.

Here is a simple example of how to use Glide to load an image from a URL into an ImageView.

ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
Copy the code

4. Butterknife

A library for binding Android views to properties and methods (for example, binding a view’s OnClick event to a method). Compared with the previous version, the basic functions are unchanged, but the options have been increased. Chestnut:

class ExampleActivity extends Activity {
  @Bind(R.id.title) TextView title;
  @Bind(R.id.subtitle) TextView subtitle;
  @Bind(R.id.footer) TextView footer;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(this);
    // TODO Use fields...}}Copy the code

5. Dagger 2

Since we moved to the MVP architecture, we have made extensive use of dependency injection. Dagger 2 is the successor to the well-known dependency injection library Dagger and is highly recommended.

One major improvement is that the generated injected code no longer relies on reflection, which makes debugging much easier.

Dagger creates instances of classes for you and satisfies their dependencies. This relies on the javax.inject.Inject annotation to determine which constructors or fields should be treated as dependencies. Take the famous CoffeeMaker for example:

All Dagger files have the following command:

class Thermosiphon implements Pump {
  private final Heater heater;

  @Inject
  Thermosiphon(Heater heater) {
    this.heater = heater; }... }Copy the code

Inject chestnuts directly into fields:

class CoffeeMaker {
  @Inject Heater heater;
  @InjectPump pump; . }Copy the code

Provide Dependencies via modules and @Proivides annotations:

@Module
class DripCoffeeModule {
  @Provides Heater provideHeater() {
    return new ElectricHeater();
  }

  @Provides Pump providePump(Thermosiphon pump) {
    returnpump; }}Copy the code

For more information about dependency injection itself, check out the Dagger 2 home page or talk about Dagger 2 by Gregory Kick.

Additional links

Android Weekly is still one of the best resources to learn about Android libraries. This is a weekly update on Android development.

In addition, here are some of the biggest names in the Android industry who regularly write about Android development:

Jake Wharton Chris Banes Cyril Mottier Mark Murphy Mark Allison Reto Meier