Rabu, 18 September 2013

Xathrya Sabertooth

Xathrya Sabertooth


Introduction to Android and Android Application Development

Posted: 17 Sep 2013 05:16 PM PDT

This article will give a short summary of what should be known when developing Android application.

Table of Contents

  1. Android Insight
  2. Architecture
  3. Security and Permissions
  4. Conventions and API Level
  5. Development Tools
  6. Android Developer Tools
  7. Android Application Component
  8. Resources and Important Files

Android Insight

The Android Operating System

Android is a operating system based on Linux Operating System. The project is lead by Google with a consortium of Open Handset Alliance.

Nowadays, Android is used as some of smartphones and tablets operating system. Moreover, there is a project to port Android to x86 (a common PC). Android is a major player in smartphone OS beside Windows Phone, iOS, etc.

Android system supports background processing, provides a rich user interface library, supports 2-D and 3-D graphics using OpenGL libraries, access to the file system and embedded SQLite database.

Platform Component

Android system is a full software stack which is typically defined into the following four areas:

  1. Applications – The Android Open Source project contains several default application, like the Browser, Camera, Gallery, Music, Phone and more.
  2. Application framework – API which allow for high-level interaction with the Android system from Android applications.
  3. Libraries and runtime – Libraries for the Application Framework for many functions (graphic rendering, data storage, web browsing, etc.) and the Dalvik runtime and the core Java libraries for running Android applications.
  4. Linux kernel – Communication layer for the underlying hardware.

androidsoftwarelayer10

How to Develop Android Applications

Android applications are primarily written in the Java programming language.

During development the developer creates the Android specific configuration files and writes the application logic in the Java programming language. The Java source files are converted to Java class files by the Java compiler.

The Android SDK contains a tool called dx which converts Java class files into a .dex (Dalvik Executable) file. All class files of one application are placed in one compressed .dex file. During this conversion process redundant information in the class files are optimized in the .dex file. For example if the same String is found in different class files, the .dex file contains only one reference of this String.

These .dex files are therefore much smaller in size than the corresponding class files.

The .dex file and the resources of an Android project, e.g. the images and XML files, are packed into an .apk (Android Package) file. The program aapt (Android Asset Packaging Tool) performs this packaging.

The resulting .apk file contains all necessary data to run the Android application and can be deployed to an Android device via the adb tool.

The ADT and Android Studio tools perform these steps transparently to the user, i.e. if the user selects that the application should be deployed, the whole Android application (.apk file) is created, deployed and started.

Architecture

The architecture discussed in this section is the architecture of Android. Android can be referred as software stack of different layer, where each layer is a group of several program components. These levels define abstraction on device and provide same API for application on above.

Android-architecture

Linux Kernel

The kernel space. This area is controlled directly by Linux kernel and the basic layer of Android OS.

The whole Android OS is built on top of the Linux 2.6 Kernel with some further architectural changes made by Google.  It is this Linux that interacts with the hardware and contains all the essential hardware drivers. Drivers are programs that control and communicate with the hardware. For example, consider the Bluetooth function. All devices has a Bluetooth hardware in it. Therefore the kernel must include a Bluetooth driver to communicate with the Bluetooth hardware.  The Linux kernel also  acts as an abstraction layer between the hardware and other software layers. Android uses the Linux for all its core functionality such as Memory management, process management, networking, security settings etc. As the Android is built on a most popular and proven foundation, it made the porting of Android to variety of hardware, a relatively painless task.

Libraries

Android Native Libraries layer is the layer that enables the device to handle different types of data. This libraries are written in C or C++ language and are specific for a particular hardware.

Some important libraries:

Surface Manager: It is used for compositing window manager with off-screen buffering. Off-screen buffering means you cant directly draw into the screen, but your drawings go to the off-screen buffer. There it is combined with other drawings and form the final screen the user will see. This off screen buffer is the reason behind the transparency of windows.

Media framework: Media framework provides different media codecs allowing the recording and playback of different media formats

SQLite: SQLite is the database engine used in android for data storage purposes

WebKit: It is the browser engine used to display HTML content

OpenGL: Used to render 2D or 3D graphics content to the screen

Runtime

Consists of Dalvik Virtual Machine and Core Java Libraries.

Android use a special virtual machine, i.e. the Dalvik Virtual Machine to run java based application. It is similar to standard Java Virtual Machine (JVM) however using stripped down and optimized one. The bytecode between Dalvik is also different from Java bytecode.

Thus, you cannot run Java class files on Android directly. They need to get converted to Dalvik bytecode format.

These are different from Java SE and Java ME libraries. However these libraries provides most of the functionalities defined in the Java SE libraries.

Application Framework

These are the blocks that our applications directly interacts with. These programs manage the basic functions of phone like resource management, voice call management etc. As a developer, you just consider these are some basic tools with which we are building our applications.

Important blocks of Application framework are:

Activity Manager: Manages the activity life cycle of applications

