The Wayback Machine - https://web.archive.org/web/20240930190141/https://www.geeksforgeeks.org/foundation-components-of-android-jetpack/
Open In App

Foundation Components of Android Jetpack

Last Updated : 16 Sep, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

  • App bar: The topmost element in an application is its app bar/action bar which generally contains the name of the application or current activity name. AppCompat library facilitates the developers to design a more customized App bar. It provides a toolbar for designing and painting the background of the app bar with primary colors or gradient background(multiple colors).
  • Navigation drawer: It is the left pane menu that provides an easy navigation guide of the application. This is a very common design pattern nowadays, and so it is needed to add very often. By using the AppCompat library, it is very straightforward to manage the colors and icons of the navigation drawer. In order to create a navigation drawer, developers just required to add a DrawerLayout with the main view and a NavigationView. Further, this library also assists in managing the active state of each menu element with XML resources.
  • Permissions: In the newer version of android OS, applications ask permission from users in order to access device hardware such as camera, microphone, etc or to perform certain actions. The further operations performed by the application depend upon the choice selected by the user i.e., whether permission is granted or not. However, this was not the case before the Marshmallow(6.0) version of the Android OS. In older versions, developers do not need to write any code in the SDK which asks permission from the users and check whether it is granted or not. This problem has been solved by the ContextCompat(a component of AppCompat) which checks the permission status of an operation and request from the user if needed irrespective of the android OS version.
  • Resources: Previously, more lines of code are needed to design the XML resources like drawable resources which are supported by a specific Android OS version. Using ContextCompat(part of AppCompat), the number of lines of code reduce drastically as well as the dependency of drawables on the OS version also removed.
  • Dialog box: The AppCompat library has an AppCompatDialog component which is similar to the toolbar. It helps in making the material design dialog box in an application.
  • Share actions: Sharing of any document/file from one application to another platform is possible because of the AppCompat library. The list of share actions like Gmail, Facebook, message, etc which appears on the screen when a document/file is selected to share is provided by the Activity toolbar of 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.  

  • android.arch.navigation:navigation-common-ktx
  • android.arch.navigation:navigation-fragment-ktx
  • android.arch.navigation:navigation-runtime-ktx
  • and android.arch.navigation:navigation-ui-ktx

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’

}



Previous Article
Next Article

Similar Reads

