Tuesday, April 10, 2012

Starting Android Tutorial Series

Hello Folks!! Apologies for being off from blogging for a while!

Well, the news is, soon i am gonna start an Android Tutorial Series on my blog where new Android developers can learn the android development step by step very easily. This series will help you to learn the android from scratch work to the application building.

We will start this series with the exercises on creating different mobile User Interfaces.

And for the experienced developers, I will keep posting my new research topics and implementation of android new features.

Please post your suggestions even if those are the silly ones. Really looking forward to yours comments, even i need great motivation to get this work done. :)


Wednesday, February 8, 2012

Creating Android Phone Tablet application in a single apk

Guys!! It was very difficult to believe that Android had introduced an entirely new API (Fragment API) for Tablet App Development. Coming from Activity background, it was really tough to digest this fragments concept while coding; even if it is very interesting and more generalized one. Soon, i got to hear that we can create a single apk file for both phone and Tablets by using Fragment API itself. On digging more into it, it became quite easy and interesting to implement.

Functional Concept

Basically, in such type of apps, we need to separate all components of our application, and then convert each component to fragments. For example- Dashboard Fragment for Dashboard screen, two fragments for List and Details. Then, as our need, we can combine them to fit on the screens of both phone and tablet based on the Space available and required functionality.

Technical Concept

The earlier concept (for mobile versions) was to have activity class for each screen having business logic written for the respective screen functionality. Here we will write the business logic once but only different main screen layouts for both phone and tablet screens. Now, we will have smaller components as Fragment classes & layouts and their respective business logic in them.
As we know, we cannot show fragment directly on screen as it needs to be in the form of Activity class.  So for phone, we will convert the fragment to FragmentActivity class and for tablet we will have separate layouts containing two or more fragments and their respective FragmentActivity classes.
Now, on deploying the same apk file to both phone and tablet, it should take respective layouts for both.

Sample Application

So here is what i have created a way or you can say a ‘BASE STRUCTURE’ for any Phone-Tablet Android Application.
Application Name: TestPhoneTablet
Deployment platforms: Phone and Tablet versions both, i.e. Android 2.x & 3.x
External Jar file: android-support-v4.jar (When we need to deploy our application on both phone and tablet, all Fragment and Fragment Activity classes in our project should extend from this jar).

Description: In the application for phone, a list of countries will be displayed on first screen; on clicking any of them, the country name will be displayed on the second screen.
On the other hand in case of Tablet, on a single screen, the list of countries will be displayed on left hand side and on clicking any of them; the country name will be displayed on right side.
Brief Description for every element (classes and xmls) in the above project:
·         com.deepak.test : This package is for all the classes which will be common elements for both phone and tablet build.
§  BaseActivity.java: This class extended by FragmentActivity class is the base class for our whole project. A base activity that defers common functionality across app activities to an ActivityHelper. This class shouldn't be used directly; instead, activities should inherit from BaseSinglePaneActivity or BaseMultiPaneActivity.
§  BaseMultiPaneActivity.java: This class extended by BaseActivity class and all Tablet activities will extend to this class. It can contain multiple panes, and has the ability to substitute fragments for activities when intents are fired using BaseActivity#openActivityOrFragment(android.content.Intent).
§  BaseSinglePaneActivity.java: This class extended by BaseActivity class and all Phone activities will extend to this class. It simply contains a single fragment. The intent used to invoke this activity is forwarded to the fragment as arguments during fragment instantiation.
§  Home Activity.java: This Activity class is the launcher activity for our application and is only class which directly extends to the BaseActivity class after BaseSinglePaneActivity and BaseMultiPaneActivity classes. It sets the layout activity_home.xml on screen which is defined for both phone and tablet in their respective layout folders.
§  List_Fragment.java: This is List Fragment class, containing a list of countries which is the first screen of our application.
§  DetailFragment.java: This is a Fragment class which shows the selected country name.
·         com.deepak.test.phone : The package for all phone activity classes.
§  DetailActivity.java: The FragmentActivity class on converting the DetailFragment class to an activity and used to show only on Phone.
·         com.deepak.test.tablet : The package for all tablet activity classes.
·         com.deepak.test.Utils : The package for utility/helper classes in our project.
§  ActivityHelper.java: A class that handles some common activity-related functionality in the app, such as setting up the action bar. This class provides functionality useful for both phones and tablets, and does not require any Android 3.0-specific features.
§  ActivityHelperHoneycomb.java: An extension of ActivityHelper that provides Android 3.0-specific functionality for Honeycomb tablets. It thus requires API level 11.
§  SimpleMenu.java: An implementation of Menu Interface to show the action bar and Options menu in tablet and phone respectively.
§  SimpleMenuItem.java: An implementation of MenuItem interface.
§  UIUtils.java: A utility class which can have many UI helper functions like- to know the version of device OS on which the app is deployed so that it could render respective activities and layouts.
·         res/layout: The layout files used for Fragments and Phone screens or we can say that common layout files for phone and tablets.
·         res/layout-xlarge-v11: the layout files for tablet screens.
If we want to make the same layout look different on both phone and tablet, we can put the same layout with same name and different design in both layout folders. On  deployment, we will automatically render to the respective folder. Similary for  drawable & drawable-xlarge-v11 and menu & menu-xlarge-v11 folders

