Open In App

Foundation Components of Android Jetpack

Android Jetpack is a set of software components, libraries, tools, and guidance to help in developing robust Android applications. Launched by Google in 2018, Jetpack comprises existing android support libraries, android architecture components with an addition of the Android KTX library as a single modular entity. Nowadays, nearly 99% of the apps present on the Google Play Store uses Android Jetpack libraries. The core system components of android apps reside in the Foundation area of Jetpack. Kotlin extension for the language support and Testing libraries are also present in it. Moreover, backward compatibility is provided from the libraries present in this component. This article explains each and every library of the Foundation component in detail. Jetpack consist of a wide collection of libraries that are built in a way to work together and make robust mobile applications. Its software components have been divided into 4 categories:

  1. Foundation Components
  2. Architecture Components
  3. Behavior Components
  4. UI Components

Further, the following are the list of all Foundation components:



  1. AppCompat
  2. Android KTX
  3. Test
  4. Multidex

Ways to include Android Jetpack libraries in the application

Add google repository in the build.gradle file of the application project.

allprojects {



  repositories {

      google()

      mavenCentral()

      }

}

All Jetpack components are available in the Google Maven repository, include them in the build.gradle file

allprojects {

   repositories {

       google()

       mavenCentral()

   }

}

Foundation Components

1. AppCompat

The AppCompat library in Jetpack foundation includes all of the components from the v7 library. This includes AppCompat, Cardview, GridLayout, MediaRouter, Palette, RecyclerView, Renderscript, Preferences, Leanback, Vector Drawable, Design, Custom tabs, etc. Moreover, this library provides implementation support to the material design user interface which makes AppCompat very useful for the developers. Following are some key areas of an android application that are difficult to build but can be designed effortlessly using the AppCompat library:

2. Android KTX

This library is the only one among the foundation components which was introduced for the first time with the release of the Jetpack. Android KTX is a collection of Kotlin extensions that are designed to facilitate developers to remove boilerplate code as well as to write concise code while developing android applications with Kotlin language. Here KTX in the name stands for Kotlin Extensions. Below is an example of a piece of code without using and after using the Android KTX library:

Code snippet of SQLite without using KTX library:

db.beginTransaction()

try {

 // insert data

 db.setTransactionSuccessful()

finally {

 db.endTransaction()

}

Above code after using KTX library:

db.transaction {

   // insert data

}

This example clearly depicts how the Android KTX library reduced the lines of code and gracefully organized the SQLite transactions using a simple function along with a trailing lambda. Many of the jetpack libraries are linked with various Android KTX modules. For example, one can use the below extensions while working with the Navigation component of Android Jetpack.  

3. Test

This part of the foundation component includes the Espresso UI testing framework for the runtime UI test and AndroidJUnitRunner for the purpose of unit testing of Android applications. Espresso is primarily used for the testing of UI elements. Further, AndroidJUnitRunner performs small tests on the logic of each and every method. It assures the fast and accurate testing of a specific piece of logic within the project code.

4. Multidex

The multidex property of Android is one of the most important features while developing mobile applications. Dex is the format of the executable file which runs on the Android virtual machine(known as Dalvik). To make a .dex file according to the Dalvik Executable specification, it must not contain more than 65,536 methods considering all libraries of the project. 

While making any real-life mobile application, the count of methods in project libraries can easily cross that number. In this situation, developers take the help of Multidex library which performs the system split of the .dex file of the application into multiple .dex files. Further, the Multidex component also provides support to the collective dex files of an application.

Detecting the need for Multidex in the application:

It is impractical for anyone to keep a record of method count while developing an application. Thus to know if there is a need for Multidex in the project files or not, one should look out for the following errors. If the IDE shows one of these errors while building the application files, then it means there is a need to enable Multidex.

Error Message 1: 

trouble writing output:

Too many field references: 131000; max is 65536.

You may try using –multi-dex option. 

Error Message 2: 

Conversion to Dalvik format failed:

Unable to execute dex: method ID not in [0, 0xffff]: 65536

Enable Multidex:

The code to enable Multidex depends upon the minimum SDK version of the android app. For different SDK versions, below is the code snippet of the build.gradle file.

a. For minimum SDK version equal to or above 21:

android{

   // …

   defaultConfig {

       // …

       minSdkVersion 21

       targetSdkVersion 30

       multiDexEnabled true

   }

   // …

}

b. For minimum SDK version less than 21:

i. For AndroidX

android {

   defaultConfig {

       // …

       minSdkVersion 18

       targetSdkVersion 30

       multiDexEnabled true

   }

   // …

}

dependencies {

   // …

   implementation ‘androidx.multidex:multidex:2.0.0’

}

ii. For old Support Library:

android {

   defaultConfig {

       // …

       minSdkVersion 18

       targetSdkVersion 30

       multiDexEnabled true

   }

   // …

}

dependencies {

   // …

   implementation ‘com.android.support:multidex:1.0.3’

}


Article Tags :