MAD Lecture # 04

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Mobile Application Development

Session: Spring 2021


Kaleem Nawaz Khan
Lecturer
Department of Computer Science

University of Engineering and Technology, Mardan


Today’s Agenda
ØThe development process
ØUsing Android Studio
ØExploring a project
ØViewing and editing Java code and Support library
ØViewing and editing layouts
ØUnderstanding the build process
ØSyncing your project
ØRunning the app on an emulator or a device
The Development Process
ØAn Android app project begins with an idea and a definition of the requirements
necessary to realize that idea.
ØAs the project progresses, it goes through design, development, and testing.
The Development Process
ØDefining the idea and its requirements: Most apps start with an idea of what it
should do, bolstered by market and user research. During this stage the app's
requirements are defined.
ØPrototyping the user interface: Use drawings, mock ups and prototypes to show
what the user interface would look like, and how it would work.
ØDeveloping and testing the app: An app consists of one or more activities. For each
activity you can use Android Studio to do the following, in no particular order:
ØCreate the layout: Place UI elements on the screen in a layout, and assign string
resources and menu items, using the extensible markup language (XML).
The Development Process
ØWrite the Java code: Create source code for components and tests, and use
testing and debugging tools.
ØRegister the activity: Declare the activity in the manifest file.
ØDefine the build: Use the default build configuration or create custom builds for
different versions of your app.
ØPublishing the app: Assemble the final APK (package file) and distribute it
through channels such as the Google Play
Using Android Studio
ØAndroid Studio provides tools for the testing, and publishing phases of the
development process, and a unified development environment for creating apps for
all Android devices.
ØThe development environment includes code templates with sample code for
common app features, extensive testing tools and frameworks, and a flexible build
system.
Using Android Studio
Starting an Android Studio project
ØAfter the successful installation of the Android Studio IDE, double-click the
Android Studio application icon to start it.
ØChoose Start a new Android Studio project in the welcome window, and name the
project the same name that you want to use for the app.
ØWhen choosing a unique Company Domain, keep in mind that apps published to
the Google Play must have a unique package name.
ØIf you are not planning to publish the app, you can accept the default example
domain. Be aware that changing the package name later is extra work
Using Android Studio
Choosing target devices and the minimum SDK
ØWhen choosing Target Android Devices, Phone and Tablet are selected by default,
ØThe choice of the Minimum SDK, e.g. API 15: Android 4.0.3 (IceCreamSandwich)
— makes your app compatible with 97% of Android devices active on the Google
Play Store
ØDifferent devices run different versions of the Android system, such as Android
4.0.3 or Android 4.4. Each successive version often adds new APIs not available in
the previous version.
ØTo indicate which set of APIs are available, each version specifies an API level. For
instance, Android 1.0 is API level 1 and Android 4.0.3 is API level 15.
Using Android Studio
Choosing a template
ØAndroid Studio pre-populates your project with minimal code for an activity and
a screen layout based on a template.
ØA variety of templates are available, ranging from a virtually blank template (Add
No Activity) to various types of activities.
ØYou can customize the activity after choosing your template. For example, the
Empty Activity template provides a single activity accompanied by a single layout
resource for the screen.
ØYou can choose to accept the commonly used name for the activity (such as
MainActivity) or change the name on the Customize the Activity screen.
Using Android Studio
Android Studio window panes
ØThe Android Studio main window is made up of several logical areas, or panes, as
shown in the figure below.
Using Android Studio
ØThe Toolbar: The toolbar carries out a wide range of actions, including running the
Android app and launching Android tools.
ØThe Navigation Bar: The navigation bar allows navigation through the project and
open files for editing.
ØThe Editor Pane: This pane shows the contents of a selected file in the project.
ØThe Status Bar: The status bar displays the status of the project and Android Studio
itself, as well as any warnings or messages. You can watch the build progress in the
status bar.
ØThe Project Pane: The project pane shows the project files and project hierarchy.
ØThe Monitor Pane. The monitor pane offers access to the TODO list for managing
tasks.
Exploring a project
ØEach project in Android Studio contains the
AndroidManifest.xml file, component source-
code files, and associated resource files.
ØBy default, Android Studio organizes your
project files based on the file type, and displays
them within the Project: Android view in the left
tool pane.
ØTo switch back to this view from another view,
click the vertical Project tab in the far left
column of the Project pane, and choose Android
from the pop-up menu at the top of the Project
pane.
Exploring a project
ØThe AndroidManifest.xml file: Used for specifying information about the app for
the Android runtime environment.
ØThe java folder: This folder includes activities, tests, and other components in Java
source code.
ØThe res folder: This folder holds resources, such as XML layouts, UI strings, and
images.
ØThe build.gradle (Module: App) file: This file specifies the module's build
configuration and other related information
Exploring a project
Important Android Manifest tags
ØAndroid namespace and application tag
ØAutomatic backup
ØThe app icon
ØApp label and string resources
ØThe app theme
Viewing and editing Java code support library
ØEach activity in an app is implemented as a Java class.
ØThe import statements import libraries needed for the app, such as the following,
which imports the AppCompatActivity library (import android.support.v7.app.AppCompatActivity;)
ØEach class declaration extends the AppCompatActivity class to implement features in
a way that is backward-compatible with previous versions of Android.
ØBefore the Android system can start an app component such as an activity, the system
must know that the activity exists by reading the app's AndroidManifest.xml file.
Therefore, each activity must be listed in the AndroidManifest.xml file.
Viewing and editing layouts
Ø Layout resources are written in XML and
listed within the layout folder in the res folder
in the Project: Android view.
Ø Click res > layout and then double-click
activity_main.xml to see the layout file in the
editing pane.
Ø Android Studio shows the Design view of the
layout, as shown in the figure below. This
view provides a Palette pane of user interface
elements, and a grid showing the screen
layout.
Understanding the build process
Ø The build process involves many tools and
processes that convert your project into an
Android Application Package (APK).
Ø The build process is very flexible, so it's
useful to understand some of what is
happening under the hood.
Understanding the build process
Ø The Android application package (APK) is the package file format for
distributing and installing Android mobile apps.
Ø The build process involves tools and processes that automatically convert
each project into an APK.
Ø Android Studio uses Gradle as the foundation of the build system, with more
Android-specific capabilities provided by the Android Plugin for Gradle.
Ø This build system runs as an integrated tool from the Android Studio menu.
Understanding the build process
Ø When you create a project, Android Studio automatically generates the
necessary build files in the Gradle Scripts folder in Project: Android view.
Ø Android Studio build files are named build.gradle as shown below:
Ø Each project has the following
Ø build.gradle (Project: apptitle)
Ø build.gradle (Module: app)
Understanding the build process
Ø build.gradle (Project: apptitle)
Ø This is the top-level build file for the entire project, located in the root project
directory, which defines build configurations that apply to all modules in your
project.
Ø This file, generated by Android Studio, should not be edited to include app
dependencies.
Ø build.gradle (Module: app)
Ø Android Studio creates separate build.gradle (Module: app) files for each module.
Ø You can edit the build settings to provide custom packaging options for each
module, such as additional build types and product flavors, and to override settings
in the manifest or top-level build.gradle file.
Ø This file is most often the file to edit when changing app-level configurations, such
as declaring dependencies in the dependencies section.
Understanding the build process
Ø The first statement applies the Android-specific
Gradle plug-in build tasks
Ø It tells the gradle the type of project and
indicate which plugin should be used.
Ø CompileSdkVersion specifies the Android API
level Gradle should use to compile the app.
This means your app can use the API features
included in this API level and lower.
Ø BuildToolsVersion specifies the version of the
SDK build tools. This is optional because the
plugin uses a recommended version of the
build tools by default.
Understanding the build process
Ø The defaultConfig block encapsulates default settings and entries for all build
variants, and can override some attributes in main/AndroidManifest.xml
dynamically from the build system.
Ø applicationId uniquely identifies the package for publishing
Ø Defines the minimum API level required to run the app.
Ø Specifies the API level used to test the app.
Ø Defines the version number of your app.
Ø Defines a user-friendly version name for your app (visible to users)
Ø The buildTypes block is where you can configure multiple build types.
Ø By default, the build system defines two build types: debug and release.
Ø By default, Android Studio configures the release build type to enable code
Ø shrinking, using minifyEnabled, and specifies the default Proguard rules file.
Understanding the build process
Ø The dependencies block in the module-level build configuration file specifies
dependencies required to build only the module itself.
Ø Android Studio makes it easy to include external binaries or other library
modules to your build as dependencies.
Ø The dependencies can be located on your machine or in a remote repository,
Ø To add a dependency to your project, specify a dependency configuration such
as implementation in the dependencies block of your build.gradle file.
Ø For example, the following build.gradle file for an app module includes three
different types of dependencies:
Ø Compile configuration was deprecated and
should be replaced by implementation or api.
Syncing your project
Ø When you make changes to the build configuration files in a project, Android
Studio requires that you sync the project files so that it can import the build
configuration changes and run some checks to make sure the configuration
won't create build errors.
Ø To sync the project files, click Sync Now in the notification bar that appears
when making a change, or click Sync Project from the menu bar.
Ø If Android Studio notices any errors with the configuration — for example, if
the source code uses API features that are only available in an API level higher
than the CompileSdkVersion — the Messages window appears to describe the
issue.
Running the app on an emulator or a device
Ø With virtual device emulators, you can test an app on different devices such as
tablets or smartphones with different API levels for different Android versions
to make sure it looks good and works for most users.
Ø Although it's a good idea, you don't have to depend on having a physical device
available for app development.
Ø The Android Virtual Device (AVD) manager creates a virtual device or emulator
that simulates the configuration for a particular type of Android device.
Ø Use the AVD Manager to define the hardware characteristics of a device and its
API level, and to save it as a virtual device configuration.
Ø When you start the Android emulator, it reads a specified configuration and
creates an emulated device on your computer that behaves exactly like a
physical version of that device.
Running the app on an emulator or a device

You might also like