Android Fundamentals
Android operating system is a stack of software components which is roughly divided into five
sections and four main layers as shown below in the architecture diagram.
Linux kernel
At the bottom of the layers is Linux - Linux 3.6 with approximately 115 patches. This provides a
level of abstraction between the device hardware and it contains all the essential hardware drivers
like camera, keypad, display etc. Also, the kernel handles all the things that Linux is really good at
such as networking and a vast array of device drivers, which take the pain out of interfacing to
peripheral hardware.
Libraries
On top of Linux kernel there is a set of libraries including open-source Web browser engine
WebKit, well known library libc, SQLite database which is a useful repository for storage and
sharing of application data, libraries to play and record audio and video, SSL libraries responsible
for Internet security etc.
Android Libraries
This category encompasses those Java-based libraries that are specific to Android development.
Examples of libraries in this category include the application framework libraries in addition to
those that facilitate user interface building, graphics drawing and database access. A summary of
some key core Android libraries available to the Android developer is as follows −
● [Link] − Provides access to the application model and is the cornerstone of all
Android applications.
● [Link] − Facilitates content access, publishing and messaging between
applications and application components.
● [Link] − Used to access data published by content providers and includes
SQLite database management classes.
● [Link] − A Java interface to the OpenGL ES 3D graphics rendering API.
● [Link] − Provides applications with access to standard operating system services
including messages, system services and inter-process communication.
● [Link] − Used to render and manipulate text on a device display.
● [Link] − The fundamental building blocks of application user interfaces.
● [Link] − A rich collection of pre-built user interface components such as buttons,
labels, list views, layout managers, radio buttons etc.
● [Link] − A set of classes intended to allow web-browsing capabilities to be built
into applications.
Having covered the Java-based core libraries in the Android runtime, it is now time to turn our
attention to the C/C++ based libraries contained in this layer of the Android software stack.
Android Runtime
This is the third section of the architecture and available on the second layer from the bottom. This
section provides a key component called Dalvik Virtual Machine which is a kind of Java Virtual
Machine specially designed and optimized for Android.
The Dalvik VM makes use of Linux core features like memory management and multi-threading,
which is intrinsic in the Java language. The Dalvik VM enables every Android application to run in
its own process, with its own instance of the Dalvik virtual machine.
The Android runtime also provides a set of core libraries which enable Android application
developers to write Android applications using standard Java programming language.
Application Framework
The Application Framework layer provides many higher-level services to applications in the form
of Java classes. Application developers are allowed to make use of these services in their
applications.
The Android framework includes the following key services −
● Activity Manager − Controls all aspects of the application lifecycle and activity stack.
● Content Providers − Allows applications to publish and share data with other applications.
● Resource Manager − Provides access to non-code embedded resources such as strings,
color settings and user interface layouts.
● Notifications Manager − Allows applications to display alerts and notifications to the user.
● View System − An extensible set of views used to create application user interfaces.
Applications
You will find all the Android application at the top layer. You will write your application to be
installed on this layer only. Examples of such applications are Contacts Books, Browser, Games etc.
****
Android apps can be written using Kotlin, Java, and C++ languages. The Android SDK tools
compile your code along with any data and resource files into an APK or an Android App Bundle.
An Android package, which is an archive file with an .apk suffix, contains the contents of an
Android app that are required at runtime and it is the file that Android-powered devices use to
install the app.
An Android App Bundle, which is an archive file with an .aab suffix, contains the contents of an
Android app project including some additional metadata that is not required at runtime. An AAB is
a publishing format and is not installable on Android devices, it defers APK generation and signing
to a later stage. When distributing your app through Google Play for example, Google Play's servers
generate optimized APKs that contain only the resources and code that are required by a particular
device that is requesting installation of the app.
Each Android app lives in its own security sandbox, protected by the following Android security
features:
● The Android operating system is a multi-user Linux system in which each app is a different
user.
● By default, the system assigns each app a unique Linux user ID (the ID is used only by the
system and is unknown to the app). The system sets permissions for all the files in an app so
that only the user ID assigned to that app can access them.
● Each process has its own virtual machine (VM), so an app's code runs in isolation from other
apps.
● By default, every app runs in its own Linux process. The Android system starts the process
when any of the app's components need to be executed, and then shuts down the process
when it's no longer needed or when the system must recover memory for other apps.
The Android system implements the principle of least privilege. That is, each app, by default, has
access only to the components that it requires to do its work and no more. This creates a very secure
environment in which an app cannot access parts of the system for which it is not given permission.
However, there are ways for an app to share data with other apps and for an app to access system
services:
The rest of this document introduces the following concepts:
● The core framework components that define your app.
● The manifest file in which you declare the components and the required device features for
your app.
● Resources that are separate from the app code and that allow your app to gracefully optimize
its behavior for a variety of device configurations.
App components
App components are the essential building blocks of an Android app. Each component is an entry
point through which the system or a user can enter your app. Some components depend on others.
There are four different types of app components:
● Activities
● Services
● Broadcast receivers
● Content providers
Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is
created and destroyed.
Activities
An activity is the entry point for interacting with the user. It represents a single screen with a
user interface. For example, an email app might have one activity that shows a list of new
emails, another activity to compose an email, and another activity for reading emails.
Although the activities work together to form a cohesive user experience in the email app,
each one is independent of the others. As such, a different app can start any one of these
activities if the email app allows it. For example, a camera app can start the activity in the
email app that composes new mail to allow the user to share a picture. An activity facilitates
the following key interactions between system and app:
● Keeping track of what the user currently cares about (what is on screen) to ensure that the
system keeps running the process that is hosting the activity.
● Knowing that previously used processes contain things the user may return to (stopped
activities), and thus more highly prioritize keeping those processes around.
● Helping the app handle having its process killed so the user can return to activities with their
previous state restored.
● Providing a way for apps to implement user flows between each other, and for the system to
coordinate these flows. (The most classic example here being share.)
You implement an activity as a subclass of the Activity class. For more information about the
Activity class, see the Activities developer guide.
Services
A service is a general-purpose entry point for keeping an app running in the background for
all kinds of reasons. It is a component that runs in the background to perform long-running
operations or to perform work for remote processes. A service does not provide a user
interface. For example, a service might play music in the background while the user is in a
different app, or it might fetch data over the network without blocking user interaction with an
activity. Another component, such as an activity, can start the service and let it run or bind to
it in order to interact with it.
There are two types of services that tell the system how to manage an app: started services and
bound services.
Started services tell the system to keep them running until their work is completed. This could be
to sync some data in the background or play music even after the user leaves the app. Syncing data
in the background or playing music also represent two different types of started services that modify
how the system handles them:
● Music playback is something the user is directly aware of, so the app tells the system this by
saying it wants to be foreground with a notification to tell the user about it; in this case the
system knows that it should try really hard to keep that service's process running, because
the user will be unhappy if it goes away.
● A regular background service is not something the user is directly aware as running, so the
system has more freedom in managing its process. It may allow it to be killed (and then
restarting the service sometime later) if it needs RAM for things that are of more immediate
concern to the user.
Bound services run because some other app (or the system) has said that it wants to make use of
the service. This is basically the service providing an API to another process. The system thus
knows there is a dependency between these processes, so if process A is bound to a service in
process B, it knows that it needs to keep process B (and its service) running for A. Further, if
process A is something the user cares about, then it also knows to treat process B as something the
user also cares about.
Because of their flexibility (for better or worse), services have turned out to be a really useful
building block for all kinds of higher-level system concepts. Live wallpapers, notification listeners,
screen savers, input methods, accessibility services, and many other core system features are all
built as services that applications implement and the system binds to when they should be running.
A service is implemented as a subclass of Service. For more information about the Service
class, see the Services developer guide.
Broadcast receivers
A broadcast receiver is a component that enables the system to deliver events to the app
outside of a regular user flow, allowing the app to respond to system-wide broadcast
announcements. Because broadcast receivers are another well-defined entry into the app, the
system can deliver broadcasts even to apps that aren't currently running. So, for example, an
app can schedule an alarm to post a notification to tell the user about an upcoming event... and
by delivering that alarm to a BroadcastReceiver of the app, there is no need for the app to
remain running until the alarm goes off. Many broadcasts originate from the system—for
example, a broadcast announcing that the screen has turned off, the battery is low, or a picture
was captured. Apps can also initiate broadcasts—for example, to let other apps know that
some data has been downloaded to the device and is available for them to use. Although
broadcast receivers don't display a user interface, they may create a status bar notification to
alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is
just a gateway to other components and is intended to do a very minimal amount of work. For
instance, it might schedule a JobService to perform some work based on the event with
JobScheduler
A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast
is delivered as an Intent object. For more information, see the BroadcastReceiver class.
Content providers
A content provider manages a shared set of app data that you can store in the file system, in a
SQLite database, on the web, or on any other persistent storage location that your app can
access. Through the content provider, other apps can query or modify the data if the content
provider allows it. For example, the Android system provides a content provider that manages
the user's contact information. As such, any app with the proper permissions can query the
content provider, such as [Link], to read and write information about
a particular person. It is tempting to think of a content provider as an abstraction on a
database, because there is a lot of API and support built in to them for that common case.
However, they have a different core purpose from a system-design perspective. To the
system, a content provider is an entry point into an app for publishing named data items,
identified by a URI scheme. Thus an app can decide how it wants to map the data it contains
to a URI namespace, handing out those URIs to other entities which can in turn use them to
access the data. There are a few particular things this allows the system to do in managing an
app:
● Assigning a URI doesn't require that the app remain running, so URIs can persist after their
owning apps have exited. The system only needs to make sure that an owning app is still
running when it has to retrieve the app's data from the corresponding URI.
● These URIs also provide an important fine-grained security model. For example, an app can
place the URI for an image it has on the clipboard, but leave its content provider locked up
so that other apps cannot freely access it. When a second app attempts to access that URI on
the clipboard, the system can allow that app to access the data via a temporary URI
permission grant so that it is allowed to access the data only behind that URI, but nothing
else in the second app.
Content providers are also useful for reading and writing data that is private to your app and not
shared.
A content provider is implemented as a subclass of ContentProvider and must implement a
standard set of APIs that enable other apps to perform transactions. For more information, see the
Content Providers developer guide.
A unique aspect of the Android system design is that any app can start another app’s component.
For example, if you want the user to capture a photo with the device camera, there's probably
another app that does that and your app can use it instead of developing an activity to capture a
photo yourself. You don't need to incorporate or even link to the code from the camera app. Instead,
you can simply start the activity in the camera app that captures a photo. When complete, the photo
is even returned to your app so you can use it. To the user, it seems as if the camera is actually a
part of your app.
When the system starts a component, it starts the process for that app if it's not already running and
instantiates the classes needed for the component. For example, if your app starts the activity in the
camera app that captures a photo, that activity runs in the process that belongs to the camera app,
not in your app's process. Therefore, unlike apps on most other systems, Android apps don't have a
single entry point (there's no main() function).
Because the system runs each app in a separate process with file permissions that restrict access to
other apps, your app cannot directly activate a component from another app. However, the Android
system can. To activate a component in another app, deliver a message to the system that specifies
your intent to start a particular component. The system then activates the component for you.
Activating components
Three of the four component types—activities, services, and broadcast receivers—are activated by
an asynchronous message called an intent. Intents bind individual components to each other at
runtime. You can think of them as the messengers that request an action from other components,
whether the component belongs to your app or another.
An intent is created with an Intent object, which defines a message to activate either a specific
component (explicit intent) or a specific type of component (implicit intent).
For activities and services, an intent defines the action to perform (for example, to view or send
something) and may specify the URI of the data to act on, among other things that the component
being started might need to know. For example, an intent might convey a request for an activity to
show an image or to open a web page. In some cases, you can start an activity to receive a result, in
which case the activity also returns the result in an Intent. For example, you can issue an intent
to let the user pick a personal contact and have it returned to you. The return intent includes a URI
pointing to the chosen contact.
For broadcast receivers, the intent simply defines the announcement being broadcast. For example,
a broadcast to indicate the device battery is low includes only a known action string that indicates
battery is low.
The manifest file
Before the Android system can start an app component, the system must know that the component
exists by reading the app's manifest file, [Link]. Your app must declare all its
components in this file, which must be at the root of the app project directory.
The manifest does a number of things in addition to declaring the app's components, such as the
following:
● Identifies any user permissions the app requires, such as Internet access or read-access to the
user's contacts.
● Declares the minimum API Level required by the app, based on which APIs the app uses.
● Declares hardware and software features used or required by the app, such as a camera,
bluetooth services, or a multitouch screen.
● Declares API libraries the app needs to be linked against (other than the Android framework
APIs), such as the Google Maps library.
Declaring components
The primary task of the manifest is to inform the system about the app's components. For example,
a manifest file can declare an activity as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:icon="@drawable/app_icon.png" ... >
<activity android:name="[Link]"
android:label="@string/example_label" ... >
</activity>
...
</application>
</manifest>
In the <application> element, the android:icon attribute points to resources for an icon
that identifies the app.
In the <activity> element, the android:name attribute specifies the fully qualified class
name of the Activity subclass and the android:label attribute specifies a string to use as
the user-visible label for the activity.
You must declare all app components using the following elements:
● <activity> elements for activities.
● <service> elements for services.
● <receiver> elements for broadcast receivers.
● <provider> elements for content providers.
Activities, services, and content providers that you include in your source but do not declare in the
manifest are not visible to the system and, consequently, can never run. However, broadcast
receivers can be either declared in the manifest or created dynamically in code as
BroadcastReceiver objects and registered with the system by calling
registerReceiver().
Declaring component capabilities
As discussed above, in Activating components, you can use an Intent to start activities, services,
and broadcast receivers. You can use an Intent by explicitly naming the target component (using
the component class name) in the intent. You can also use an implicit intent, which describes the
type of action to perform and, optionally, the data upon which you’d like to perform the action. The
implicit intent allows the system to find a component on the device that can perform the action and
start it. If there are multiple components that can perform the action described by the intent, the user
selects which one to use.
The system identifies the components that can respond to an intent by comparing the intent received
to the intent filters provided in the manifest file of other apps on the device.
When you declare an activity in your app's manifest, you can optionally include intent filters that
declare the capabilities of the activity so it can respond to intents from other apps. You can declare
an intent filter for your component by adding an <intent-filter> element as a child of the
component's declaration element.
For example, if you build an email app with an activity for composing a new email, you can declare
an intent filter to respond to "send" intents (in order to send a new email), as shown in the following
example:
<manifest ... >
...
<application ... >
<activity android:name="[Link]">
<intent-filter>
<action android:name="[Link]" />
<data android:type="*/*" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
If another app creates an intent with the ACTION_SEND action and passes it to
startActivity(), the system may start your activity so the user can draft and send an email.
Declaring app requirements
There are a variety of devices powered by Android and not all of them provide the same features
and capabilities. To prevent your app from being installed on devices that lack features needed by
your app, it's important that you clearly define a profile for the types of devices your app supports
by declaring device and software requirements in your manifest file. Most of these declarations are
informational only and the system does not read them, but external services such as Google Play do
read them in order to provide filtering for users when they search for apps from their device.
App resources
An Android app is composed of more than just code—it requires resources that are separate from
the source code, such as images, audio files, and anything relating to the visual presentation of the
app. For example, you can define animations, menus, styles, colors, and the layout of activity user
interfaces with XML files. Using app resources makes it easy to update various characteristics of
your app without modifying code. Providing sets of alternative resources enables you to optimize
your app for a variety of device configurations, such as different languages and screen sizes.
For every resource that you include in your Android project, the SDK build tools define a unique
integer ID, which you can use to reference the resource from your app code or from other resources
defined in XML. For example, if your app contains an image file named [Link] (saved in the
res/drawable/ directory), the SDK tools generate a resource ID named [Link].
This ID maps to an app-specific integer, which you can use to reference the image and insert it in
your user interface.
What is API Level?
API Level is an integer value that uniquely identifies the framework API revision offered by a
version of the Android platform.
The Android platform provides a framework API that applications can use to interact with the
underlying Android system. The framework API consists of:
● A core set of packages and classes
● A set of XML elements and attributes for declaring a manifest file
● A set of XML elements and attributes for declaring and accessing resources
● A set of Intents
● A set of permissions that applications can request, as well as permission enforcements
included in the system
Each successive version of the Android platform can include updates to the Android application
framework API that it delivers.
Platform API VERSION_COD
Version Level E
Android 12 31 S
Android 11 30 R
Android 10 29 Q
Android 9 28 P
Three-Tier Architecture
Three-tier architecture, which separates applications into three logical and physical computing tiers,
is the predominant software architecture for traditional client-server applications.
What is three-tier architecture?
Three-tier architecture is a well-established software application architecture that organizes
applications into three logical and physical computing tiers: the presentation tier, or user interface;
the application tier, where data is processed; and the data tier, where the data associated with the
application is stored and managed.
The chief benefit of three-tier architecture is that because each tier runs on its own infrastructure,
each tier can be developed simultaneously by a separate development team, and can be updated or
scaled as needed without impacting the other tiers.
For decades three-tier architecture was the prevailing architecture for client-server
applications. Today, most three-tier applications are targets for modernization, using cloud-native
technologies such as containers and microservices, and for migration to the cloud.
The three tiers in detail
Presentation tier
The presentation tier is the user interface and communication layer of the application, where the end
user interacts with the application. Its main purpose is to display information to and collect
information from the user. This top-level tier can run on a web browser, as desktop application, or a
graphical user interface (GUI), for example. Web presentation tiers are usually developed using
HTML, CSS and JavaScript. Desktop applications can be written in a variety of languages
depending on the platform.
Application tier
The application tier, also known as the logic tier or middle tier, is the heart of the application. In this
tier, information collected in the presentation tier is processed - sometimes against other
information in the data tier - using business logic, a specific set of business rules. The application
tier can also add, delete or modify data in the data tier.
The application tier is typically developed using Python, Java, Perl, PHP or Ruby, and
communicates with the data tier using API calls.
Data tier
The data tier, sometimes called database tier, data access tier or back-end, is where the information
processed by the application is stored and managed. This can be a relational database management
system such as PostgreSQL, MySQL, MariaDB, Oracle, DB2, Informix or Microsoft SQL Server,
or in a NoSQL Database server such as Cassandra, CouchDB or MongoDB.
In a three-tier application, all communication goes through the application tier. The presentation tier
and the data tier cannot communicate directly with one another.
Tier vs. layer
In discussions of three-tier architecture, layer is often used interchangeably – and mistakenly –
for tier, as in 'presentation layer' or 'business logic layer.'
They aren't the same. A 'layer' refers to a functional division of the software, but a 'tier' refers to a
functional division of the software that runs on infrastructure separate from the other divisions. The
Contacts app on your phone, for example, is a three-layer application, but a single-tier
application, because all three layers run on your phone.
The difference is important, because layers can't offer the same benefits as tiers.
Benefits of three-tier architecture
Again, the chief benefit of three-tier architecture its logical and physical separation of functionality.
Each tier can run on a separate operating system and server platform - e.g., web server, application
server, database server - that best fits its functional requirements. And each tier runs on at least one
dedicated server hardware or virtual server, so the services of each tier can be customized and
optimized without impact the other tiers.
Other benefits (compared to single- or two-tier architecture) include:
● Faster development: Because each tier can be developed simultaneously by different teams,
an organization can bring the application to market faster, and programmers can use the
latest and best languages and tools for each tier.
● Improved scalability: Any tier can be scaled independently of the others as needed.
● Improved reliability: An outage in one tier is less likely to impact the availability or
performance of the other tiers.
● Improved security: Because the presentation tier and data tier can't communicate directly, a
well-designed application tier can function as a sort of internal firewall, preventing SQL
injections and other malicious exploits.
Three-tier application in web development
In web development, the tiers have different names but perform similar functions:
● The web server is the presentation tier and provides the user interface. This is usually a web
page or web site, such as an ecommerce site where the user adds products to the shopping
cart, adds payment details or creates an account. The content can be static or dynamic, and is
usually developed using HTML, CSS and Javascript .
● The application server corresponds to the middle tier, housing the business logic used to
process user inputs. To continue the ecommerce example, this is the tier that queries the
inventory database to return product availability, or adds details to a customer's profile. This
layer often developed using Python, Ruby or PHP and runs a framework such as e Django,
Rails, Symphony or [Link], for example.
● The database server is the data or backend tier of a web application. It runs on database
management software, such as MySQL, Oracle, DB2 or PostgreSQL, for example.