Open In App
Related Articles

Implementation of Facebook’s Android Device Year Class Library in Android

Like Article
Save Article
Report issue

As developers, we always aim to create an application that would run smoothly on millions of devices, however, the performance of practically every application is influenced by the device specifications. There are currently over 16,000 handsets on the market with varying specifications, and the application may perform poorly on some devices when compared to others. Several functions require a fast processor to operate smoothly and may lag on a slow processor. To address this problem, Facebook developed the Device Year Class Library, which uses a simple algorithm to match a device’s RAM, CPU cores, and clock speed to the year in which those characteristics were considered high-end. This enables developers to easily write different logics for the application’s behavior based on the phone’s hardware capabilities. For classification, the Year Class library takes three factors into account:

  • Number of CPU Cores in the device
  • The Clock speed of a CPU core on the device
  • Total RAM of the device

Based on the device hardware, we can optimize a variety of operations, including:

  • Determine whether to display animations or not, as animations may lag on low-end devices.
  • Select whether to load low or high-quality content for faster loading.
  • Suggest a lite app version for devices with low specs.

We’ll now look at how to use this library in an application. We’ll build an application that detects whether or not to display animations based on the device’s year class.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add the library dependency 

Navigate to the Gradle Scripts > build.gradle(Module:app), add the library in the dependencies section, and sync the project.

implementation 'com.facebook.device.yearclass:yearclass:2.1.0'

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />


Step 4: Working with the animation.xml file

  • Navigate to app > res
  • Right-click on res and select New > Directory
  • Type anim as the directory name
  • Right-click on anim and select New > Animation Resource File
  • Type animation as the File Name and press OK.

Below is the code for the animation.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!-- animation to scale out the view -->


Step 5: Working with the file

Go to the file and refer to the following code. Below is the code for the file. 


import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import com.facebook.device.yearclass.YearClass;
public class MainActivity extends AppCompatActivity {
    TextView textView;
    protected void onCreate(Bundle savedInstanceState) {
        textView = findViewById(;
        // getting the device year class
        int year = YearClass.get(getApplicationContext());
        if (year > 2013) {
            // if device year class is greater than 2013 --> load animation
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation);
        } else {
            // load no animation if device year 
              // class is less than 2013



Last Updated : 23 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads