Dr.
Babasaheb Ambedkar Technological University Testing Android Applications
UNIT 4) Testing Android Applications
Publishing Android Applications
Android application publishing is a process that makes your Android applications available to
users. Infect, publishing is the last phase of the Android application development process.
Android development life cycle
Once you developed and fully tested your Android Application, you can start selling or
distributing free using Google Play (A famous Android marketplace). You can also release
your applications by sending them directly to users or by letting users download them from
your own website.
You can check a detailed publishing process at Android official website, but this tutorial will
take you through simple steps to launch your application on Google Play. Here is a simplified
check list which will help you in launching your Android application −
Step Activity
Regression Testing Before you publish your application, you need to make sure
that its meeting the basic quality expectations for all Android apps, on all of the
1
devices that you are targeting. So perform all the required testing on different
devices including phone and tablets.
2 Application Rating When you will publish your application at Google Play,
you will have to specify a content rating for your app, which informs Google
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
Play users of its maturity level. Currently available ratings are (a) Everyone (b)
Low maturity (c) Medium maturity (d) High maturity.
Targeted Regions Google Play lets you control what countries and territories
where your application will be sold. Accordingly you must take care of setting
3
up time zone, localization or any other specific requirement as per the targeted
region.
Application Size Currently, the maximum size for an APK published on Google
Play is 50 MB. If your app exceeds that size, or if you want to offer a secondary
4 download, you can use APK Expansion Files, which Google Play will host for
free on its server infrastructure and automatically handle the download to
devices.
SDK and Screen Compatibility It is important to make sure that your app is
5 designed to run properly on the Android platform versions and device screen
sizes that you want to target.
Application Pricing Deciding whether you app will be free or paid is important
6 because, on Google Play, free app's must remain free. If you want to sell your
application then you will have to specify its price in different currencies.
Promotional Content It is a good marketing practice to supply a variety of
high-quality graphic assets to showcase your app or brand. After you publish,
7
these appear on your product details page, in store listings and search results,
and elsewhere.
Build and Upload release-ready APK The release-ready APK is what you you
will upload to the Developer Console and distribute to users. You can check
8
complete detail on how to create a release-ready version of your app: Preparing
for Release.
Finalize Application Detail Google Play gives you a variety of ways to
promote your app and engage with users on your product details page, from
9 colourful graphics, screen shots, and videos to localized descriptions, release
details, and links to your other apps. So you can decorate your application page
and provide as much as clear crisp detail you can provide.
Export Android Application Process
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
Apk development process
Before exporting the apps, you must some of tools
• Dx tools(Dalvik executable tools ): It going to convert .class file to .dex file. it has
useful for memory optimization and reduce the boot-up speed time
• AAPT(Android assistance packaging tool):it has useful to convert .Dex file [Link]
• APK(Android packaging kit): The final stage of deployment process is called as .apk.
You will need to export your application as an APK (Android Package) file before you
upload it Google Play marketplace.
To export an application, just open that application project in Android studio and select Build
→ Generate Signed APK from your Android studio and follow the simple steps to export
your application −
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
Next select, Generate Signed APK option as shown in the above screen shot and then click
it so that you get following screen where you will choose Create new keystore to store your
application.
Enter your key store path,key store password,key alias and key password to protect your
application and click on Next button once again. It will display following screen to let you
create an application −
Once you filled up all the information,like app destination,build type and flavours
click finish button While creating an application it will show as below
Finally, it will generate your Android Application as APK formate File which will be
uploaded at Google Play marketplace.
Google Play Registration
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
The most important step is to register with Google Play using Google Play Marketplace. You
can use your existing google ID if you have any otherwise you can create a new Google ID
and then register with the marketplace. You will have following screen to accept terms and
condition.
You can use Continue to payment button to proceed to make a payment of $25 as a
registration fee and finally to complete your account detail.
Once you are a registered user at Google Play, you can upload release-ready APK for your
application and finally you will complete application detail using application detail page as
mentioned in step 9 of the above mentioned checklist.
Shared Preferences in Android with Example
One of the most Interesting Data Storage options Android provides its users is Shared
Preferences. Shared Preferences is the way in which one can store and retrieve small
amounts of primitive data as key/value pairs to a file on the device storage such as String, int,
float, Boolean that make up your preferences in an XML file inside the app on the device
storage. Shared Preferences can be thought of as a dictionary or a key/value pair. For
example, you might have a key being “username” and for the value, you might store the
user’s username. And then you could retrieve that by its key (here username). You can have a
simple shared preference API that you can use to store preferences and pull them back as and
when needed. The shared Preferences class provides APIs for reading, writing, and managing
this data. A sample GIF is given below to get an idea about what we are going to do in this
article. The code for that has been given in both Java and Kotlin Programming Language
for Android.
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
Shared Preferences are suitable for different situations. For example, when the user’s
settings need to be saved or to store data that can be used in different activities within the
app. As you know, onPause() will always be called before your activity is placed in the
background or destroyed, So for the data to be saved persistently, it’s preferred to save it in
onPause(), which could be restored in onCreate() of the activity. The data stored using shared
preferences are kept private within the scope of the application. However, shared preferences
are different from that activity’s instance state.
How are Shared Preferences different from Saved Instance State?
Shared Preferences Saved Instance State
Persist Data across user sessions, even if
Preserves state data across activity instances
the app is killed and restarted, or the
in the same user session.
device is rebooted
Data that should be remembered across Data that should not be remembered across
sessions, such as the user’s preferred sessions, such as the currently selected tab or
settings or game score. current state of activity.
A common use is to store user A common use is to recreate the state after
preferences the device has been rotated
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
How to Create Shared Preferences?
The first thing we need to do is to create one shared preferences file per app. So name it with
the package name of your app- unique and easy to associate with the app. When you want to
get the values, call the getSharedPreferences() method. Shared Preferences provide modes
of storing the data (private mode and public mode). It is for backward compatibility- use
only MODE_PRIVATE to be secure.
public abstract SharedPreferences getSharedPreferences (String name, int mode)
This method takes two arguments, the first being the name of the SharedPreference(SP)
file and the other is the context mode that we want to store our file in.
MODE_PUBLIC will make the file public which could be accessible by other applications
on the device
MODE_PRIVATE keeps the files private and secures the user’s data.
MODE_APPEND is used while reading the data from the SP file.
Nested classes of Shared Preferences
1. [Link]: Interface used to write(edit) data in the SP file. Once
editing has been done, one must commit() or apply() the changes made to the file.
2. [Link](): Called when a shared
preference is changed, added, or removed. This may be called even if a preference is
set to its existing value. This callback will be run on your main thread.
Following are the methods of Shared Preferences
1. contains(String key): This method is used to check whether the preferences contain a
preference.
2. edit(): This method is used to create a new Editor for these preferences, through
which you can make modifications to the data in the preferences and atomically
commit those changes back to the SharedPreferences object.
3. getAll(): This method is used to retrieve all values from the preferences.
4. getBoolean(String key, boolean defValue): This method is used to retrieve a
boolean value from the preferences.
5. getFloat(String key, float defValue): This method is used to retrieve a float value
from the preferences.
6. getInt(String key, int defValue): This method is used to retrieve an int value from
the preferences.
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
7. getLong(String key, long defValue): This method is used to retrieve a long value
from the preferences.
8. getString(String key, String defValue): This method is used to retrieve a String
value from the preferences.
9. getStringSet(String key, Set defValues): This method is used to retrieve a set of
String values from the preferences.
10. registerOnSharedPreferencechangeListener([Link]
encechangeListener listener): This method is used to register a callback to be
invoked when a change happens to a preference.
11. unregisterOnSharedPreferencechangeListener([Link]
ferencechangeListener listener): This method is used to unregister a previous
callback.
Example to Demonstrate the use of Shared Preferences in Android
Below is the small demo for Shared Preferences. In this particular demo, there are
two EditTexts, which save and retain the data entered earlier in them. This type of feature can
be seen in applications with forms. Using Shared Preferences, the user will not have to fill in
details again and again. Invoke the following code inside the activity_main.xml file to
implement the UI:
• XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText">
<TextView
android:id="@+id/textview"
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Shared Preferences Demo"
android:textColor="@android:color/black"
android:textSize="24sp" />
<!--EditText to take the data from the user and save the data in SharedPreferences-->
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Enter your Name"
android:padding="10dp" />
<!--EditText to take the data from the user and save the data in SharedPreferences-->
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit1"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
android:hint="Enter your Age"
android:inputType="number"
android:padding="10dp" />
</RelativeLayout>
Working with the MainActivity file to handle the two of the EditText to save the data
entered by the user inside the SharedPreferences.
Below is the code for the MainActivity file. Comments are added inside the code to
understand the code in more detail.
• Java
• Kotlin
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText name, age;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
name = findViewById([Link].edit1);
age = findViewById([Link].edit2);
}
// Fetch the stored data in onResume() Because this is what will be called when the app opens
again
@Override
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
protected void onResume() {
[Link]();
// Fetching the stored data from the SharedPreference
SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE);
String s1 = [Link]("name", "");
int a = [Link]("age", 0);
// Setting the fetched data in the EditTexts
[Link](s1);
[Link]([Link](a));
}
// Store the data in the SharedPreference in the onPause() method
// When the user closes the application onPause() will be called and data will be stored
@Override
protected void onPause() {
[Link]();
// Creating a shared pref object with a file name "MySharedPref" in private mode
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",
MODE_PRIVATE);
[Link] myEdit = [Link]();
// write all the data entered by the user in SharedPreference and apply
[Link]("name", [Link]().toString());
[Link]("age", [Link]([Link]().toString()));
[Link]();
}
}
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
Resource types overview
Each page in this section describes the usage, format, and syntax for a certain type of app
resource that you can provide in your project resources directory (res/).
Here's a brief summary of each page:
Animation resources
Define pre-determined animations.
Tween animations are saved in res/anim/ and accessed from the [Link] class.
Frame animations are saved in res/drawable/ and accessed from the [Link] class.
Color state list resource
Define a color resource that changes based on the View state.
Saved in res/color/ and accessed from the [Link] class.
Drawable resources
Define various graphics with bitmaps or XML.
Saved in res/drawable/ and accessed from the [Link] class.
Layout resource
Define the layout for your application UI.
Saved in res/layout/ and accessed from the [Link] class.
Menu resource
Define the contents of your application menus.
Saved in res/menu/ and accessed from the [Link] class.
String resources
Define strings, string arrays, and plurals and include string formatting and styling.
Saved in res/values/ and accessed from the [Link], [Link], and [Link] classes.
Style resource
Define the look and format for UI elements.
Saved in res/values/ and accessed from the [Link] class.
Font resources
Define font families and include custom fonts in XML.
Saved in res/font/ and accessed from the [Link] class.
More resource types
Define other primitive values as static resources, including the following:
Bool
XML resource that carries a boolean value.
Color
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
XML resource that carries a hexadecimal color value.
Dimension
XML resource that carries a dimension value with a unit of measure.
ID
XML resource that provides a unique identifier for application resources and components.
Integer
XML resource that carries an integer value.
Integer array
XML resource that provides an array of integers.
Typed array
XML resource that provides a TypedArray, which you can use for an array of drawables.
Android Resource types Tutorial
Today we shall study types of Android resources. We shall have a brief introduction and a
glance over the types. It is theoretical so please be patient.
5.4.1 Introduction
Android resource types are as follows:
• Drawable resources
• Color state list resources
• Animation resources
• Layout resources
• Menu resources
• Style resource
• String resources
• Others
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
5.4.2 Android Drawable Resources
These resources define the graphics of application with xml or bitmaps. They are accessed
from [Link] class and saved in res/drawable/ folder. It is a general concept for a graphic
that can be drawn to the screen which can be retrieved with APIs. The different types of
drawables are as follows:
• Nine-Patch file: This is a PNG file which has stretchable regions. With this image can
be resized according to content.
• Layer List: This is a drawable which manages an array of other Drawables They are
drawn in array order. Element with largest index will be at top.
• Level list: This is an XML file. It defines one drawable which manages number of
alternate drawables. These alternatives are assigned with a maximum number.
• Bitmap file: This is a simple bitmap graphic file.
• Clip drawable: This XML file defines a drawable that clips another drawable.
• Shape drawable: This XML file defines geometric shape.
• Transition drawable: This Xml file deals with the transition. It cross-fades between
two drawable resources.
5.4.3 Android Color State List Resources
A ColorStateList is an object which can be defined in XML. This can be applied as a color.
Depending on the state of view object to which it is applied the color actually changes. Each
color can be defined in a XML file under <item /> tag. So the state list in an XML file can be
described. When state changes, state list is traversed from top to bottom and the most suitable
match is picked.
5.4.4 Android Animation Resource
There are two types of animations which an animation resource can refer to and they are:
1. Property Animation: An animator is used to set an object’s property over a period of
time. In short we modify the properties of object.
2. View Animation: There are two types of animations which can be viewed:
a. Frame animation: A sequence of images is displayed in order
b. Tween animation: An animation is created by performing a series of
transformations on a single image.
5.4.5 Android Layout Resource
Android layout resource is used to define the user interface of application. We already know
the usage of layouts so there is no point in redundancy.
5.4.6 Android Menu
Android Menu resource is used to design and define the menu of application. We have
options menu, context menu and submenu. This can be inflated by MenuInflater.
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe
Dr. Babasaheb Ambedkar Technological University Testing Android Applications
5.4.7 Android String Resource
Android String resource provides text strings for application. We have an option to format
text and style it as well. We have three types of resources:
• String Array: It is an xml resource which provides an array of strings
• String: This is an xml resource which provides a single string
• Quantity Strings: It is an xml resource. It carries the strings for pluralization.
5.4.8 Android Style Resources
Style resource as the name suggests it is going to define the format and look of user interface.
An individual view can have a specific style. An entire activity or an application can be
stylized by manifest file. It is nothing but a resource which has to be referenced properly.
5.4.9 Other
Other resources are listed below:
• Color: It is an xml resource which has a hexadecimal number. This number
corresponds to a particular color.
• Bool: This resource carries a color value.
• Dimension: It contains the value of dimension with the specific unit of measure.
• ID: This is also an xml resource. This is an unique identifier which identifies
application resources and components.
• Integer: It is an xml resource which carries an integer value.
• Typed Array: We can use this as array of drawables.
• Integer Array: It is an xml resource and it is an array of integers.
Class: First Year Information Technology Subject In-Charge: Prof. Karan R. Korpe