Jetpack Features In Android

Introduction

 
According to Google, "Jetpack is a collection of Android software components to make it easier for you to develop great Android apps. These components help you follow best practices, free you from writing boilerplate code, and simplify complex tasks, so you can focus on the code you care about."
 
Jetpack contains certain package libraries called androidx.* and offers backward compatibility; however, these components update more frequently than the Android platform as promised by Google.
 

Benefits of Jetpack

  • Accelerate development
  • Eliminate boilerplate code
  • Build high quality, robust applications

Jetpack Components

 
Well, there are four Jetpack components released by Google in the documentation. These components offer some new excellent features to make Kotlin more usable and robust and make the work more productive.
 
The four main components of Jetpack are listed below. 
  • Foundation
  • Architectural
  • Behaviour
  • UI Component
Jetpack Features In Android
 
We won't cover all of these features in this article. Let's take the first one, Data Binding, and let's go through the architecture component,i.e, used in the very first line in the queue. 
 

Data Binding

 
Data Binding is a library that enables the user to bind UI component without using findViewById(). It binds the data to Views in the layout using declarative format rather than programmatically. Let us see this using some code.
 
Create a simple project and create a blank MainActivity.java. See the code below for better understating.
 
activity_main.xml
  1. <layout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     xmlns:tools="http://schemas.android.com/tools">      
  3.     <!--Step 1-->    
  4.     <data>     
  5.         <!--Step 2-->    
  6.         <variable    
  7.             name="dataModel"    
  8.             type="<full package name>.DataModel" />    
  9.     </data>      
  10.     <LinearLayout    
  11.         android:layout_width="match_parent"    
  12.         android:layout_height="match_parent"    
  13.         android:orientation="vertical"    
  14.         android:padding="16dp"    
  15.           >    
  16.         <!--Step 3-->    
  17.         <TextView    
  18.             android:id="@+id/text_view_name"    
  19.             android:layout_width="wrap_content"    
  20.             android:layout_height="wrap_content"    
  21.             android:text="@{dataModel.Name}"    
  22.             android:textColor="@android:color/black"    
  23.             android:textSize="24sp" />    
  24.         <TextView    
  25.             android:id="@+id/text_view_work"    
  26.             android:layout_width="wrap_content"    
  27.             android:layout_height="wrap_content"    
  28.             android:text="@{dataModel.Work}"    
  29.             android:textColor="@android:color/black"    
  30.             android:textSize="15sp"    
  31.             android:layout_marginTop="10dp"    
  32.             android:layout_marginStart="20dp"/>    
  33.     </LinearLayout>    
  34. </layout>    
Now, create a DataModel.java pojo class which contains the name and work. For the sake of simplicity, we are taking only two fields but you can increase your fields whenever you want.
 
DataModel.java 
  1. public class DataModel {  
  2.     private String Name;  
  3.     private String Work;  
  4.     public DataModel(String name, String work) {  
  5.         this.Name = name;  
  6.         this.Work = work;  
  7.     }  
  8.     public String getName() {  
  9.         return Name;  
  10.     }  
  11.     public void setName(String name) {  
  12.         Name = name;  
  13.     }  
  14.     public String getWork() {  
  15.         return Work;  
  16.     }  
  17.     public void setWork(String work) {  
  18.         Work = work;  
  19.     }  
  20. }  
Now, see MainActivity.java.
  1. import android.databinding.DataBindingUtil;    
  2. import android.os.Bundle;    
  3. import android.support.v7.app.AppCompatActivity;    
  4.     
  5. public class MainActivity extends AppCompatActivity {  
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         //        Before Data Binding    
  10.         /*  setContentView(R.layout.activity_main);  
  11.   
  12.         TextView textViewName = (TextView) findViewById(R.id.text_view_name);  
  13.         TextView textViewWork = (TextView) findViewById(R.id.text_view_work);  
  14.   
  15.         textViewName.setText("Gaurav Kumar");  
  16.         textViewWork.setText("Android app developer");  */  
  17.         //        After Data Binding    
  18.         ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);  
  19.         DataModel dataModel = new DataModel("Gaurav kumar""Android app developer");  
  20.         binding.setDataModel(dataModel);  
  21.     }  
  22. }  
Clearly, we can see that here the commented code is before data binding, and after data binding you can see the difference. Don't forget to import "import <yourpackage>databinding.ActivityMainBinding " and most importantly, enable binding in gradle -- see code below. 
  1. apply plugin: 'com.android.application'  
  2. android {  
  3.     compileSdkVersion 28  
  4.     defaultConfig {  
  5.         applicationId "yourdomain.databindingproject"  
  6.         minSdkVersion 21  
  7.         targetSdkVersion 28  
  8.         versionCode 1  
  9.         versionName "1.0"  
  10.         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  
  11.     }  
  12.     buildTypes {  
  13.         release {  
  14.             minifyEnabled false  
  15.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
  16.         }  
  17.     }  
  18.     dataBinding {  
  19.         enabled = true  
  20.     }  
  21. }  
  22. dependencies {  
  23.     implementation fileTree(dir: 'libs', include: ['*.jar'])  
  24.     implementation 'com.android.support:appcompat-v7:28.0.0'  
  25.     implementation 'com.android.support.constraint:constraint-layout:1.1.3'  
  26.     testImplementation 'junit:junit:4.12'  
  27.     androidTestImplementation 'com.android.support.test:runner:1.0.2'  
  28.     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'  
  29. }  
Output
 
Jetpack Features In Android
 
Now see here, without programmatically setting the data to the text view, you can bind the data using this method. 
 

Conclusion

 
We can conclude that Jetpack is the most versatile collection of libraries. With the example of data binding library, we saw how powerful Jetpack is. The beauty of the data binding library is that it eliminates the use of various lines of View initialization, like findViewById() etc. Now, data binding takes care of it. Stay tuned for other features. We will be here with more library descriptions and other Jetpack features.


Similar Articles