0% found this document useful (0 votes)
2 views

Unit -I Notes

The document provides an overview of Android, detailing its history, architecture, and ecosystem. It explains the components of Android applications, including activities, services, content providers, and broadcast receivers, as well as the features and APIs of the Android platform. Additionally, it highlights the role of the Open Handset Alliance and the importance of the Linux kernel in the Android architecture.

Uploaded by

ssathya60612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit -I Notes

The document provides an overview of Android, detailing its history, architecture, and ecosystem. It explains the components of Android applications, including activities, services, content providers, and broadcast receivers, as well as the features and APIs of the Android platform. Additionally, it highlights the role of the Open Handset Alliance and the importance of the Linux kernel in the Android architecture.

Uploaded by

ssathya60612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Unit – I

• Introducing Android :

Android Inc was founded in Palo Alto of California, US by Andy Rubin, Rich Miner, Nick
Scars ant Chris White during 2003. After that it was acquired by Google in 2005.

• What is Android ?

1. Android is a stack of software for mobile devices that are an Operating System, Middleware
and Key Applications.

Operating
System

Middleware

Key
Application

Fig. 1.1 Android Operating System

2. Android is a Linux-based operating system which is designed for touchscreen mobile


devices like smartphones, tablet, computers, wrist watch and LED - LCD android TV’s.
3. It is an open source technology that allows the software to be freely modified and
distributed by device manufacturers, wireless carriers and developers.
4. Android is written in C, C++ and JAVA Programming Languages java lies at the top of its all
development layers and therefore its applications are primarily written in JAVA.
5. Android was invented during 2007 along with the founding of the Open Handset Alliance
(OHA).

Fig. 1.2 Android Logo


• What is “Open Handset Alliance” (OHA) ?
Open Handset Alliance (OHA) was formed in November 2007. The OHA is the group that
is in charge of the Android smartphones operating system. It was created by Google. The Open
Handset Alliance (OHA) is a business alliance that consists of 47 companies for developing open
standard platform for mobile devices. The members of OHA include handset manufactures, chip
makers, commercialization companies, software companies and mobile operators.

Handset
Manufactures

Mobile Operators Chip Makers

Open Handset Alliance


( OHA )

Commercialization
Software
Companies
companies

Fig. 1.3 Android Open Handset Alliance

• Android Ecosystem :
For Android and develop Android Applications, it is very important to understand the
Entire ecosystem of Android and the stakeholders of this ecosystem :

Google
Original
Consumers Equipment
Manufacturer
(OEM)
Android

Application
Development
Freelancers Centers
Companies
Fig. 1.4 Android Ecosystem

The stakeholders are the Consumers that own Android Devices. But there are others as well :
• Google : It develops Android.
• OEMs (Original Equipment Manufactures) : They manufacture the hardware and as
well the custom applications computers. Example : LG, Sony, HTC, Samsung, etc.

• Application Development Companies : They are the major contributors to the echo
system and employ Android developers, and also contract out the product development
to services companies. Example : WhatsApp, Facebook, Angry Birds Gaming App, etc.

• Freelance Android Developers : Developers have the skill set to development they are
to create their own application and published them on to Google play store, freelancers
can also generates money by developing applications for product companies.
• Describing Android Architecture :
The architecture of an Android system consists of a software stack of different
layers. Each layer contains group of various program components. The software stack of
architecture works in a way that each layer provides services to the layer just above it.
Together, it includes OS, middleware, and applications. Putting all layers together comprises
and architecture called Android Architecture.

Applications

Home Contacts Phone Browser Browser

Application Framework

Activity Window Content View


Manager Manager Providers System

Package Telephony Resource Location Notification


Manager Manager Manager Manager Manager

Libraries Android Runtime


Surface Manager SSL SGL SQLite Core Libraries

Media Component WebKite OpenGL


Dalvik Virtual Machine

Linux Kernel
Brinder (IPS)
Display Driver Camera Driver Flash Memory Driver Driver

Power
Keypad Driver WiFi Driver Audio Drivers
Management

Fig. 1.5 Android Architecture

Android Architecture as a following layers are :-

1. Application Layer
2. Application Framework Layer
3. Android Runtime
4. Libraries
5. Linux Kernal
As the Android Architecture is a software stack of different layers. Let’s discuss the each layer
one-by-one.

5. Linux Kernal : Linux Kernal is the root layer in the android architecture. The interaction of
the hardware into software is provided by means of Linux. All the hardware devices are handled
by this Kernal.
Drivers are the small program written specifically to communicate with the hardware.
It has all the major hardware drivers a000s display driver, camera driver, power management
driver, and so forth.
For example, the Linux kernel has camera driver. On installing the Android OS in a compatible
device, the Linux kernel will search the camera a then make it available to the system by using
camera driver.
The Linux kernel not only contains hardware drivers, but also serves as the abstraction
layer for other layers Since the core of the Android system lies in the Linux kernel, all the main
functionality of the system is based of Linux. The foundation of Android is Linux, which is a
much powerful system; therefore, it provides a great versatility to the Android OS to work it on
multiple devices without any (or few) glitches.

4. Libraries Layer : The layer next to the Linux kernel layer is the Android's native libraries. This
layer provides the device, which has the capability to handle different types of data and
information. These libraries are coded in C and C++ languages for fast procedural transactions
of data. A brief description of some of the native Android libraries is as follows :