App Screenshots:

Tablet Screen
Phone Screen:
Here is link for source code. Enjoy doing some creative stuff and meeting your requirements.
http://code.google.com/p/testphonetablet-androidapp/downloads/list

Folks!! This is all about the project structure and files for this particular requirement. Please provide your comments, ques and suggestions J

Wednesday, January 18, 2012

Android Application Performance Improvement (using DDMS/Traceview/MAT tool): Part-4

Guys!! Here is the most awaited post on my blog!! The Performance analysis and Improvement ways by using Eclipse MAT tool !!

The Application Performance improvement process is one of the real challenges in the mobile application development. Android provides various tools to analyze the performance of the application, like-DDMS, Traceview, Allocation Tracker. The memory analysis can be done through Eclipse Memory Analysis Tool which a very powerful tool. It provides various analysis, like-Histogram, Dominator Tree, Leak Suspects along with chart analysis.

This post covers the memory analysis of an application, i.e. Stock Watch Application (which is one of my in-house developments) for an example, by using Eclipse MAT (Memory Analyzer Tool) tool and the improvements observed in the application performance on post-optimization.

Eclipse Memory Analysis Tool (MAT)

The Eclipse Memory Analyzer is a fast, feature-rich Java heap analyzer that helps to identify memory leakage. The Eclipse Memory Analyzer (MAT) provides a large selection of features to help in analyzing a single snapshot of heap. MAT can be used to identify memory leaks using a single click
In Android, DDMS helps you creating the heap dump HPROF file of the application. A Java heap dump is an image of the complete Java object graph at a certain point in time. It includes all objects, Fields, Primitive types and object references.
The Eclipse MAT helps to visualize based on Java heap dumps the references to objects and provides tools to identify potential memory leaks.
Heap Dump - Shallow Heap, Retained Heap
HPROF heap dump is a snapshot of a Java heap at a particular instance in time. The garbage collector is triggered before the dump is written. It contains information about all objects, all classes, GCR, and all information about the remaining objects. Heap does not contain allocation information.
Shallow heap is the total amount of memory used by all instances.
Retained Heap is the total amount of memory kept alive by all instances, including other objects that they have references to.
Eclipse MAT Pre-Optimization Analysis
On running the application, select the application process from the devices tab. DDMS helps to create the heap dump HPROF file of the application by selecting the ‘Dump HPROF file’ Button as shown in the below image.
It will ask for the type of report to be created. Select Leak Suspects Report and click Finish.
If we're running ADT (which includes a plug-in version of DDMS) and have MAT installed in Eclipse as well, clicking the “dump HPROF” button will automatically do the conversion (using hprof-conv) and open the converted hprof file into Eclipse (which will be opened by MAT).
Analysis Overview
It will display the overview of application performance analysis report like-total memory size no. of objects and no. of classes in the application, a pie chart showing biggest objects by retained size the application.
Also, it shows the various action items which produce the various analysis reports based on the different parameters as following:
Histogram lists number of instances per class.
Top Consumers prints the most expensive objects grouped by class and by package.
Leak Suspects displays the leak suspects and a system overview.
Top Components list report for components bigger than 1 percent.
Dominator Tree lists the biggest objects what they keep alive.
Duplicate Classes detect classes loaded by multiple class loaders.

Histogram
Using the Histogram view, we can filter the object/classes based on the package names, and you can see the incoming and outgoing references.

If we sort by shallow heap, we can see that instances of char [] are at the top. Right-click on the char [] class and select List Objects > with incoming references. This produces a list of all char arrays in the heap, which we can sort based on Shallow Heap usage.
Pick some of the big objects, and drill down on them. This will show the path from the root set to the object -- the chain of references that keeps this object alive.
In the above image, we have found some of the objects are using large memory as comparison to the other application objects as marked by red arrows. The array objects temp_data4 and temp_dataf are having more allocated memory than the memory they are consuming. Also, in object of Input Stream Class is open.