Content Providers: Manage the data sharing between applications

Telephony Manager: Manages all voice calls. We use telephony manager if we want to access voice calls in our application.

Location Manager: Location management, using GPS or cell tower

Resource Manager: Manage the various types of resources we use in our Application

Application

The top layer, also referred as user space. In this layer, our application will reside and run.

Security and Permissions

Security Concept

The Android system installs every Android application with a unique user and group ID. Each application file is private to this generated user, e.g. other applications cannot access these files. In addition each Android application is started in its own process.

Therefore, by means of the underlying Linux operating system, every Android application is isolated from other running applications.

If data should be shared, the application must do this explicitly, e.g. via a service or a content provider.

Permission Concept

Android contains a permission system and predefines permissions for certain tasks. Every application can request required permissions and also define new permissions. For example an application may declare that it requires access to the Internet.

Permissions have different levels. Some permissions are automatically granted by the Android system, some are automatically rejected. In most cases the requested permissions are presented to the user before the installation of the application. The user needs to decide if these permissions are given to the application.

If the user denies a required permission, the related application cannot be installed. The check of the permission is only performed during installation, permissions cannot be denied or granted after the installation.

An Android application declares its required permissions in its AndroidManifest.xml configuration file. It can also define additional permissions which it can use to restrict access to certain components.

Conventions and API Level

API Version

API Level is an integer value that uniquely identifies the framework API revision offered by a version of 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.

Updates to the framework API are designed so that the new API remains compatible with earlier versions of the API. That is, most changes in the API are additive and introduce new or replacement functionality. As parts of the API are upgraded, the older replaced parts are deprecated but are not removed, so that existing applications can still use them. In a very small number of cases, parts of the API may be modified or removed, although typically such changes are only needed to ensure API robustness and application or system security. All other API parts from earlier revisions are carried forward without modification.

Android Project and Package Name

The base package for the projects is always the same as the project name, e.g. if you are asked to create a project called com.vogella.android, then the corresponding package name is com.vogella.android.

The application name, which must be entered on the Android project generation wizard, is not always predefined. In this case choose a name you like.

Development Tools

Android SDK

Software Development Kit (SDK) is a collection of tools and source code necessary to create, compile, and package a certain application. Thus, Android SDK is a SDK to create, compile, and package an application for Android. Most of these tools are command line. The primary language used at SDK is Java programming language.

The Android SDK contains Android debug bridge (adb), a tool which allows us to connect to a virtual or even real android. Using this, we can manage the device and debugging application.

Android NDK

Native Development Kit (NDK) or Android NDK, is similar to Android SDK. Android NDK is targeted to expert in C\C++ who want to explore more into lower level.

The primary language used at NDK is C\C++ programming language.

Android Developer Tools and Android Studio

For developing an Android application, Google has provided graphical development environment. There are two flaovrs you can choose: Eclipse and IntelliJ IDE. Using Eclipse IDE, it means we should install a plugin so the Android developing process can be done. This is called Android Development Tool (ADT). The counterparts using IntelliJ IDE and has been tweaked to fulfill the duty. It’s called Android  Stutio.

The Android Developer Tools (ADT) are based on the Eclipse IDE and provide additional functionalities to develop Android applications. ADT is a set of components (plug-ins) which extend the Eclipse IDE with Android development capabilities.

Both tools contain all required functionalities to create, compile, debug and deploy Android applications from the IDE. They also allow the developer to create and start Android virtual devices for testing.

Both tools provide specialized editors for Android specific files. Most of Android configuration files are based on XML. In this case these editors allow you to switch between the XML representation of the file and a structured user interface for entering the data.

Android Developer Tools

Eclipse provides a perspective for interacting with Android Virtual Device and your Android application program. This is included on everytime you install plugin for Eclipse.

DDMS – Dalvik Debug Monitor Service

Select Window → Open Perspective → Other… → DDMS to open this perspective. It groups several Eclipse views which can also be used independently.

On the left side it shows you the connected Android devices and the running processes on the device. The right side is a stack of views with different purposes. You can select processes and trigger actions from the toolbar, e.g. start a trace or stop the process.

Emulator Control

Simulate phone calls and SMS on the AVD. It also allows the application to set the current geo position.

File Explorer

Browse the filesystem on Android Virtual Device.

Android Application Components

Android applications can consists out of four components:

  • Activities
  • Services
  • Broadcast receivers
  • Content provider

Activities

An activity represents the visual representation of an Android application. An Android application can have several activities.

Activities use views and fragments to create the user interface and to interact with the user. Both elements are described in the next sections.

Services

A service performs tasks without providing an user interface. They can communicate with other Android components for example via broadcast receivers and notify the user via the notification framework in Android.

Broadcast Receivers

A broadcast receiver (receiver) can be registered to receive system messages and intents. A receiver gets notified by the Android system, if the specified event occurs.