• Media library provides support to play and record an audio and video formats.
• Surface manager responsible for managing access to the display subsystem.
• SGL and OpenGL both cross-language, cross-platform application program interface
(API) are used for 2D and 3D computer graphics.
• SQLite provides database support and FreeType provides font support.
• Web-Kit : This open source web browser engine provides all the functionality to display
web content and to simplify page loading.
• SSL (Secure Sockets Layer) is security technology to establish an encrypted link
between a web server and a web browser.

3. Android Runtime : Consist of Core Libraries and Dalvik Virtual Machine.

a. Dalvik Virtual Machine (DVM) : The Dalvik Virtual Machine (DVM) is an android virtual
machine optimizes for mobile devices. It optimized the virtual machine for memory,
battery life and performance.
Dalvik is a name of town in Iland. The Dalvik VM was written by “Dan Bornstein”.
Fig. 1.5 Dalvik Virtual Machine

• Optimizing the Virtual Machine for memory, battery life, and performance.
• Conversion of class files into .dex file through Dex compiler that runs on Dalvik VM.
• Converting multiple class files into dex files.

The Dex compiler helps convert the class file into .dex file, the following image shows how it
flows:

• First of all the .java file converts into .class file with the help of Java compiler.
• Next .class file converts into .dex file using Dec compiler.
• Then finally the packaging process is handled by the Android Assets packaging (aapt)
tools.

b. Core Libraries :
The core libraries enable android developers to write android application using standard
java programming language.
Core Java Libraries are the exclusively design java libraries for android rather than java SE
(Standard Edition) and java ME (Mobile Edition). However most of the functionality of this
libraries similar to the SE libraries.
This libraries consist of data structure, file access, network access, utilities and Graphics.

2. Android Runtime :
Application framework is of utmost importance for developer as the blocks inside this layer
directly communicate with Android applications.
The basic and much needed functionalities such as resource management and voice call
management are provided by this layer.
From the developer's perspective, this layer provides basic building blocks for Android
applications. The important blocks of Application framework are as follows :

• Activity Manager - Manages the activity life cycle of applications.


• Content Providers - Provide data sharing between applications.
• Telephony Manager - Manages all voice calls. This management can be considered as
optional. If your application is meant to use the voice calls, then telephony manager is used.
• Location Manager - Manages the activities related with location in your application.
• Resource Manager - Manages the different kinds of resources in an application.

1. Application Layer :
Application layer is the top layer in the Android architecture. This is the place where our
application is finally deployed over a device. Some applications are installed by default in a device,
such as SMS client app, dialer, Web browser, and contact manager.

Pre-installed applications can be replaced by newly developed applications. This is the fact
which makes Android OS so flexible. Many device vendors modify the pure Android applications
with their own applications for the sake of different feel and look.
• Android API :
API stands for application programming interface, which, as a portion of code, acts as an
interface for communication among different software. An API contains specifications to develop
a program by means of all the building blocks. These building blocks are then joined by a
programmer.
The applications developed for Android are also called Android apps. Many new features
have been added for users and application developers. Let's discuss the most noteworthy and
useful new APIs for developers. API levels available up to Android version 12 are tabulated in
Table 1.1 :
Table 1.1 Description of the Android API Levels
Version API Level
1.5 3
1.6 4
2.1 7
2.2 8
2.3 – 2.3.2 9
2.3.3 – 2.3.7 10
3.1 12
3.2 13
4.0 14
4.1 16
4.2 17
4.3 18
4.4 19
5.0 21
5.1 22
7.0 24
7.1 25
8.0 26
8.1 27
12 31

1. API level provides the API review offered by a version of the Android platform.
2. The platform provides a framework API, which allows the applications to communicate with
the underlying Android system.
3. The API level for Android consists of packages, classes, Extensible Markup Language (XML)
element and attributes intents, and a set of permissions.
4. The API designers always keep in the mind the backward compatibility with the lower API
levels. For examples - the API level 14 has compatibility with the API level 3.
• Features of Android :
Android, the mobile operating system developed by Google, offers a wide range of
features that have evolved over time. Here are some of the key features of Android :

1. User Interface Customization : Android provides extensive options for customizing the
user interface, including widgets, wallpapers, themes, and icon packs.

2. Multitasking : Users can easily switch between multiple apps and perform tasks
simultaneously using Android's multitasking capabilities, such as split-screen mode and
picture-in-picture mode.

3. Notifications : Android delivers rich and interactive notifications, allowing users to


quickly view and respond to messages, emails, and alerts without opening the respective
apps.

4. Google Assistant : Google's virtual assistant, Google Assistant, is deeply integrated into
Android, providing voice-controlled access to various functions, information, and
services.

5. Google Play Store : Android users have access to a vast ecosystem of apps, games,
movies, books, and music through the Google Play Store, which serves as the primary
marketplace for downloading and purchasing digital content.

6. Security : Android offers various security features, including built-in malware protection,
app sandboxing, secure boot process, and regular security updates provided by Google.

7. Customizability : Android is highly customizable, allowing users to install custom ROMs,


modify system settings, and personalize their devices according to their preferences.

8. Integration with Google Services : Android seamlessly integrates with various Google
services, such as Gmail, Google Maps, Google Drive, and Google Photos, providing a
cohesive ecosystem for users.

9. Cloud Storage : Android devices often come with built-in support for cloud storage
services, enabling users to back up their data, sync files across devices, and access their
content from anywhere.