Note: MAT can't tell us for sure that this is a leak, because it doesn't know whether these objects are needed or not -- only the programmer can do that.
Top Consumers
It will show us which object and classes are the top consumers of memory in the application by using pie charts as shown below.

In the above image, we can analyze the biggest objects.

The above image is showing biggest top level dominator classes. As we can analyze that a large amount of memory is used by java.lang.class and java.lang.string classes.
Leak Suspects
We can get a higher level of leak suspect report with a pie chart and list of leak suspects as shown in below image.

From the above Leak Suspects report, we can analyse which objects/classes can cause the memory leaks. In this case, it is showing objects java.lang.string class as one of the leak suspects. For deep analysis on the specific objects for this class, we can also make use of Dominator tree.
Dominator Tree
The dominator tree is a data structure that allows you to answer the question about the biggest objects in almost no time. The dominator tree can also be used to find high memory usage in certain areas, which is a much harder problem.
Using the "dominates" relationship we can create a dominator tree out of the graph of objects in memory. At each node of this tree we store the amount of memory that would be freed (= retained size).
In the below image of Dominator tree, we can see class of our application and memory used by the different objects under this class.

Here we have found a many string arrays having some particular length. But they are using the full memory as of their defined lengths. As shown in the image, a string array of defined length 200 is containing only 39 items (e.g. string array is using only 39 positions out of 200, leaving empty the remaining positions).

Areas for Optimization

From the above performance analysis, we can see various areas of optimization in our application which is following.
·         java.lang.Class – we need to reduce the number of objects of this class
·         java.lang.String- The number of objects is quiet high, some of string arrays have defined length but still not being used fully.
We have found 9 string arrays of average lengths 250 defined in the activity where 8 arrays are not being used fully and containing the duplicate objects.
·         Lazy Initialization- we have found that the high number of String array objects looked suspicious. Also they retain not that much memory. So instead of always using the default constructor it would most likely better to use lazy initialization and only initialize the field when it's first needed. 
·         java.io.inputStream objects- Some of the Inputstream objects are not closed due to which the probability of leaks is more.

Post-optimization - Performance Analysis


Eclipse MAT Overview Analysis
The total amount of memory consumed by the application has been reduced from 2.3 MB to 2.2 MB. Also the number of total objects has been reduced.

Histogram
The inputstream objects has been closed as we can’t see memory allocated to in object in the below image. The objects of array strings are consuming less memory and not having vacant spaces as marked by red arrow in the image.

Top Consumers
If we compare the below image chart with the earlier one (e.g. before optimization), we have found that objects of BuySellHold class are not being counted in the biggest objects (in the place of (f) in the image)

Leak Suspects
We have reduced the probability ratios of the leak suspects of both the classes java.lang.Class  and java.lang.String as shown in below images


Dominator Tree


The Percentage memory consumed by the native class com.myapp.stockwatcher.BuySellHold class has been reduced from 2.5% to 1.8%. Moreover, the number of string arrays has been reduced to 1.

Performance Improvements

Improving application performance in terms of time and memory is one of the real challenges in mobile development. The android native tools like-DDMS/Traceview and Eclipse MAT plays a significant role in improving the performance. Also these performance tests can be executed during the development life cycle.
Following are the performance improvements by using both the tools:
v  Using MAT:

·         Memory consumption by the two activities has been reduced from 2.3 MB to 2.2 MB

·         Reduction in Percentage probability of the leaks suspects

·         Improved the program performance by reducing the java.lang.String objects

·         Reduction in Duplicate String arrays

·         Stream objects closed on time

Although, Performance improvement is an ongoing process, there will always be the scope of optimization. But by using these tools we can significantly reduce the time taken in optimization process as these tools enable us to hit the culprit areas directly.

Guys!! This post covers the most powerful tool analysis to improve android app performance. Post your comments!! J

Monday, January 16, 2012

Android Application Performance Improvement (using DDMS-Traceview/MAT tool): Part-3

DDMS - Viewing heap usage for a Process/Application
DDMS allows you to view how much heap memory a process is using. This information is useful in tracking heap usage at a certain point of time during the execution of your application. Also, you can invoke garbage collection, which enables the collection of heap data.
In DDMS, Devices tab is on left side from where you can select the device and the process running for your application and then, you can see the various log reports under different tabs on the right side.

Open the application in Eclipse which you want to analysis the performance.
Select DDMS from the Eclipse

On running the application, you can analyse the heap memory usage as well. In DDMS, after the selecting application process, select Heap tab from the right side panel.

In the Devices tab, select the process that you want to see the heap information for. Click the Update Heap button to enable heap information for the process as shown in the below image.