Jetpack Architecture Components in Android
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
10 min read
Behaviour 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
10 min read
UI 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
14 min read
Android Runtime Permissions with Dexter using Android Jetpack Compose
Android applications require the usage of hardware devices within the android applications such as microphones or cameras. For using these devices within any android application. Permissions for using this hardware have to be provided to the application. For giving these permissions to the application runtime permissions are used. Handling permissi
7 min read
Horizontal ListView in Android using Jetpack Compose
Many Android Applications present data in the form of horizontally scrollable lists. This list can be scrolled horizontally. In this article, we will take a look at How to Implement Horizontal ListView in Android Applications using Jetpack Compose. Note: If you are seeking Java code for Jetpack Compose, please note that Jetpack Compose is only avai
6 min read
Material Design Text Input Field using Jetpack Compose in Android
Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs. Compose is built to support material design principles. Many of its UI elements implement material design out of the box. In this article, we will explain
3 min read
How to Remove TextField Padding in Android using Jetpack Compose?
In Android Jetpack Compose, a TextField is used to take input from the user in the form of text. TextField when in focus or when clicked invokes a soft keyboard. In general, TextField is highly attributed for a better feel and appearance. However, we can use a pre-developed TextField, named BasicTextField, which has no attributes like that of a reg
3 min read
WebView in Android using Jetpack Compose
In Android, a WebView is an extension of View Class that allows us to display webpages. In simpler words, if you want to display a webpage in Activity Layout, then you can implement a WebView to display it. It is very similar to a browser but isn't one. A WebView can rather be referred to as a show or a preview of a browser as it lacks most functio
3 min read
Motion Layout Button in Android Jetpack Compose
Motion Layout is a special version of Constraint layout. With the help of motion layout, we can add animations to the widgets within the layout and change the position of that widget dynamically. In this article, we will take a look at How to implement Motion Layout animation on buttons in Android using Jetpack compose. What we are going to build i
8 min read
Play Audio in Android using Jetpack Compose
In Android, a MediaPlayer is used to play media files like audio and video files in the application. The MediaPlayer Class uses the MediaPlayer API for performing various functions like creating a MediaPlayer, playing, pausing, starting, and stopping the media. In this article, we will show you how you could play an Audio File on Android using Jetp
3 min read
Android Jetpack - WorkManager with Example
WorkManager is an Android Jetpack library that provides a flexible and efficient way to schedule and manage background tasks in your app. It is a powerful and recommended tool for managing background processing in Android applications. With WorkManager, you can schedule asynchronous, deferred, and periodic tasks that need to run even when the app i
5 min read
Curved Text in Android using Jetpack Compose
In Android, there are no inbuilt schemes to implement designed text like the word art, which is available in applications like MS Word. We can always refer to or download such schemes like designed font faces to implement designed text. However, it is also possible to natively build (building from scratch) such designed texts in an Android Project.
3 min read
How to Get Screen Width and Height in Android using Jetpack Compose?
Android applications are being developed for different device orientations to support a huge range of devices. So that users with different device size configurations can use the application. Many applications need to get the height and width of the device screen to create UI. In this article, we will take a look at How to get Screen Width and Heig
5 min read
Extract Data From PDF File using Android Jetpack Compose
PDF files are used in many android applications for displaying data in the form of images, text as well as graphics. Many applications are also required to get the data from this PDF file and display this data within the android application. So for extracting the data from PDF file pdf reader is used which is used to read pdf and get the data from
6 min read
Introduction to Android Jetpack
The Android support libraries are used in almost all android applications to overcome the compatibility issues across different Android OS versions and devices. These libraries also facilitate users to add various kinds of updated widgets in the application. Over time, these libraries are updated according to the latest android versions. The proble
6 min read
Jetpack Compose Navigation and Passing Data in Android
Almost every app uses some kind of navigation, allows users to move from one screen to another. In this article, we will learn to implement Navigation in Jetpack Compose using Compose way. We will build a simple app demonstrating Jetpack compose navigation, It will have three screens(Home, Profile, and Settings). It will navigate to Settings Screen
4 min read
Basics of Jetpack Compose in Android
Jetpack Compose is a modern UI toolkit that is designed to simplify UI development in Android. It consists of a reactive programming model with conciseness and ease of Kotlin programming language. It is fully declarative so that you can describe your UI by calling some series of functions that will transform your data into a UI hierarchy. When the
5 min read
TopAppBar in Android using Jetpack Compose
TopAppBar is similar to that of the Action Bar widget in Android. This is one of the most UI components in Android. The action bar is used to represent the app name and action items in Android. In this article, we will take a look at the implementation of the TopAppBar in Android using Jetpack compose. Attributes of Top App Bar WidgetAttributes Des
3 min read
View Binding in Android Jetpack
View Binding is one of the best features which provides the views to bind with the activity which is ongoing. Replacing the findViewById() method, hence reducing the boilerplate code, generated the instances of the views of the current layout. And most important feature about the View Binding is it's always null safe. In this article detailed it's
4 min read
View Binding with Fragments in Android Jetpack
In the Previous article View Binding in Android Jetpack, it's been discussed why acquiring the ViewBinding feature in the Android project provides a lot of benefits. But when it comes to ViewBinding with fragments the scenario changes. Because the lifecycle of the Fragment is different and that of activity is different Here also things go the same
6 min read
How to Draw Track on Google Maps in Android using Jetpack Compose?
In many android apps, we have seen that there is a route marker from a source location to the destination location. In this article, we will take a look at How we can draw a track on Google Maps in Android using Jetpack Compose. Step By Step ImplementationStep 1: Create a New Project in Android StudioTo create a new project in Android Studio please
6 min read
Material Design Buttons using Jetpack Compose in Android
Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs. Compose is built to support material design principles. Many of its UI elements implement material design out of the box. In this article, we will explain
4 min read
Custom Chips using Jetpack Compose in Android
Chips in android are one of the components which are used to make the choice filters, actions, and display the selectable options in the compact area of the Android Window. In this article, we will use Android's Jetpack Compose to create those chips. A sample image is given below to give an idea of what we are going to build. Note that we are going
5 min read
Android Jetpack Compose - How to Use Phone Selector API
In most android applications, we get to see that users don't have to enter their phone numbers for authentication. A pop-up message is triggered automatically which displays the phone number of the user which is present on the device. This feature helps users to easily authenticate within the application and reduces the efforts. In this article, we
5 min read
How to make Text Selectable in Android using Jetpack Compose?
By default, Composable Texts are not selectable which means users cannot copy text from your application, and to enable text selection you need to wrap the text elements into Selection Container. In this article, we will use Android’s Jetpack Compose to create those chips. A sample image is given below to give an idea of what we are going to build.
2 min read
How to Create a Timer using Jetpack Compose in Android?
Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs. In this article, we are going to create a Timer using Jetpack Compose. Below is the sample video to show what we are going to build. [video mp4="https://m
4 min read
ConstraintLayout in Android using Jetpack Compose
ConstraintLayout is the view system in a ViewGroup to place widgets relative to the position of other widgets on the screen. In jetpack compose, almost every layout can be created using Rows and Columns but if you want to create larger complicated Layouts you can also use Constraint Layout as an alternative to Rows and Columns. In this article, we
5 min read
How to Create Superscript and Subscript Text using Jetpack Compose in Android?
In this article, we will explain how to create a Superscript and Subscript text using Jetpack Compose. Below is the sample picture to show what we are going to build. Note that we are going to implement this project using the Kotlin language. Prerequisites: Basic Knowledge of Kotlin.Jetpack Compose in Android.Step by Step Implementation Step 1: Cre
2 min read
Android Jetpack Compose - Implement Different Types of Styles and Customization to Text
Jetpack Compose is a new UI toolkit from Google used to create native Android UI. It speeds up and simplifies UI development using less code, Kotlin APIs, and powerful tools. Prerequisites:Familiar with Kotlin and OOP Concepts as wellBasic understanding of Jetpack ComposeAndroid Studio Canary Version A sample video is given below to get an idea abo
14 min read
What is Android Jetpack and Why Should We Use It?
Android Jetpack is a set of Android software components that aid in the development of outstanding Android apps. These software components aid in the following tasks: Adhering to best practices and creating boilerplate codeMaking complicated things simple. Previously, there were several challenges, which are as follows: Managing the lifecycles of a
3 min read