10. Accessibility : Android offers a wide range of accessibility features, including screen
readers, magnification gestures, captioning, and voice commands, to make the platform
more inclusive for users with disabilities.

11. Device Compatibility : Android runs on a diverse range of devices, including


smartphones, tablets, smartwatches, smart TVs, and automotive systems, catering to
various form factors and use cases.
12. Open Source : Android is based on the Linux kernel and is open source, allowing
developers to modify the source code, create custom distributions (forks), and contribute
to the platform's development.

These are just some of the key features of Android, and the platform continues to evolve with
each new version and update, incorporating new functionalities and improvements.
• Discussing About Android Application :
1. Android applications are developed in Java. Although the full blend of Java is not used, it
can be considered as a part of Java.
2. The Java part used in Android application development does not include the classes that
are not important for mobile devices.
3. XML and Apache Ant are used alongside Java in Android development.
4. Proficiency in Java and XML basics prepares you for Android app creation.

Now, Let's discuss the basic components used for application development.

➢ The Application Components :


The major components associated with an Android application are activity, service,
content provider, and broadcast receiver.

1. Activity :
It is obvious that an application can have multiple activities which can be loosely coupled.
The main or the very first screen (as soon as you launch an application) of the application is known
as Main Activity. When you switch for the next activity (screen), the previous activity is halted and
stored in a stack called as back-stack. As stack is a data structure with the last-in-first-out property,
on pressing the Back button by user, the previous activity is activated. Consequently, the current
activity (from where the Back button was press demolished as it was popped out from back stack.
Whenever we introduce new screen in our Android Project, then two files are created one is XML
file and another one is java file.
The java code of the Activity is –
public class MainActivity extends AppCompactActivity {

}
For example – Image application can be started in an email application to compose an email that
shares an image. The picture below depicts how each new activity adds an item to back stack and
how the current activity is destroyed and previous activity is resumed.

2. Services :
An application component in the working process that does not use UI is called a service.
For example, a service that is playing music, providing contents, etc., from the background but
not appearing on the screen. A service is an application component that can perform long-
running operations in the background and does not provide a UI. Even on switching to another
application, the services of the previously activated apps not cannot be stopped.
The java code of the Services is –
public class My_Services extends Service {

}
For example - We can surf the internet or use any other application while listening to music.

3. Content Providers :
Accessing of contents from the central data warehouse is managed by a content provider. Being
a part of Android application, the content provider empowers its UI for working with the data.
The java code of the Services is –
public class My_Content_Provider extends Content Provider {
public void onCreate() {

}
}
For example - We can consider looking for contact details in contact list. Or we might want
photos from the gallery which are also provided by Content Provider.

4. Broadcast Receivers :
Many system-generated notifications, such as battery low, downloading starts, or file received,
are broadcasted by broadcast receivers. However, some applications can also stimulate
broadcast receivers. For example, an application informs the other application that data has
been downloaded for them to use. The broadcast receivers do not have a UI; however, they send
a notification to the status bar to alert the user. In essence, a broadcast receiver is a component
that alerts a user by sending a notification, which is intended to do a very little work (just notify).
It just acts as a gateway for other components.
The java code of the Broadcast Receivers is –

public class My_Receiver extends BroadCastReceiver {


public void onReceive ( content, intent ){

}
}

For example - Notify the user that the battery is low. Android developers can use broadcast
messages in the application or outside the normal flow.
• The manifest File :
1. The manifest file is the core of Android development.
2. It is an XML file and is so important to Android application that sometimes it is also known
as the heart of the Android application. It is named as AndroidManifest.xml file.
3. AndroidManifest.xml file contain information of various packages including component of
the application such as Activity, Services, Content Provider and Broadcast Receiver.
4. It performs another task also :
i. It is responsible to protect application to access any protected part providing
the permissions.
ii. It also declares the android API that the application is going to use.
iii. It lists the instrumentation classes. The instrumentation classes provide
profiling and other information. This information is removed just before the
application is published etc.
5. The elements used in the above XML file are described below.
<manifest>

manifest is the root element of the AndroidManifest.xml file. It has package attribute that
describes the package name of the activity class.

Application is the sub-element of the manifest. It includes the namespace declaration.


This element contains several sub-elements that declares the application component such
as activity etc.

6. The commonly used attributes are of this element are icon, label, theme etc.

a. android:icon represents the icon for all the android components.


b. android:label works as the default label for all the application components.
c. android:theme represents a common theme for all the android activities.
d. activity is the subelement of application and represents an activity that must be
defined in the AndroidManifest.xml file. It has many attributes such as label, name,
theme, launchMode, etc.
e. android:name represents a name for the activity class. It is required asstribute.
f. intent-filter is the sub-element of activity that describes the type of intent to which
activity, service or broadcast receiver can respond to. It adds an action for the intent-
filter. The intent-filter must have at least one action element.
<category>
It adds a category name to an intent-filter.
● Downloading and Installing Android :

For Android Applications Development we have to set the environment. This


comprises downloading, configuring, and installing of several software components
required to make the IDE complete and ready for developing purposes. The prerequisite
components to setup our Android IDE are as follows :

1. Java Development Kit or JDK since Android applications are developed in


Java

2. Android Software Development Kit (SDK)

3. Eclipse IDE

Setting up the environment for Android development involves configuring our


development environment with the necessary software tools and SDK components :

1. Install Java Development Kit or (JDK):


Android development requires JDK. Download and install the latest version of
JDK from the official Oracle website
(https://www.oracle.com/java/technologies/javase-jdk15-downloads.html).
And Follow the installation instructions.

2. Install Android Studio:

1. Download Android Studio:

Visit the official Android Studio website


(https://developer.android.com/studio) and click on the "Download"
button.

2. Run the Installer:

Once the download is complete, locate the downloaded file and double-
click on it to run the installer.
3. Choose Setup Type:

The installer will prompt us to choose the type of setup we want. We can
choose the Standard setup, which includes the most commonly used
features, or the Custom setup, which allows us to customize the
installation further.

4. Select Components:

In the next step, we’ll be asked to select the components we want to


install. Make sure the "Android Studio" and "Android Virtual Device"
options are checked.

5. Choose Installation Location:

Select the installation location for Android Studio. The default location is
usually fine, but we can choose a different location if we prefer.

6. Choose Start Menu Folder:

Specify the Start Menu folder where shortcuts for Android Studio will be
created.

7. Install:

Click on the "Install" button to begin the installation process. The installer
will now download and install the selected components.

8. Launch Android Studio:

Once the installation is complete, we can launch Android Studio by


clicking on the "Finish" button.

9. Set Up Android Studio:

On the first launch, Android Studio will prompt us to import settings from
a previous version (if any) and to customize the setup according to our
preferences.
10. Download SDK Components :

Android Studio will prompt us to download additional SDK components


and system images for the Android versions we want to develop for.
Follow the on-screen instructions to download and install these
components.

11. Create a New Project:

After completing the setup, we can create a new Android project or open
an existing one to start developing our app.

3. Configure Android SDK in Android Studio:

After installing Android Studio, launch it. Android Studio will prompt us to
download the necessary SDK components if they're not already installed. For this, we have
to follow the on-screen instructions to download and install the required SDK
components.

4. Set Up Android Virtual Device (AVD) :

Android Virtual Device (AVD) allows us to test our apps on virtual devices. Open
Android Studio, go to "Tools" > "AVD Manager," and create a new virtual device by
following the prompts. Choose a device definition and system image that we want to
emulate.

5. Set Environment Variables :

We might need to set up environment variables for Java and Android SDK if they
are not automatically detected. This step ensures that other tools can locate the
JDK and Android SDK. Here's how to set environment variables :

● JAVA_HOME: Set this variable to the installation directory of JDK. For


example, C:\Program Files\Java\jdk15.
● ANDROID_HOME: Set this variable to the installation directory of Android
SDK. For example, C:\Users\YourUsername\AppData\Local\Android\Sdk.
● Path: Add %JAVA_HOME%\bin and %ANDROID_HOME%\platform-tools to
our system's Path variable to ensure that commands like java, javac, adb,
etc., can be run from the command line.

6. Install Additional SDK Components :

Depending on our project requirements, we may need to install additional SDK


components. You can do this through the Android Studio SDK Manager
(accessible via "Tools" > "SDK Manager").

8. Update Android Studio and SDK:

Regularly update Android Studio and SDK components to access the latest
features and improvements. You can check for updates within Android Studio via
"Help" > "Check for Updates" and update SDK components through the SDK
Manager.

So, this is the environment setup we have to follow for Android Programming.

● Downloading and Installing the Android SDK Setting up Android


Virtual Device :

Setting up the Android SDK (Software Development Kit) and configuring an


Android Virtual Device (AVD) involves several steps. Below is a general guide on
how to do this:
1. Download Android Studio:
● Visit the official Android Developer website: Android Studio Download.
● Download the latest version of Android Studio for as per our operating
system (Windows, macOS, or Linux).

2. Install Android Studio:


● Run the downloaded installer and follow the installation instructions.
● Android Studio will also install the Android SDK along with it.
3. Open Android Studio:
● After installation, launch Android Studio.

4. Configure Android SDK:


● When we first open Android Studio, it may prompt us to install additional
components or updates for the Android SDK. Follow the prompts to install
them.
5. Set up Android Virtual Device (AVD):
● Go to "Tools" > "AVD Manager" or click on the AVD Manager icon in the
toolbar.
● In the AVD Manager, click on "Create Virtual Device."
● Choose the device definition we want to emulate (e.g., Pixel, Nexus).
● Select a system image for the virtual device. If we haven't downloaded any,
click on "Download" next to the system image we want to use.
● Configure the AVD settings such as RAM, Virtual Machine Memory, etc.,
according to our requirements.
● Click "Finish" to create the virtual device.

6. Start the Virtual Device:


● In the AVD Manager, select the virtual device we created.
● Click on the "Play" button (green triangle) to start the virtual device.

7. Wait for the Emulator to Start:


● The Android emulator may take some time to start up, especially the first
time.

8. Test our Virtual Device:


● Once the emulator is up and running, we can interact with it just like we
would with a physical Android device.

That's the process of Android Virtual Device with Android Studio ! We have now set up
the Android SDK and configured an Android Virtual Device. We can use this virtual
device for testing our Android applications without needing a physical device.
• Setting Up Android Physical Device :

Setting up an Android physical device for development with USB debugging involves a
few steps to enable developer options and USB debugging on the device itself, as well
as configuring our development environment to recognize the device. Here's a step-by-
step guide :

Steps that we have to follow :

1. Enable Developer Options:


● On your Android device, go to Settings.
● Scroll down and find "About phone" or "About device."
● Look for the "Build number" or “Version Number” entry.
● Tap on "Build number" repeatedly (usually 7 times) until we see a message
saying "You are now a developer!"

2. Enable USB Debugging:


● Now that we have enabled Developer Options, go back to the main
Settings menu.
● Find and open "Developer options" (it should be above or below "About
phone" or "About device").
● Scroll down and find "USB debugging."
● Enable "USB debugging" by toggling the switch.

3. Connect Your Device to Your Computer:


● Connect your Android device to your computer using a USB cable.
● Make sure the device is unlocked.

4. Authorize Your Computer:


● When you connect your device to your computer for the first time with
USB debugging enabled, you may see a prompt on your device asking if
you want to allow USB debugging from the connected computer.
● Check the box to "Always allow" USB debugging from this computer.
● Tap "OK" or "Allow".
5. Install Device Drivers (if needed):
● Depending on our computer's operating system, we may need to install
device drivers to ensure our computer can communicate with our Android
device properly. This is more common for Windows users.
● Our device manufacturer's website usually provides drivers if they are
needed.

6. Verify Device Connection:


● Open a terminal or command prompt on your computer.
● Enter the following command to verify that your device is recognized :

adb devices

● If our device is listed, you're all set! You can now use it for development
and debugging.

Note :

1. ADB (Android Debug Bridge) is a command-line tool that allows us to


communicate with our Android device from our computer. Make sure we have ADB
installed on our computer. It's typically included with the Android SDK, or we can install
it separately if needed.

2. If our device is not recognized after following these steps, try restarting both our
computer and our Android device, and make sure we have installed any necessary
drivers.

● Exploring the development Environment :

Eclipse, a popular Integrated Development Environment (IDE), provides support


for Android app development through the Android Development Tools (ADT) plugin. The
ADT plugin extends the functionality of Eclipse to include tools specifically for Android
development.
The Java Perspective Using Eclipse :

The Java perspective in Eclipse lets us access the associated Java libraries, which
are to be used with Android applications in Eclipse IDE. The sections of Java perspective
in Eclipse IDE are as follows :

1. Package Explorer :

● The Package Explorer provides a hierarchical view of the files and folders in our
Eclipse project.
● It organizes our project's resources, including Java source files, XML files, images,
and other assets, into a navigable tree structure.
● We can expand and collapse folders to navigate through our project's directory
structure, making it easy to locate and manage files.
● It allows us to perform various operations on project resources, such as creating,
deleting, renaming, copying, and moving files and folders.

2. Task List :

● The Task List displays a list of tasks, comments, and other markers that are
associated with our project's source code files.
● Tasks can include TODO comments and custom task tags that we define.
● It helps us keep track of pending work items, reminders, and areas of code that
need attention.
● We can navigate directly to the location of a task in our code by double-clicking
on it in the Task List.

3. Java Class Pane :

● The Java Class Pane, also known as the Editor area, is where we edit and view the
contents of Java class files.
● It provides a text editor with syntax highlighting, code completion, and other
features to assist us in writing Java code.
● We can open and switch between multiple Java class files in separate tabs within
the Java Class Pane.
● It offers tools for navigating through our code, such as the Outline view and
Quick Outline feature.

4. Outline:
● The Outline view displays a structured outline of the current Java class file being
edited in the Java Class Pane.
● It provides a high-level overview of the class's structure, including its methods,
fields, inner classes, and other members.
● We can quickly navigate to specific sections of the class by clicking on entries in
the Outline view.
● It allows us to collapse or expand sections of the class to focus on specific parts
of the code.

The Java perspective in Eclipse provides a set of essential tools and views for Java
development, including the Package Explorer for managing project resources, the Task
List for tracking tasks and markers, the Java Class Pane for editing and viewing Java class
files, and the Outline view for navigating class structures. These components work
together to support efficient and productive Java development in Eclipse.

● The DDMS Perspective :

DDMS stands for Dalvik Debug Monitor Server. It's a tool provided by the Android
SDK (Software Development Kit) that allows developers to debug Android applications
and analyze the behavior of applications running on Android devices or emulators. Below
are the main components of the DDMS perspective :

1. Devices :
● The Devices view lists all connected Android devices and running
emulators.
● It provides information about each device/emulator, including its serial
number, state, and other properties.
● We can select a device/emulator to view detailed information and perform
various debugging and monitoring tasks.
2. Emulator Control:
● The Emulator Control view allows us to interact with and control Android
emulators.
● We can simulate phone calls, SMS messages, and location updates, as well
as control the orientation and battery level of the emulator.
● This feature is particularly useful for testing and debugging applications
that rely on phone calls, messaging, or location services.

3. LogCat :
● The LogCat view displays real-time system log messages from connected
Android devices and emulators.
● It provides filtering options to focus on specific log messages based on
priority level, tag, or text.
● LogCat is essential for debugging and diagnosing issues in Android
applications, as it allows developers to monitor runtime behavior and trace
execution flow.

4. Threads:
● The Threads view provides information about the threads running in the
selected Android application.
● It displays the thread's name, state, priority, and stack trace, allowing
developers to analyze multithreaded behavior and identify potential issues
such as deadlocks or race conditions.

5. Heap:
● The Heap view offers insights into the memory usage of the selected
Android application.
● It shows a graphical representation of the application's heap memory,
including the size of allocated objects and the distribution of memory
across different object types.
● Developers can use the Heap view to identify memory leaks, optimize
memory usage, and troubleshoot performance issues related to memory
management.

6. Allocation Tracker :
● The Allocation Tracker view allows developers to monitor object
allocations and deallocations in the selected Android application.
● It provides detailed information about each allocation event, including the
type of object, allocation size, allocation stack trace, and the number of
references.
● Developers can use the Allocation Tracker to analyze memory usage
patterns, detect memory leaks, and optimize resource allocation in their
applications.

7. File Explorer :
● The File Explorer view enables browsing and managing files on the file
system of connected Android devices and emulators.
● Developers can navigate through the device's directory structure, upload
and download files, and perform file operations such as copy, paste,
delete, and rename.
● This feature is useful for inspecting application files, transferring data
between the device and the computer, and debugging file-related issues.

To enable the DDMS perspective in Eclipse, we can follow the steps : Select "Window"
→ "Open Perspective" → "DDMS" option, or select "Window" → "Open Perspective"
→ "Other" option → "DDMS" option. This will switch the Eclipse IDE to the DDMS
perspective, where we can access the mentioned tools and views for debugging and
monitoring Android applications.

● Developing and Exploring the First Android Application :

We have seen that whenever we learn new programming languages, the first
program always displays ‘hello world’ on the screen. The same trend continues with
Android here; yet, we will write a somewhat comprehensible "hello world" application. It
is very easy and practically no coding is required. However, to make this application
interesting, we will add some extra coding in it.

● Using Eclipse IDE to Create an Application :

The use of an IDE is recommended for Android application development. In this


section, we learn the basic steps to create an Android application using the Eclipse IDE.
We are using the Eclipse IDE, which is bundled with the Android SDK. In this IDE, we need
to use the New Android Application wizard to start creating an Android application. Let's
discuss the development of the first Android application, named “MyFirstApp”. The steps
to create a new project in Eclipse IDE are as follows :

1. Install Android Development Tools (ADT):


● Ensure that we have Eclipse IDE installed on our system.
● Install the Android Development Tools (ADT) plugin for Eclipse. We can do
this by following the instructions provided on the official Android
Developer website.

2. Set up Android SDK:


● Download and install the Android SDK if you have not already.
● Open Eclipse and configure it to use the Android SDK. We can do this by
navigating to Window > Preferences > Android and specifying the
location of the Android SDK on our system.

3. Create a New Android Project :

● In Eclipse, go to File > New > Android Application Project.


● Enter a project name (e.g., HelloWorld), choose a package name (e.g.,
com.example.helloworld), and select a minimum SDK version.
● Click "Next" and choose a blank activity template for the project.
● Click "Next" again and customize the activity settings if needed.
● Click "Finish" to create the project.

4. Navigate to the MainActivity.java file :

● Expand the project folder in the Package Explorer.


● Navigate to the src > com.example.helloworld directory (or the package
name you specified) and open the MainActivity.java file.

5. Edit MainActivity.java :
● Inside MainActivity.java, you'll see a method named onCreate. This method
is called when the activity is created.
● Within onCreate, locate the line setContentView(R.layout.activity_main);.
This line sets the layout for the activity.
● We'll edit the activity_main.xml layout file next to create the UI for our
"Hello World" message.

6. Create UI Layout (activity_main.xml):

● Navigate to the res > layout directory.


● Open the activity_main.xml file.
● Drag and drop a TextView widget onto the layout.
● Set the text property of the TextView to "Hello World" or any desired
message.
● Optionally, customize the appearance of the TextView (e.g., font size, color,
alignment).

7. Run the Application:

● Ensure that an Android Virtual Device (AVD) is set up or connect a physical


Android device to your computer.
● Right-click on the project in Eclipse and select Run As > Android
Application.
● Choose the AVD or device to run the application on.
● Click "OK" to launch the application.

8. View the Output:

● The emulator or connected device should display the "Hello World"


message on the screen.

By following these steps, we can create and run a "Hello World" application using
Eclipse IDE for Android development.
● Running Your Application :

The steps to run the "Hello World" application (MyFirstApp) on the Android Emulator
as described :

1. Open Eclipse IDE and navigate to any directory containing the MyFirstApp
project in the Package Explorer.
● Click the "Run" icon in the toolbar of Eclipse IDE.
● Alternatively, select the application and click on the "Run" option in the
menu bar. We can also choose "Run As" -> "Android Application."

2. The Android emulator (AVD) will appear as the next window. It might take some
time to boot up, and then the home screen will be displayed.

3. Click the Applications Icon on the home screen of the emulator. Look for the
‘MyFirstApp’ application icon.

4. Click on the icon of the ‘MyFirstApp’ application. The screen will display the
output, which should be "Hello world!"

By following these steps, we can create and run the "Hello World" application on an
Android emulator.

● Exploring the Application :

The purpose of exploring the application is to know how the “hello world” text
message has been displayed by the “MyFirstApp” application.

To explore the internal structure of the “MyFirstApp” application and understand how
the "Hello world" text message is displayed, we need to familiarize ourselves with some
important directories within the application.
1. AndroidManifest.xml :

The importance of the AndroidManifest.xml file and its components in the MyFirstApp
application, we have :

Manifest File Overview :


● The AndroidManifest.xml file is important as it manages all components of
the application.
● It serves as the heart of the Android application, providing various
functionalities and interactions.

Components of AndroidManifest.xml:
● <manifest> :
● It's the main element enclosing the entire structure of the manifest file
and acts as the root element.
● <uses-sdk>:
● This element indicates the compatibility of the application.
● It specifies the minimum and maximum version (API level) of the
Android application platform required to run the app.
● <application>:
● This element is a significant part of the manifest file.
● It contains declarations of various components and attributes related
to the application.
● <activity>:
● Declared inside the <application> element.
● Responsible for defining the visual UI of the application.
● Each activity represents a screen or a user interface component within
the application.

● <intent-filter>:
● This declaration is used for activities, services, and broadcast receivers to
specify how they respond to incoming messages called intents.
● It defines the criteria for an intent to match with the component.
● Activities, services, and broadcast receivers can have one or more intent
filters to specify the types of intents they can handle.
● <action> :
● This element is used within an <intent-filter> to specify the action(s) that
the component can handle.
● Actions describe the overall behavior or purpose of the intent.
● Examples of actions include "android.intent.action.VIEW" for viewing data
and "android.intent.action.SEND" for sending data.

● <category>:
● This element is also used within an <intent-filter> to specify additional
categorization for the intent.
● Categories provide more specific information about the intent's purpose or
behavior.
● Common categories include "android.intent.category.DEFAULT" for the
default category and "android.intent.category.LAUNCHER" for activities that
can be the entry point of the application.

By understanding and modifying the AndroidManifest.xml file, developers can control the
behavior and characteristics of their Android applications, including compatibility,
permissions, and declarations of various components like activities, services, broadcast
receivers, and content providers.

2. MainActivity.java :

The code and its functionality in the MainActivity.java file:

Package Declaration:

● The first line represents the package declaration for the MainActivity.java
file.
● The package name is com.kogent.android.myfirstapp, which identifies the
location of the file within the project's directory structure.

Import Statements:
● The next lines import necessary classes required for the application.
● For example, android.app.Activity and android.os.Bundle are imported to
utilize functionalities provided by the Android framework.

MainActivity Class Definition:


● The MainActivity class is defined, which extends the Activity class.
● By extending the Activity class, MainActivity inherits methods and behaviors
provided by the Android framework for managing activities.

onCreate() Method Override:


● The onCreate() method is overridden within the MainActivity class.
● This method is a crucial lifecycle method in Android activities and is called
when the activity is first created.
● Inside the overridden onCreate() method, super.onCreate(savedInstanceState) is
called to perform necessary initialization tasks provided by the superclass.
● The setContentView(R.layout.activity_main) method is called to set the layout
for the activity.
● R.layout.activity_main is a reference to the layout file named activity_main.xml,
which defines the UI layout for the MainActivity.
● This line indicates that the UI layout for the MainActivity is defined in the
activity_main.xml file located in the layout directory under the res folder of
the project.

In summary, the MainActivity.java file contains the main code for the MainActivity class,
which is responsible for defining the behavior and appearance of the main activity in the
MyFirstApp application. The onCreate() method is overridden to initialize the activity and
set its layout using the activity_main.xml file.

3. activity_main.xml :

The process of viewing and understanding the code in the activity_main.xml file:

Open activity_main.xml:
● Navigate to the res directory in your project.
● Inside the res directory, locate the layout subdirectory.
● Find the activity_main.xml file within the layout directory.

Open the File:


● Double-click on the activity_main.xml file to open it in the editor.
● By default, you'll likely see the graphical layout of the file, displaying the
visual representation of the UI components.

Switch to the Code Tab:


● Locate the tabs at the bottom of the file editor window.
● Find and click on the "Code" tab to switch from the graphical layout view
to the XML code view.

Review the Code:


● In the XML code view, you'll see the markup that defines the layout of the
activity.
● Look for attributes such as layout_width and layout_height, which determine
how the UI elements fill the screen.
● Identify the <TextView> element, which represents a text widget in the
application.
● The text attribute of the <TextView> specifies the text to be displayed, often
associated with a string resource defined in the strings.xml file.

In summary, by following these steps, you can access and examine the XML code of the
activity_main.xml file, which defines the layout of the main activity's user interface in the
MyFirstApp application. This file is crucial for understanding how the UI works and how
widgets are managed within the application.

4. strings.xml :

The process of accessing and understanding the strings.xml file :

Locate the strings.xml file:


● Expand the MyFirstApp project in the Package Explorer pane of Eclipse.
● Navigate to the res directory and then the values directory.
● Look for a file named strings.xml.
Open the strings.xml file:
● Double-click on the strings.xml file to open it in the editor.

Review the contents:


● In the strings.xml file, you will see several <string> elements, each with a
unique name attribute.
● The name attributes represent keys for string resources that can be
referenced in the application code or XML layout files.
● For example, you'll likely find three string attributes mentioned: app_name,
hello_world, and action_settings.

Understanding the strings:


● The app_name string is typically used as the label for the application in the
AndroidManifest.xml file.
● The hello_world string, as mentioned, is used for displaying "Hello world!"
and is likely used as the text for a TextView in the layout file.
● The action_settings string is commonly used as a default setting for a menu
option.

Modify strings as needed:


● We can modify the content of the strings in this file to change the text
displayed in the application.
● This allows for easy localization and customization of text throughout the
app.

In summary, the strings.xml file contains string resources used within the Android
application. It allows for easy management and customization of text displayed in the user
interface and other parts of the application. By accessing and modifying this file,
developers can control the text displayed in their Android application, including the "Hello
world!" message.
● The Command-Line Tools :

Command-line tools provide an alternative approach to developing Android


Applications compared to using Integrated development environments (IDEs) like Eclipse.
With command-line tools, developers have more flexibility and control over the
development process, and they can automate tasks more efficiently.

To utilize command-line tools for Android application development, we need to have the
Android SDK installed on our system. The Android SDK contains the necessary tools,
libraries, and APIs required for building Android applications. Additionally, we need to
have the applicable API levels installed, depending on the version of Android we are
targeting for our application.

By using command-line tools, developers can perform various tasks such as creating
projects, compiling code, managing dependencies, and running tests directly from the
terminal or Command Prompt, without relying on graphical user interfaces provided by
IDEs. This approach is preferred by some developers for its simplicity, speed, and
flexibility.
Let's first set the environment to work with command-line tools.

● Setting the Environment Variable :

The steps to set up the environment variable to access the SDK are as follows:

1. Open System Properties:


● Open Control Panel.
● Select "System" and then "Advanced system settings." This opens the
System Properties dialog box.

2. Access Environment Variables:


● In the System Properties dialog box, navigate to the "Advanced" tab.
● Click the "Environment Variables" button. This opens the Environment
Variables dialog box.

3. Edit Path Variable:


● In the Environment Variables dialog box, locate the "Path" variable under
the "User variables" section.
● Select the "Path" variable and click the "Edit" button. This opens the Edit
User Variable dialog box.

4. Add Android SDK Paths:


● In the Edit User Variable dialog box, append the location of the platform-
tools directory as well as the tools directory of the Android SDK to the
existing value of the Path variable.
● Separate the path addresses with a semicolon.

5. Confirm Changes:
● Click "OK" to save the changes and close the Edit User Variable dialog box.
● Click "OK" again to close the Environment Variables dialog box.
● Finally, click "OK" to close the System Properties dialog box.

These steps ensure that the environment variable is correctly configured to allow access
to the Android SDK tools and platform-tools from the command line. This configuration
is necessary for performing various development tasks using command-line tools for
Android development.

Creating a Project :

For creating a project using command-line tools with the Android SDK :

1. Open Command Prompt:


● Launch the Command Prompt on our system.

2. Navigate to Project Directory:


● Use the ‘cd’ command to navigate to the directory where we want to
create the Android project.
● For example:

cd path\to\desired\directory
3. List Available Android Platforms:
● Execute the command android list targets.
● This command displays the existing Android platforms along with their
target IDs.
● Note down the target ID corresponding to the platform we want to
develop for.

4. Create Android Project:


● Execute the command:

android create project --target <target-id> --name AndroidProject --path


<path-to-workspace>\FirstAndroidApp --activity MainActivity --package
com.kogent

● Replace <target-id> with the ID of the target platform noted earlier.


● Replace <path-to-workspace> with the desired workspace directory.
● This command creates a new Android project with the specified
parameters:
● --name : Name of the project.
● --path : Path where the project will be created.
● --activity : Main activity class name.
● --package : Package name for the project.

5. Execute Command:
● Press Enter after entering the command to execute it.

6. Project Creation Confirmation:


● Upon successful execution of the command, the Android project along
with its default configurations and artifacts will be created in the specified
directory.

Following these steps, our Android project will be set up and ready for development
using command-line tools. We can now proceed with further development tasks and
coding for our Android application.
● Using Command-Line :
Instructions for running an Android project using command-line tools:

1. Install Apache Ant :


● Download Apache Ant from http://ant.apache.org/ and extract it to our
preferred directory.
● Set the classpath to the \bin directory of Apache Ant in the environment
variables. For example, set the PATH variable to C:\Ant\apache-ant-
1.9.3\bin.
● This allows us to access the ant command from anywhere in the
command-line tool.

2. Run the Emulator :


● Navigate to the SDK's tools directory.
● Execute the command android avd to launch the Android Virtual Device
(AVD) Manager.
● Select the desired emulator from the AVD Manager or create a new one if
needed.
● Start the emulator by clicking the "Start" button or by executing emulator
avd <AVD_name> from the command line.

3. Build the Project:


● Open the Command Prompt and navigate to the directory of our Android
project (e.g., D:\AndroidWorkspace\FirstAndroidApp).
● Execute the command ant debug to build the project. This compiles the
project and its dependencies.

4. Install the APK:


● Navigate to the bin directory of your project.
● Execute the command adb install
D:\AndroidWorkspace\FirstAndroidApp\bin\AndroidProject-
debug.apk to install the debug APK on the emulator.

5. Launch the Application:


● Locate the MainActivity icon on the emulator and click on it to launch the
application.
● Verify that the application launches successfully and displays the expected
output.

6. Modify and Recompile (Optional):


● If we need to make changes to the application, navigate to the project's
res\layout directory and open the activity_main.xml file.
● Make the necessary changes (e.g., modify the text displayed).
● Recompile the project by executing ant debug followed by ant installed
at the root directory of our project.

These steps outline the process of building, installing, and running an Android project
using command-line tools and Apache Ant. This approach provides developers with a
streamlined method for managing and deploying Android applications without relying
on IDEs.

You might also like