You can see total Heap size, Allocated & Free memory, percentage usage and no of objects. Also, the distribution of heap memory size and count is available in different types of objects like, free, data object, class objects, 1-byte array, 2-byte array etc. 
In the Heap tab, click Cause GC to invoke garbage collection, which enables the collection of heap data.
When the operation completes, you will see a group of object types and the memory that has been allocated for each type. You can click Cause GC again to refresh the data.
Click on an object type in the list to see a bar graph that shows the number of objects allocated for a particular memory size in bytes.
In Bar graph of Allocation count per size, you can analyse which size of objects are maximum/minimum in the application.
The performance of your application can be improved in terms of heap allocations to the objects.
Note: The next part of this post will cover the analysis by using a powerful tool Eclipse MAT. Keep an eye!! J

Android Application Performance Improvement (using DDMS/Traceview/MAT tool): Part-2

DDMS - Memory Allocation Tracker

DDMS provides a feature to track objects that are being allocated to memory and to see which classes and threads are allocating the objects. Memory Allocation Tracker allows the following:
  • Memory Usage
  • Tracking of memory allocated to objects
  • Tracking of threads & classes that are allocating objects
In DDMS, Devices tab is on left side from where you can select the device and the process running for your application and then, you can see the various log reports under different tabs on the right side.
On running the application, you can analyse the memory usage as well. In DDMS, after the selecting application process, select Allocation Tracker tab from the right side panel.
Select Start Tracking Button to start the memory allocation tracking. You can navigate through the application/screens where you want to track the memory usage.
Click Get Allocations to see a list of objects that have been allocated since you clicked on the Start Tracking button. You can click on Get Allocations again to append to the list new objects that that have been allocated.


It will display Allocation order, size, class and thread for each object. On selecting any object, it will fetch the details of selected object like, Class, Method, File and Line number in the file. This allows you to track, in real time, where objects are being allocated when you perform certain actions in your application. This information is valuable for assessing memory usage that can affect application performance.
Hope now you can take advantage of MAT tool(provided by DDMS) to improve the performance of your application.
Note: However, The Eclipse has also provided a MAT tool which provides quite a strong features to improve the application memory usage. The later parts of this post will cover it. Keep an eye!! J

Saturday, December 17, 2011

Android Application Performance Improvement (using DDMS-Traceview/MAT tool): Part-1

The Application Performance improvement process is one of the real challenges in the mobile application development. Android provides various tools to analyze the performance of the application, like-DDMS, Traceview, Allocation Tracker. The memory analysis can be done through Eclipse Memory Analysis Tool which a very powerful tool. It provides various analysis, like-Histogram, Dominator Tree, Leak Suspects along with chart analysis.

This post covers the time analysis of an application, i.e. Stock Watch Application (which is one of my in-house developments) for an example, by using Traceview tool and the improvements observed in the application performance on post-optimization.

Traceview


Traceview is a graphical viewer for execution logs that you creates by using the Debug class to log tracing information in your code. Traceview can help you debug your application and profile its performance. It displays the log files data in two panels:
  • A timeline panel -- describes when each thread and method started and stopped
  • A profile panel -- provides a summary of what happened inside a method

Traceview Analysis on Pre-optimisation

Traceview helps us to find out the time spent in each method so that we could track the performance of each method we have written in the application.

1.       Start with Traceview profiling: Open Eclipse->DDMS->Devices(Tab), select the process of your application in the all the processes running on emulator/device. Now press ‘Start Method Profiling’ button as shown in below image.


2.       Application Navigation: Navigate to the application parts for which you want to create the profiling

3.       Stop Profiling: From the Devices Tab, select ‘Stop Method Profiling’ button as shown in below image. A profiling report will get generate automatically where you can start analysis.



As shown in the below image of Traceview report, the navigation time of these two activities is 51000 milliseconds*. The control was in the main Asynchronous thread for a large amount of time.

*since it is tested at android emulator; on device we might found the time difference.


Traceview Analysis on Post-optimisation

After optimizing the code, the navigation time has been reduced from 51000 milliseconds to 42000 milliseconds.


Performance Improvements
Improving application performance in terms of time and memory is one of the real challenges in mobile development. The android native tools like-DDMS/Traceview plays a significant role in improving the performance. Also these performance tests can be executed during the development life cycle.

Following are the performance improvements by using both the tools:

v  Using Traceview:
·         Navigation time reduced by 18%

·         Time spent by program control in Asyn Thread is lessened.

Hope now you can take advantage of Traceview tool to improve the performance of your application.

Note: The later parts of this post will cover the other tools analysis to improve android app performance. Keep an eye!! J