For example you can register a receiver for the event that the Android system finished the boot process. Or you can register for the event that the state of the phone changes, e.g. someone is calling.

Content Provider

A content provider (provider) provides a structured interface to application data. A provider can be used for accessing data within one application but can also be used to share data with other applications.

Android contains an SQLite database which is frequently used in conjunction with a content provider. The SQLite database would store the data, which would be accessed via the provider.

Resources and Important Files

Android Manifest File

The components and settings of an Android application are described in the AndroidManifest.xml file. This file is known as the Android manifest file.

All activities, services and content providers components of the application must be statically declared in this file. Broadcast receiver can be defined statically in the manifest file or dynamically at runtime in the application.

The Android manifest file must also contain the required permissions for the application. For example if the application requires network access it must be specified here.

Following is the example of Android Manifest File:

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="de.vogella.android.temperature"        android:versionCode="1"        android:versionName="1.0">      <application android:icon="@drawable/icon" android:label="@string/app_name">          <activity android:name=".Convert"                    android:label="@string/app_name">              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>        </application>      <uses-sdk android:minSdkVersion="9" />    </manifest>

The package attribute defines the base package for the Java objects referred to in this file. If a Java object lies within a different package, it must be declared with the full qualified package name.

Google Play requires that every Android application uses its own unique package. Therefore it is a good habit to use your reverse domain name as package name. This will avoid collisions with other Android applications.

android:versionName and android:versionCode specify the version of your application. versionName is what the user sees and can be any String.

versionCode must be an integer. The Android Market determine based on the versionCode, if it should perform an update of the applications for the existing installations. You typically start with “1″ and increase this value by one, if you roll-out a new version of your application.

The <activity> tag defines an activity component. The name attribute points to class, which (if not fully qualified), is relative to the package defined in the package attribute.

The intent filter part in the Android manifest file, tells the Android runtime that this activity should to registered as possible entry point into the application and made available in the launcher of the Android system. The action define that is can be started android:name="android.intent.action.MAIN" ) and the category android:name="android.intent.category.LAUNCHER" category tells the Android system to add the activity to the launcher.

The @string/app_name value refers to resource files which contain the actual value of the application name. The usage of resource file makes it easy to provide different resources, e.g. strings, colors, icons, for different devices and makes it easy to translate applications.

The uses-sdk part of the AndroidManifest.xml file defines the minimal SDK version for which your application is valid. This will prevent your application being installed on unsupported devices.

Resources Files

Android supports that resources, like images and certain XML configuration files, can be keep separate from the source code.

Resource files must be placed in the /res directory in a predefined sub-folder dependent on their type. You can also append additional qualifiers to the folder name to indicate that the related resources should be used for special configurations. For example you can specify that layout file is only valid for a certain screen size.

Resource IDs and R.java

Every resource file gets an ID assigned by the Android build system. The gen directory in an Android project contains the R.java references file which contains these generated values. These references are static integer values.

If you add a new resource file, the corresponding reference is automatically created in R.java file. Manual changes in the R.java file are not necessary and are overridden by the tooling.

The Android system provides methods to access the corresponding resource files via these IDs.

For example to access a String with the R.string.yourString ID in your source code, you would use the getString(R.string.yourString)) method.

Deployment

In general, you can use several method to deploy an application to your device. Those are by USB transfer, email the application, use Google Play, etc. The following sections will highlights the most common ones and common deployment process.

There is also a specific article discussing this material. You can check it on this link.

Deployment via Eclipse

Turn on USB Debugging on your device in the settings. Select in the settings of your device Applications → Development, then enable USB debugging.

You may also need to install the a driver for your mobile phone. Linux and Mac OS usually work out of the box while an Windows OS typically requires the installation of a driver.

Please note that the Android version you are developing for must be the installed version on your phone.

If you have only one device connected and no emulator running, the Android develoment tools will automatically deploy to this device. If you have several connected you can selected which one shoudl be used.

Export Application

Android application must be signed before they can get installed on an Android device. During development Eclipse signs your application automatically with a debug key.

If you want to install your application without the Eclipse IDE you can right-click on it and select Android Tools → Export Signed Application Package.

This wizard allows to use an existing key or to create a new one.

Please note that you need to use the same signature key in Google Play (Google Market) to update your application. If you loose the key you will NOT be able to update your application ever again.

Make sure to backup your key.

Via External Source

Android allow to install applications also directly. Just click on a link which points to an .apk file, e.g. in an email attachment or on a webpage. Android will prompt you if you want to install this application.

This requires a setting on the Android device which allows the installation of non-market application. Typically this setting can be found under the “Security” settings.

Google Play

Google Play requires a one time fee, currently 25 Dollar. After that the developer can directly upload his application and the required icons, under Google Play Publishing .

Google performs some automatic scanning of applications, but no approval process is in place. All application, which do not contain malware, will be published. Usually a few minutes after upload, the application is available.

Tidak ada komentar:

Posting Komentar